deku-prop-types
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -7,2 +7,5 @@ 'use strict'; | ||
return { | ||
get name() { | ||
return name; | ||
}, | ||
get isRequired() { | ||
@@ -66,2 +69,13 @@ this.required = true; | ||
}, | ||
get instanceOf() { | ||
return function (constructor) { | ||
return checkerFactory('instanceOf', function (prop, key) { | ||
if (!(prop instanceof constructor)) { | ||
return new TypeError(key + ' is not an instance of `' + constructor.name + '`'); | ||
} | ||
return null; | ||
}); | ||
}; | ||
}, | ||
get number() { | ||
@@ -73,2 +87,17 @@ return checkerFactory('number', 'number'); | ||
}, | ||
get objectOf() { | ||
return function (validator) { | ||
return checkerFactory('objectOf', function (prop, key) { | ||
var anyErrors = Object.keys(prop).some(function (p) { | ||
return validator.validate(prop[p]) instanceof Error; | ||
}); | ||
if (anyErrors) { | ||
return new TypeError(key + ' does not consist of all properties with `' + validator.name + '` values'); | ||
} | ||
return null; | ||
}); | ||
}; | ||
}, | ||
get oneOf() { | ||
@@ -102,2 +131,17 @@ return function (allowedValues) { | ||
}, | ||
get shape() { | ||
return function (propsObj) { | ||
return checkerFactory('shape', function (prop, key) { | ||
var anyErrors = Object.keys(propsObj).some(function (validator) { | ||
return propsObj[validator].validate(prop[validator]) instanceof Error; | ||
}); | ||
if (anyErrors) { | ||
return new TypeError(key + ' does not have the correct value types'); | ||
} | ||
return null; | ||
}); | ||
}; | ||
}, | ||
get string() { | ||
@@ -126,2 +170,7 @@ return checkerFactory('string', 'string'); | ||
module.exports.validate = function (component) { | ||
/* eslint-disable no-process-env */ | ||
if (process.env.NODE_ENV === 'production') { | ||
return component; | ||
} | ||
if (typeof component === 'function') { | ||
@@ -128,0 +177,0 @@ return function (model) { |
{ | ||
"name": "deku-prop-types", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Prop type validation for Deku components", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -17,2 +17,4 @@ # deku-prop-types | ||
**Note: propType validations are not performed in production environments** | ||
`function-component.jsx` | ||
@@ -76,2 +78,21 @@ ```jsx | ||
Validate prop is a function | ||
### propTypes.instanceOf | ||
Validate prop is an instance of a function or class | ||
```jsx | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
const ItemList ({props}) => <div> | ||
{props.list.map(item => <div>{item}</div>)} | ||
</div> | ||
ItemList.propTypes = { | ||
list: propTypes.instanceOf(Array) | ||
} | ||
export default validate(ItemList) | ||
``` | ||
### propTypes.number | ||
@@ -82,2 +103,18 @@ Validate prop is a number | ||
### propTypes.objectOf | ||
Validate prop has keys all matching an allowed type | ||
```jsx | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
const NameCard = ({props}) => <div>{props.person.firstName + ' ' + props.person.lastName}</div> | ||
NameCard.propTypes = { | ||
person: propTypes.objectOf(propTypes.string) | ||
} | ||
export default validate(NameCard) | ||
``` | ||
### propTypes.oneOf | ||
@@ -115,2 +152,21 @@ Validate prop is one of the allowed values | ||
### propTypes.shape | ||
Validate an object's properties are of a certain type | ||
```jsx | ||
import {element} from 'deku' | ||
import {propTypes, validate} from 'deku-prop-types' | ||
const ConfigDisplay = ({props}) => <div>{props.config.port + ' ' + props.config.host}</div> | ||
ConfigDisplay.propTypes = { | ||
config: propTypes.shape({ | ||
host: propTypes.string, | ||
port: propTypes.number | ||
}) | ||
} | ||
export default validate(ConfigDisplay) | ||
``` | ||
### propTypes.string | ||
@@ -117,0 +173,0 @@ Validate prop is a string |
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
12389
171
206