deku-prop-types
Advanced tools
Comparing version 0.2.4 to 0.2.5
149
lib/index.js
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _propTypes = require('./prop-types'); | ||
var checkerFactory = function checkerFactory(name, validator) { | ||
return { | ||
get isRequired() { | ||
this.required = true; | ||
return this; | ||
}, | ||
validate: function validate(prop, key) { | ||
if (this.required && prop === undefined) { | ||
return new Error(key + ' is required'); | ||
} | ||
var _propTypes2 = _interopRequireDefault(_propTypes); | ||
if (prop !== undefined) { | ||
if (typeof validator === 'string' && (typeof prop === 'undefined' ? 'undefined' : _typeof(prop)) !== validator) { | ||
var actualType = typeof prop === 'undefined' ? 'undefined' : _typeof(prop); | ||
return new TypeError('Expected ' + key + ' to be of type `' + name + '`, but got `' + actualType + '`'); | ||
} | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
if (typeof validator === 'function') { | ||
return validator(prop, key); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
module.exports.propTypes = _propTypes2.default; | ||
module.exports.propTypes = { | ||
get any() { | ||
return checkerFactory('any'); | ||
}, | ||
get array() { | ||
return checkerFactory('array', function (prop, key) { | ||
if (!Array.isArray(prop)) { | ||
var actualType = typeof prop === 'undefined' ? 'undefined' : _typeof(prop); | ||
return new TypeError('Expected ' + key + ' to be an `Array`, but got `' + actualType + '`'); | ||
} | ||
}); | ||
}, | ||
get arrayOf() { | ||
return function (validator) { | ||
return checkerFactory('arrayOf', function (prop, key) { | ||
if (!Array.isArray(prop)) { | ||
var actualType = typeof prop === 'undefined' ? 'undefined' : _typeof(prop); | ||
return new TypeError('Expected ' + key + ' to be an `Array`, but got `' + actualType + '`'); | ||
} | ||
var anyErrors = prop.some(function (p) { | ||
return validator.validate(p) instanceof Error; | ||
}); | ||
if (anyErrors) { | ||
return new TypeError(key + ' does not consist of the correct type'); | ||
} | ||
}); | ||
}; | ||
}, | ||
get bool() { | ||
return checkerFactory('bool', 'boolean'); | ||
}, | ||
get func() { | ||
return checkerFactory('func', 'function'); | ||
}, | ||
get instanceOf() { | ||
return function (constructor) { | ||
return checkerFactory('instanceOf', function (prop, key) { | ||
if (!(prop instanceof constructor)) { | ||
/* eslint-disable prefer-reflect */ | ||
var actualConstructorName = prop === null ? prop : Object.getPrototypeOf(prop).constructor.name; | ||
var errorMsg = 'Expected ' + key + ' to be an instance of `' + constructor.name + '`, ' + ('but got `' + actualConstructorName + '`'); | ||
return new TypeError(errorMsg); | ||
} | ||
}); | ||
}; | ||
}, | ||
get number() { | ||
return checkerFactory('number', 'number'); | ||
}, | ||
get object() { | ||
return checkerFactory('object', 'object'); | ||
}, | ||
get objectOf() { | ||
return function (validator) { | ||
return checkerFactory('objectOf', function (prop, key) { | ||
var propKeys = Object.keys(prop); | ||
for (var i = 0; i < propKeys.length; i++) { | ||
var validatorResult = validator.validate(prop[propKeys[i]], key + '.' + propKeys[i]); | ||
if (validatorResult instanceof Error) { | ||
return validatorResult; | ||
} | ||
} | ||
}); | ||
}; | ||
}, | ||
get oneOf() { | ||
return function (allowedValues) { | ||
return checkerFactory('oneOf', function (prop, key) { | ||
var isAllowed = allowedValues.some(function (value) { | ||
return value === prop; | ||
}); | ||
if (!isAllowed) { | ||
return new TypeError(key + ' is not one of the allowed values'); | ||
} | ||
}); | ||
}; | ||
}, | ||
get oneOfType() { | ||
return function (allowedTypes) { | ||
return checkerFactory('oneOfType', function (prop, key) { | ||
var isAllowed = !allowedTypes.every(function (type) { | ||
return type.validate(prop) instanceof Error; | ||
}); | ||
if (!isAllowed) { | ||
return new TypeError(key + ' is not one of the allowed types'); | ||
} | ||
}); | ||
}; | ||
}, | ||
get shape() { | ||
return function (propsObj) { | ||
return checkerFactory('shape', function (prop, key) { | ||
var validators = Object.keys(propsObj); | ||
for (var i = 0; i < validators.length; i++) { | ||
var validatorResult = propsObj[validators[i]].validate(prop[validators[i]], key + '.' + validators[i]); | ||
if (validatorResult instanceof Error) { | ||
return validatorResult; | ||
} | ||
} | ||
}); | ||
}; | ||
}, | ||
get string() { | ||
return checkerFactory('string', 'string'); | ||
} | ||
}; | ||
/** | ||
* Determine if the props are valid | ||
* @param {Object} propTypes - an object with values being checkers | ||
* @param {Object} props - an object to check for validity | ||
* @throws {Error} - if a prop is invalid | ||
*/ | ||
var validate = function validate(propTypes, props) { | ||
@@ -156,2 +32,7 @@ Object.keys(propTypes).forEach(function (key) { | ||
/** | ||
* Transform a component into a component with prop validation | ||
* @param {Function|Object} component - the component to add validation to | ||
* @return {Function|Object} -the modified component with validation added | ||
*/ | ||
module.exports.validate = function (component) { | ||
@@ -158,0 +39,0 @@ /* eslint-disable no-process-env */ |
{ | ||
"name": "deku-prop-types", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Prop type validation for Deku components", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
14899
6
206
2
1