policeman
Lightweight yet powerful schema validator
- Validate objects based on provided schema
- Inspired by mappet
npm)
Installation (npm i -S policeman
Examples
; // setup entry validatorsconst requiredValidator = ;const emailValidator = ;const phoneNumberValidator = ; // setup entry filter predicatesconst isGift = sourcegift === true; // define schemaconst schema = // 1. array of validators - multiple errors // 2. combine validators - first of many errors // 3. single validator - single error // 4. skip validation based on filter predicate "email" "email" requiredValidator emailValidator // #1 "phone" "phone" // #2 "name" "name" requiredValidator // #3 "giftCode" "giftCode" requiredValidator isGift // #4 // [dest, source, Validator, Filter]; // create validatorconst validator = ; // validate; // {// valid: false,// errors: {// email: ["is invalid email"],// phone: "is invalid phone",// name: "is required"// }// }
See tests for more examples.
Built-in validators
All built-in validators are curried.
isRequired(() => message, value)
Validates presence. Fails on null
, empty string or undefined
.
isMinLength(min, () => message, value)
Passed value
must be a string longer or with length equal to min
.
isMaxLength(max, () => message, value)
Passed value
must be a string shorther or with length equal to max
.
isEqualLength(equal, () => message, value)
Passed value
must be a string shorther or with length equal to max
.
isEmail(() => message, value)
Passed value
must be a valid email. It's a simple check, if you need more complex solution use
isMatching
or isPassing
.
isMatching(regexp, () => message, value)
Passed value
must pass regexp
.
isPassing(predicate, () => message, value)
Passed predicate
answers on "Is value
valid?". When predicate
returns true
validator passes,
when predicate
returns false
error message is returned.
It makes policeman
compatible with all available validators i.e. validator.
;; const creditCardValidator = ;const uuid4Validator = ;const ftpValidator =
See tests for more examples.