deku-prop-types
Advanced tools
Comparing version 0.2.5 to 0.3.0
@@ -7,59 +7,11 @@ 'use strict'; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _validate = require('./validate'); | ||
module.exports.propTypes = _propTypes2.default; | ||
var _validate2 = _interopRequireDefault(_validate); | ||
/** | ||
* 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) { | ||
Object.keys(propTypes).forEach(function (key) { | ||
var result = undefined; | ||
if (typeof propTypes[key] === 'function') { | ||
result = propTypes[key](props, key); | ||
} else { | ||
result = propTypes[key].validate(props[key], key); | ||
} | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
if (result instanceof Error) { | ||
throw result; | ||
} | ||
}); | ||
}; | ||
/** | ||
* 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) { | ||
/* eslint-disable no-process-env */ | ||
if (process.env.NODE_ENV === 'production') { | ||
return component; | ||
} | ||
if (typeof component === 'function') { | ||
return function (model) { | ||
validate(component.propTypes, model.props); | ||
return component(model); | ||
}; | ||
} | ||
// create render function that validates and calls original render fn | ||
var transformedComponent = { | ||
render: function render(model) { | ||
validate(component.propTypes, model.props); | ||
return component.render(model); | ||
} | ||
}; | ||
// copy original component's properties | ||
Object.keys(component).forEach(function (key) { | ||
if (key !== 'render') { | ||
transformedComponent[key] = component[key]; | ||
} | ||
}); | ||
return transformedComponent; | ||
module.exports = { | ||
PropTypes: _propTypes2.default, | ||
validate: _validate2.default | ||
}; |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var _checkerFactory = require('./checker-factory'); | ||
var _checkerFactory = require('checker-factory'); | ||
@@ -8,0 +8,0 @@ var _checkerFactory2 = _interopRequireDefault(_checkerFactory); |
{ | ||
"name": "deku-prop-types", | ||
"version": "0.2.5", | ||
"version": "0.3.0", | ||
"description": "Prop type validation for Deku components", | ||
@@ -47,3 +47,6 @@ "main": "lib/index.js", | ||
] | ||
}, | ||
"dependencies": { | ||
"checker-factory": "^1.0.0" | ||
} | ||
} |
@@ -22,7 +22,7 @@ # deku-prop-types | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
import {PropTypes, validate} from 'deku-prop-types' | ||
const Counter = ({props}) => <div>{props.count}</div> | ||
Count.propTypes = { | ||
count: propTypes.number.isRequired | ||
count: PropTypes.number.isRequired | ||
} | ||
@@ -36,7 +36,7 @@ | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
import {PropTypes, validate} from 'deku-prop-types' | ||
const Counter = { | ||
propTypes: { | ||
count: propTypes.number.isRequired | ||
count: PropTypes.number.isRequired | ||
}, | ||
@@ -52,8 +52,8 @@ render({props}) { | ||
## Supported types | ||
### propTypes.any | ||
### PropTypes.any | ||
Validate prop is of any type (not undefined) | ||
### propTypes.array | ||
### PropTypes.array | ||
Validate prop is an array | ||
### propTypes.arrayOf | ||
### PropTypes.arrayOf | ||
Validate prop is an array consisting of a type | ||
@@ -63,3 +63,3 @@ | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
import {PropTypes, validate} from 'deku-prop-types' | ||
@@ -71,3 +71,3 @@ const NamesList = ({props}) => <div> | ||
NamesList.propTypes = { | ||
names: propTypes.arrayOf(propTypes.string) | ||
names: PropTypes.arrayOf(PropTypes.string) | ||
} | ||
@@ -78,8 +78,8 @@ | ||
### propTypes.bool | ||
### PropTypes.bool | ||
Validate prop is a boolean | ||
### propTypes.func | ||
### PropTypes.func | ||
Validate prop is a function | ||
### propTypes.instanceOf | ||
### PropTypes.instanceOf | ||
Validate prop is an instance of a function or class | ||
@@ -89,3 +89,3 @@ | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
import {PropTypes, validate} from 'deku-prop-types' | ||
@@ -97,3 +97,3 @@ const ItemList ({props}) => <div> | ||
ItemList.propTypes = { | ||
list: propTypes.instanceOf(Array) | ||
list: PropTypes.instanceOf(Array) | ||
} | ||
@@ -104,8 +104,8 @@ | ||
### propTypes.number | ||
### PropTypes.number | ||
Validate prop is a number | ||
### propTypes.object | ||
### PropTypes.object | ||
Validate prop is an object | ||
### propTypes.objectOf | ||
### PropTypes.objectOf | ||
Validate prop has keys all matching an allowed type | ||
@@ -115,3 +115,3 @@ | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
import {PropTypes, validate} from 'deku-prop-types' | ||
@@ -121,3 +121,3 @@ const NameCard = ({props}) => <div>{props.person.firstName + ' ' + props.person.lastName}</div> | ||
NameCard.propTypes = { | ||
person: propTypes.objectOf(propTypes.string) | ||
person: PropTypes.objectOf(PropTypes.string) | ||
} | ||
@@ -128,3 +128,3 @@ | ||
### propTypes.oneOf | ||
### PropTypes.oneOf | ||
Validate prop is one of the allowed values | ||
@@ -134,3 +134,3 @@ | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
import {PropTypes, validate} from 'deku-prop-types' | ||
@@ -140,3 +140,3 @@ const Color = ({props}) => <div>{props.color}</div> | ||
Color.propTypes = { | ||
color: propTypes.oneOf(['red', 'green', 'blue']) | ||
color: PropTypes.oneOf(['red', 'green', 'blue']) | ||
} | ||
@@ -147,3 +147,3 @@ | ||
### propTypes.oneOfType | ||
### PropTypes.oneOfType | ||
Validate prop is one of the allowed types | ||
@@ -153,3 +153,3 @@ | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
import {PropTypes, validate} from 'deku-prop-types' | ||
@@ -159,3 +159,3 @@ const Age = ({props}) => <div>{props.age}</div> | ||
Age.propTypes = { | ||
age: propTypes.oneOfType([propTypes.number, propTypes.number]) | ||
age: PropTypes.oneOfType([PropTypes.number, PropTypes.number]) | ||
} | ||
@@ -166,3 +166,3 @@ | ||
### propTypes.shape | ||
### PropTypes.shape | ||
Validate an object's properties are of a certain type | ||
@@ -172,3 +172,3 @@ | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
import {PropTypes, validate} from 'deku-prop-types' | ||
@@ -178,5 +178,5 @@ const ConfigDisplay = ({props}) => <div>{props.config.port + ' ' + props.config.host}</div> | ||
ConfigDisplay.propTypes = { | ||
config: propTypes.shape({ | ||
host: propTypes.string, | ||
port: propTypes.number | ||
config: PropTypes.shape({ | ||
host: PropTypes.string, | ||
port: PropTypes.number | ||
}) | ||
@@ -188,9 +188,9 @@ } | ||
### propTypes.string | ||
### PropTypes.string | ||
Validate prop is a string | ||
***Note: all propTypes can be required by specifying `isRequired` like below:*** | ||
***Note: all PropTypes can be required by specifying `isRequired` like below:*** | ||
`propTypes.number.isRequired` | ||
`PropTypes.number.isRequired` | ||
@@ -197,0 +197,0 @@ ## Custom Validators |
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
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
1
0
13379
1
169
+ Addedchecker-factory@^1.0.0
+ Addedchecker-factory@1.0.0(transitive)