UNPKG

react-schema

Version:

Use react like PropTypes for generic object validation.

50 lines (44 loc) 1.31 kB
'use strict'; /** * Performs validation on the a set of properties. * * @param {Object|Function} propTypes * An object containing the property-type definitions (schema) or a * single PropType. * * @param {Object} props * An object of properties to validate or a single value of a single * <propTypes> definiiton was passed. * * @param {String} [displayName] * The name of the component or module being validated. * * Used in formatting the error message(s). * * @return {Object} result * The validation results. Looks something like this: * * { * isValid: Boolean, * errors: Object.<String, String>? * } */ exports.validate = function (propTypes, props, displayName) { var result = { isValid: true }; if (typeof propTypes === 'function') { propTypes = { value: propTypes }; props = { value: props }; } Object.keys(propTypes).forEach(function (key) { var validator = propTypes[key]; var error = validator(props, key, displayName); if (error !== null) { result.isValid = false; result.errors = result.errors || {}; result.errors[key] = error; } }); return result; }; exports.PropTypes = require('./PropTypes'); //# sourceMappingURL=index.js.map