Checker Factory
Checker Factory used by Deku Prop Type Validators
Install
npm install --save checker-factory
Usage
import checkerFactory from 'checker-factory'
const numberChecker = checkerFactory('number')
numberChecker.validate('hello', 'age')
numberChecker.validate(3, 'age')
const requiredString = checkerFactory('string').isRequired
requiredString.validate(undefined, 'name')
const evenNumberChecker = checkerFactory((prop, key) => {
if (prop % 2 === 1) {
return new Error(`Expected ${key} to be an even number`)
}
})
evenNumberChecker.validate(3, 'id')
API
checkerFactory(name, validator)
Returns a CheckerFactory with an isRequired
getter and validate
function.
name
type: string
The name of the CheckerFactory. This name is how users will reference the checker.
For example, a name of string will have users using propTypes.string.
validator
type: function
| string
If validator
is a string
, then a typeof
check will be performed. If validator
is a function
, then the function will be execution. The function is passed the prop
value and the key
name.
License
MIT © Dustin Specker