@dhis2/prop-types
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -12,53 +12,50 @@ "use strict"; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const arrayWithLengthFactory = ({ | ||
min = 0, | ||
max = Infinity, | ||
propType, | ||
isRequired | ||
}) => (props, propSelector, // normally a propName, but when wrapped in arrayOf an index | ||
componentName, location, propFullName // normally null but a string like "propName[index]" when wrapped in arrayOf | ||
) => { | ||
const arr = props[propSelector]; | ||
const propName = propFullName || propSelector; | ||
const baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); | ||
const insideArrayOf = !!propFullName; | ||
var arrayWithLengthFactory = function arrayWithLengthFactory(_ref) { | ||
var _ref$min = _ref.min, | ||
min = _ref$min === void 0 ? 0 : _ref$min, | ||
_ref$max = _ref.max, | ||
max = _ref$max === void 0 ? Infinity : _ref$max, | ||
propType = _ref.propType, | ||
isRequired = _ref.isRequired; | ||
return function (props, propSelector, // normally a propName, but when wrapped in arrayOf an index | ||
componentName, location, propFullName // normally null but a string like "propName[index]" when wrapped in arrayOf | ||
) { | ||
var arr = props[propSelector]; | ||
var propName = propFullName || propSelector; | ||
var baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); | ||
var insideArrayOf = !!propFullName; | ||
if (isRequired && typeof arr === 'undefined') { | ||
return new Error("".concat(baseMsg, " this prop is required but no value was found.")); | ||
} | ||
if (isRequired && typeof arr === 'undefined') { | ||
return new Error("".concat(baseMsg, " this prop is required but no value was found.")); | ||
} | ||
if (arr && !Array.isArray(arr)) { | ||
return new Error("".concat(baseMsg, " prop value is not an array.")); | ||
} | ||
if (arr && !Array.isArray(arr)) { | ||
return new Error("".concat(baseMsg, " prop value is not an array.")); | ||
} | ||
if (arr && arr.length > max) { | ||
return new Error("".concat(baseMsg, " array has a length of ").concat(arr.length, ", but the maximum is ").concat(max)); | ||
} | ||
if (arr && arr.length > max) { | ||
return new Error("".concat(baseMsg, " array has a length of ").concat(arr.length, ", but the maximum is ").concat(max)); | ||
} | ||
if (arr && arr.length < min) { | ||
return new Error("".concat(baseMsg, " array has a length of ").concat(arr.length, ", but the minimum is ").concat(min)); | ||
} | ||
if (arr && arr.length < min) { | ||
return new Error("".concat(baseMsg, " array has a length of ").concat(arr.length, ", but the minimum is ").concat(min)); | ||
} | ||
if (arr && propType) { | ||
const checkPropName = insideArrayOf ? location : propName; | ||
const checkPropType = insideArrayOf ? // array should be array containing only the given type | ||
_propTypes.default.arrayOf(_propTypes.default.arrayOf(propType)) : // array should contain only the given type | ||
_propTypes.default.arrayOf(propType); | ||
const checkPropTypes = { | ||
[checkPropName]: checkPropType | ||
}; | ||
const checkProps = insideArrayOf ? { | ||
[location]: props | ||
} : props; // When not inside an array, the error message only reads correctly | ||
// when using "prop" | ||
if (arr && propType) { | ||
var checkPropName = insideArrayOf ? location : propName; | ||
var checkPropType = insideArrayOf ? // array should be array containing only the given type | ||
_propTypes.default.arrayOf(_propTypes.default.arrayOf(propType)) : // array should contain only the given type | ||
_propTypes.default.arrayOf(propType); | ||
const checkProp = insideArrayOf ? propName : 'prop'; | ||
var checkPropTypes = _defineProperty({}, checkPropName, checkPropType); | ||
_propTypes.default.checkPropTypes(checkPropTypes, checkProps, checkProp, componentName); | ||
} | ||
var checkProps = insideArrayOf ? _defineProperty({}, location, props) : props; // When not inside an array, the error message only reads correctly | ||
// when using "prop" | ||
var checkProp = insideArrayOf ? propName : 'prop'; | ||
_propTypes.default.checkPropTypes(checkPropTypes, checkProps, checkProp, componentName); | ||
} | ||
return null; | ||
}; | ||
return null; | ||
}; | ||
@@ -88,12 +85,12 @@ /** | ||
function arrayWithLength(min, max, propType) { | ||
var fn = arrayWithLengthFactory({ | ||
min: min, | ||
max: max, | ||
propType: propType, | ||
const fn = arrayWithLengthFactory({ | ||
min, | ||
max, | ||
propType, | ||
isRequired: false | ||
}); | ||
fn.isRequired = arrayWithLengthFactory({ | ||
min: min, | ||
max: max, | ||
propType: propType, | ||
min, | ||
max, | ||
propType, | ||
isRequired: true | ||
@@ -100,0 +97,0 @@ }); |
@@ -8,36 +8,34 @@ "use strict"; | ||
var instanceOfComponentFactory = function instanceOfComponentFactory(Component, isRequired) { | ||
return function (props, propSelector, // normally a propName, but when wrapped in arrayOf an index | ||
componentName, _location, propFullName // normally null but a string like "propName[index]" when wrapped in arrayOf | ||
) { | ||
var child = props[propSelector]; | ||
var propName = propFullName || propSelector; | ||
var hasRenderableChild = child === 0 || !!child; | ||
var baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); | ||
const instanceOfComponentFactory = (Component, isRequired) => (props, propSelector, // normally a propName, but when wrapped in arrayOf an index | ||
componentName, _location, propFullName // normally null but a string like "propName[index]" when wrapped in arrayOf | ||
) => { | ||
const child = props[propSelector]; | ||
const propName = propFullName || propSelector; | ||
const hasRenderableChild = child === 0 || !!child; | ||
const baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); | ||
if (Array.isArray(child)) { | ||
return new Error("".concat(baseMsg, " expected a single component instance but received an array.")); | ||
} | ||
if (Array.isArray(child)) { | ||
return new Error("".concat(baseMsg, " expected a single component instance but received an array.")); | ||
} | ||
if (!hasRenderableChild) { | ||
if (isRequired) { | ||
return new Error("".concat(baseMsg, " this is a required property but its value is `").concat(child, "`.")); | ||
} else { | ||
return null; | ||
} | ||
if (!hasRenderableChild) { | ||
if (isRequired) { | ||
return new Error("".concat(baseMsg, " this is a required property but its value is `").concat(child, "`.")); | ||
} else { | ||
return null; | ||
} | ||
} | ||
var expectedComponentName = typeof Component === 'string' ? Component : Component.name || Component.displayName; | ||
var foundComponentName = typeof child.type !== 'string' ? child.type && child.type.name ? child.type.name : child.type : child.type && (child.type.name || child.type.displayName); | ||
const expectedComponentName = typeof Component === 'string' ? Component : Component.name || Component.displayName; | ||
const foundComponentName = typeof child.type !== 'string' ? child.type && child.type.name ? child.type.name : child.type : child.type && (child.type.name || child.type.displayName); | ||
if (!foundComponentName) { | ||
return new Error("".concat(baseMsg, " could not read component name. Property value does not look like a component instance.")); | ||
} | ||
if (!foundComponentName) { | ||
return new Error("".concat(baseMsg, " could not read component name. Property value does not look like a component instance.")); | ||
} | ||
if (child.type !== Component) { | ||
return new Error("".concat(baseMsg, " expected an instance of `").concat(expectedComponentName, "` but found an instance of `").concat(foundComponentName, "`.")); | ||
} | ||
if (child.type !== Component) { | ||
return new Error("".concat(baseMsg, " expected an instance of `").concat(expectedComponentName, "` but found an instance of `").concat(foundComponentName, "`.")); | ||
} | ||
return null; | ||
}; | ||
return null; | ||
}; | ||
@@ -73,5 +71,5 @@ /** | ||
function instanceOfComponent(Component) { | ||
var fn = instanceOfComponentFactory(Component, false); | ||
const fn = instanceOfComponentFactory(Component, false); | ||
fn.isRequired = instanceOfComponentFactory(Component, true); | ||
return fn; | ||
} |
@@ -12,34 +12,30 @@ "use strict"; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const mutuallyExclusiveFactory = (exlusivePropNames, propType, isRequired) => (props, propName, componentName) => { | ||
const baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); | ||
const isDefined = typeof props[propName] !== 'undefined'; // Usage errors | ||
var mutuallyExclusiveFactory = function mutuallyExclusiveFactory(exlusivePropNames, propType, isRequired) { | ||
return function (props, propName, componentName) { | ||
var baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); | ||
var isDefined = typeof props[propName] !== 'undefined'; // Usage errors | ||
if (exlusivePropNames.length === 0) { | ||
return new Error("mutuallyExclusive was called without any arguments for property `".concat(propName, "` on component `").concat(componentName, "`. Please add the required arguments.")); | ||
} // Validation errors | ||
if (exlusivePropNames.length === 0) { | ||
return new Error("mutuallyExclusive was called without any arguments for property `".concat(propName, "` on component `").concat(componentName, "`. Please add the required arguments.")); | ||
} // Validation errors | ||
if (isRequired && !isDefined) { | ||
return new Error("".concat(baseMsg, " this prop is required but no value was found.")); | ||
} // This is how to programatically invoke a propTypes check | ||
// https://github.com/facebook/prop-types#proptypescheckproptypes | ||
if (isRequired && !isDefined) { | ||
return new Error("".concat(baseMsg, " this prop is required but no value was found.")); | ||
} // This is how to programatically invoke a propTypes check | ||
// https://github.com/facebook/prop-types#proptypescheckproptypes | ||
_propTypes.default.checkPropTypes({ | ||
[propName]: propType | ||
}, props, 'prop', componentName); | ||
_propTypes.default.checkPropTypes(_defineProperty({}, propName, propType), props, 'prop', componentName); | ||
if (props[propName]) { | ||
const thruthySiblingPropName = exlusivePropNames.find(name => props[name] && name !== propName); | ||
if (props[propName]) { | ||
var thruthySiblingPropName = exlusivePropNames.find(function (name) { | ||
return props[name] && name !== propName; | ||
}); | ||
if (thruthySiblingPropName) { | ||
return new Error("".concat(baseMsg, " Property '").concat(propName, "' is mutually exclusive with '").concat(thruthySiblingPropName, "', but both have a thruthy value.")); | ||
} | ||
if (thruthySiblingPropName) { | ||
return new Error("".concat(baseMsg, " Property '").concat(propName, "' is mutually exclusive with '").concat(thruthySiblingPropName, "', but both have a thruthy value.")); | ||
} | ||
} | ||
return null; | ||
}; | ||
return null; | ||
}; | ||
@@ -76,5 +72,5 @@ /** | ||
function mutuallyExclusive(exlusivePropNames, propType) { | ||
var fn = mutuallyExclusiveFactory(exlusivePropNames, propType, false); | ||
const fn = mutuallyExclusiveFactory(exlusivePropNames, propType, false); | ||
fn.isRequired = mutuallyExclusiveFactory(exlusivePropNames, propType, true); | ||
return fn; | ||
} |
@@ -12,35 +12,31 @@ "use strict"; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const isEmpty = value => typeof value === 'undefined' || value === null || value === ''; | ||
var isEmpty = function isEmpty(value) { | ||
return typeof value === 'undefined' || value === null || value === ''; | ||
}; | ||
const requiredIfFactory = (condition, propType, isRequired) => (props, propName, componentName) => { | ||
const propValue = props[propName]; // Usage errors | ||
var requiredIfFactory = function requiredIfFactory(condition, propType, isRequired) { | ||
return function (props, propName, componentName) { | ||
var propValue = props[propName]; // Usage errors | ||
if (isRequired) { | ||
return new Error("Property `".concat(propName, "` on component `").concat(componentName, "` is using the `requiredIf` prop-validator combined with `.isRequired`. This is an invalid combination.")); | ||
} | ||
if (isRequired) { | ||
return new Error("Property `".concat(propName, "` on component `").concat(componentName, "` is using the `requiredIf` prop-validator combined with `.isRequired`. This is an invalid combination.")); | ||
} | ||
if (typeof condition !== 'function') { | ||
return new Error("The `condition` argument passed to the `requiredIf` prop-validator was not a function."); | ||
} | ||
if (typeof condition !== 'function') { | ||
return new Error("The `condition` argument passed to the `requiredIf` prop-validator was not a function."); | ||
} | ||
if (typeof propType !== 'function') { | ||
return new Error("The `propType` argument passed to the `requiredIf` prop-validator was not a function."); | ||
} // Validation errors | ||
if (typeof propType !== 'function') { | ||
return new Error("The `propType` argument passed to the `requiredIf` prop-validator was not a function."); | ||
} // Validation errors | ||
if (condition(props) && isEmpty(propValue)) { | ||
return new Error("Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`, this prop is conditionally required but has value `").concat(propValue, "`.")); | ||
} // This is how to programatically invoke a propTypes check | ||
// https://github.com/facebook/prop-types#proptypescheckproptypes | ||
if (condition(props) && isEmpty(propValue)) { | ||
return new Error("Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`, this prop is conditionally required but has value `").concat(propValue, "`.")); | ||
} // This is how to programatically invoke a propTypes check | ||
// https://github.com/facebook/prop-types#proptypescheckproptypes | ||
_propTypes.default.checkPropTypes({ | ||
[propName]: propType | ||
}, props, 'prop', componentName); | ||
_propTypes.default.checkPropTypes(_defineProperty({}, propName, propType), props, 'prop', componentName); | ||
return null; | ||
}; | ||
return null; | ||
}; | ||
@@ -70,5 +66,5 @@ /** | ||
function requiredIf(condition, propType) { | ||
var fn = requiredIfFactory(condition, propType, false); | ||
const fn = requiredIfFactory(condition, propType, false); | ||
fn.isRequired = requiredIfFactory(condition, propType, true); | ||
return fn; | ||
} |
@@ -0,1 +1,8 @@ | ||
# [1.6.0](https://github.com/dhis2/prop-types/compare/v1.5.0...v1.6.0) (2020-03-27) | ||
### Features | ||
* add conditional propType ([e859fff](https://github.com/dhis2/prop-types/commit/e859fff2a3f0ef2470c1f4d8e2b70da7e3caa760)) | ||
# [1.5.0](https://github.com/dhis2/prop-types/compare/v1.4.0...v1.5.0) (2019-12-04) | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "@dhis2/prop-types", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"main": "./build/cjs/index.js", | ||
@@ -27,9 +27,9 @@ "module": "./build/es/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.7.4", | ||
"@babel/core": "^7.7.4", | ||
"@babel/preset-env": "^7.7.4", | ||
"@babel/preset-react": "^7.7.4", | ||
"@dhis2/cli-style": "5.0.3", | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.8.4", | ||
"@babel/preset-env": "^7.8.4", | ||
"@babel/preset-react": "^7.8.3", | ||
"@dhis2/cli-style": "6.0.0", | ||
"babel-eslint": "^10.0.3", | ||
"jest": "^24.9.0", | ||
"jest": "^25.1.0", | ||
"jsdoc-to-markdown": "^5.0.3", | ||
@@ -36,0 +36,0 @@ "react": "^16.12.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71823
25
1441