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')
const checkerWithName = checkerFactory('number', 'dog')
checkerWithName.name
API
checkerFactory(validator[, name])
Returns a CheckerFactory with an isRequired
getter and validate
function.
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.
name
type: string
This is an optional value to retrieve a name for the checker factory.
License
MIT © Dustin Specker