@dhis2/prop-types
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -21,21 +21,23 @@ "use strict"; | ||
isRequired = _ref.isRequired; | ||
return function (props, propName, componentName) { | ||
var arr = props[propName]; | ||
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, "`,"); | ||
if (isRequired && typeof arr === 'undefined') { | ||
return new Error("".concat(propName, " is required.")); | ||
return new Error("".concat(baseMsg, " this prop is required but no value was found.")); | ||
} | ||
if (arr && !Array.isArray(arr)) { | ||
return new Error("".concat(propName, " is not an array.")); | ||
return new Error("".concat(baseMsg, " prop value is not an array.")); | ||
} | ||
if (arr && arr.length > max) { | ||
return new Error( // prettier-ignore | ||
"".concat(propName, " array has a length of ").concat(arr.length, ", but the maximum is ").concat(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( // prettier-ignore | ||
"".concat(propName, " array has a length of ").concat(arr.length, ", but the minimum is ").concat(min)); | ||
return new Error("".concat(baseMsg, " array has a length of ").concat(arr.length, ", but the minimum is ").concat(min)); | ||
} | ||
@@ -42,0 +44,0 @@ |
@@ -13,18 +13,32 @@ "use strict"; | ||
var instanceOfComponentFactory = function instanceOfComponentFactory(Component, isRequired) { | ||
return function (props, propName) { | ||
var children = props[propName]; | ||
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 children = props[propSelector]; | ||
var propName = propFullName || propSelector; | ||
var count = _react.default.Children.count(children); | ||
var isWrappedInArrayOf = !!propFullName; | ||
var baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); | ||
if (isWrappedInArrayOf && count === 1 && children === '') { | ||
// When mapping over an empty array to render components react will return '' | ||
// So this is a valid case and should not produce an error | ||
return null; | ||
} | ||
if (isRequired && count === 0) { | ||
return new Error("".concat(propName, " is required.")); | ||
return new Error("".concat(baseMsg, " this is a required prop, but no component instance was found")); | ||
} | ||
if (count > 1) { | ||
return new Error("Prop validator 'instanceOfComponent' expected 1 component instance, instead found ".concat(count, ".")); | ||
return new Error("".concat(baseMsg, " expected 1 component instance, instead found ").concat(count, ".")); | ||
} | ||
if (children.type !== Component) { | ||
var componentName = Component.name || Component.displayName; | ||
return new Error("Child at index ".concat(propName, " is not an instance of component ").concat(componentName, ".")); | ||
var expectedComponent = typeof Component === 'string' ? Component : Component.name || Component.displayName; | ||
var foundComponent = typeof children.type === 'string' // native elements | ||
? children.type : children.type.name || children.type.displayName; | ||
return new Error("".concat(baseMsg, " expected an instance of `").concat(expectedComponent, "` but found an instance of `").concat(foundComponent, "`.")); | ||
} | ||
@@ -31,0 +45,0 @@ |
@@ -15,9 +15,20 @@ "use strict"; | ||
var mutuallyExclusiveFactory = function mutuallyExclusiveFactory(exlusivePropNames, propType, isRequired) { | ||
return function (props, propName, componentName) { | ||
if (exlusivePropNames.length === 0) { | ||
return new Error("mutuallyExclusive was called without any arguments for property '".concat(propName, "'.")); | ||
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 propName = propFullName || propSelector; | ||
var isWrappedInArrayOf = !!propFullName; | ||
var baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); // Usage errors | ||
if (isWrappedInArrayOf) { | ||
return new Error("mutuallyExclusive is being wrapped in `arrayOf` for property `".concat(propName, "` on component `").concat(componentName, "`. This is not supported.")); | ||
} | ||
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 && typeof props[propName] === 'undefined') { | ||
return new Error("".concat(propName, " is required.")); | ||
return new Error("".concat(baseMsg, " this prop is required but no value was found.")); | ||
} // This is how to programatically invoke a propTypes check | ||
@@ -35,3 +46,3 @@ // https://github.com/facebook/prop-types#proptypescheckproptypes | ||
if (thruthySiblingPropName) { | ||
return new Error("Property '".concat(propName, "' is mutually exclusive with '").concat(thruthySiblingPropName, "', but both have a value.")); | ||
return new Error("".concat(baseMsg, " Property '").concat(propName, "' is mutually exclusive with '").concat(thruthySiblingPropName, "', but both have a thruthy value.")); | ||
} | ||
@@ -38,0 +49,0 @@ } |
@@ -8,21 +8,23 @@ import propTypes from 'prop-types'; | ||
isRequired | ||
}) => (props, propName, componentName) => { | ||
const arr = props[propName]; | ||
}) => (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 \`${propName}\` supplied to \`${componentName}\`,`; | ||
if (isRequired && typeof arr === 'undefined') { | ||
return new Error(`${propName} is required.`); | ||
return new Error(`${baseMsg} this prop is required but no value was found.`); | ||
} | ||
if (arr && !Array.isArray(arr)) { | ||
return new Error(`${propName} is not an array.`); | ||
return new Error(`${baseMsg} prop value is not an array.`); | ||
} | ||
if (arr && arr.length > max) { | ||
return new Error( // prettier-ignore | ||
`${propName} array has a length of ${arr.length}, but the maximum is ${max}`); | ||
return new Error(`${baseMsg} array has a length of ${arr.length}, but the maximum is ${max}`); | ||
} | ||
if (arr && arr.length < min) { | ||
return new Error( // prettier-ignore | ||
`${propName} array has a length of ${arr.length}, but the minimum is ${min}`); | ||
return new Error(`${baseMsg} array has a length of ${arr.length}, but the minimum is ${min}`); | ||
} | ||
@@ -29,0 +31,0 @@ |
import React from 'react'; | ||
const instanceOfComponentFactory = (Component, isRequired) => (props, propName) => { | ||
const children = props[propName]; | ||
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 children = props[propSelector]; | ||
const propName = propFullName || propSelector; | ||
const count = React.Children.count(children); | ||
const isWrappedInArrayOf = !!propFullName; | ||
const baseMsg = `Invalid prop \`${propName}\` supplied to \`${componentName}\`,`; | ||
if (isWrappedInArrayOf && count === 1 && children === '') { | ||
// When mapping over an empty array to render components react will return '' | ||
// So this is a valid case and should not produce an error | ||
return null; | ||
} | ||
if (isRequired && count === 0) { | ||
return new Error(`${propName} is required.`); | ||
return new Error(`${baseMsg} this is a required prop, but no component instance was found`); | ||
} | ||
if (count > 1) { | ||
return new Error(`Prop validator 'instanceOfComponent' expected 1 component instance, instead found ${count}.`); | ||
return new Error(`${baseMsg} expected 1 component instance, instead found ${count}.`); | ||
} | ||
if (children.type !== Component) { | ||
const componentName = Component.name || Component.displayName; | ||
return new Error(`Child at index ${propName} is not an instance of component ${componentName}.`); | ||
const expectedComponent = typeof Component === 'string' ? Component : Component.name || Component.displayName; | ||
const foundComponent = typeof children.type === 'string' // native elements | ||
? children.type : children.type.name || children.type.displayName; | ||
return new Error(`${baseMsg} expected an instance of \`${expectedComponent}\` but found an instance of \`${foundComponent}\`.`); | ||
} | ||
@@ -19,0 +32,0 @@ |
import propTypes from 'prop-types'; | ||
const mutuallyExclusiveFactory = (exlusivePropNames, propType, isRequired) => (props, propName, componentName) => { | ||
if (exlusivePropNames.length === 0) { | ||
return new Error(`mutuallyExclusive was called without any arguments for property '${propName}'.`); | ||
const mutuallyExclusiveFactory = (exlusivePropNames, 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 propName = propFullName || propSelector; | ||
const isWrappedInArrayOf = !!propFullName; | ||
const baseMsg = `Invalid prop \`${propName}\` supplied to \`${componentName}\`,`; // Usage errors | ||
if (isWrappedInArrayOf) { | ||
return new Error(`mutuallyExclusive is being wrapped in \`arrayOf\` for property \`${propName}\` on component \`${componentName}\`. This is not supported.`); | ||
} | ||
if (exlusivePropNames.length === 0) { | ||
return new Error(`mutuallyExclusive was called without any arguments for property \`${propName}\` on component \`${componentName}\`. Please add the required arguments.`); | ||
} // Validation errors | ||
if (isRequired && typeof props[propName] === 'undefined') { | ||
return new Error(`${propName} is required.`); | ||
return new Error(`${baseMsg} this prop is required but no value was found.`); | ||
} // This is how to programatically invoke a propTypes check | ||
@@ -22,3 +33,3 @@ // https://github.com/facebook/prop-types#proptypescheckproptypes | ||
if (thruthySiblingPropName) { | ||
return new Error(`Property '${propName}' is mutually exclusive with '${thruthySiblingPropName}', but both have a value.`); | ||
return new Error(`${baseMsg} Property '${propName}' is mutually exclusive with '${thruthySiblingPropName}', but both have a thruthy value.`); | ||
} | ||
@@ -25,0 +36,0 @@ } |
@@ -0,1 +1,8 @@ | ||
# [1.2.0](https://github.com/dhis2/prop-types/compare/v1.1.1...v1.2.0) (2019-10-07) | ||
### Features | ||
* messages and instanceOfComponent when wrapped in `arrayOf` ([#56](https://github.com/dhis2/prop-types/issues/56)) ([e7a2355](https://github.com/dhis2/prop-types/commit/e7a2355)) | ||
## [1.1.1](https://github.com/dhis2/prop-types/compare/v1.1.0...v1.1.1) (2019-09-24) | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "@dhis2/prop-types", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"main": "./build/cjs/index.js", | ||
@@ -29,3 +29,4 @@ "module": "./build/es/index.js", | ||
"@babel/preset-env": "^7.6.2", | ||
"@dhis2/cli-style": "4.1.1", | ||
"@dhis2/cli-style": "4.1.3", | ||
"babel-eslint": "^10.0.3", | ||
"jsdoc-to-markdown": "^5.0.1" | ||
@@ -32,0 +33,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
27759
434
6