checker-factory
v2.1.0
Published
Checker Factory used by Deku Prop Type Validators
Downloads
5
Maintainers
Readme
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')
// => TypeError: Expected age to be of type `number`, but got `string`
numberChecker.validate(3, 'age')
// => undefined
const requiredString = checkerFactory('string').isRequired
requiredString.validate(undefined, 'name')
// => Error: name is required
const evenNumberChecker = checkerFactory((prop, key) => {
if (prop % 2 === 1) {
return new Error(`Expected ${key} to be an even number`)
}
})
evenNumberChecker.validate(3, 'id')
// => Error: Expected id to be an even number
const checkerWithName = checkerFactory('number', 'dog')
checkerWithName.name
// => 'dog'
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