chopin-validator
v1.0.7
Published
chopin-validator is used to validate and sanitize objects by properties, values and values types. <br> There are two different ways to use the chopin-validator: * ModelRuleSet - a rule that validates objects and the relationships of its properties as a
Downloads
12
Readme
AppValidator
Motivation
AppValidator is used to validate and sanitize objects by properties, values and values types. There are two different ways to use the AppValidator:
- ModelRuleSet - a rule that validates objects and the relationships of its properties as a package
- RuleSetFactory - a factory that creates rules to validate single values
Code wise, both ModelRuleSet & RuleSetFactory (mainly) create and use PropRuleSet under the hood.
ModelRuleSet({strict, validations})
- strict (default:false) - if true, when validating an object that has unmentioned properties by the rule set, the validation will fail.
- validations (default: require('./validations')) - available validations for the model properties. can be overridden with an external library.
API
validate(object={}) - returns an array of validation errors for the given object. errors.length === 0 means valid object
isValid(object={}) - runs validate(object) under the hood and returns errors.length === 0
sanitize(object={}, allowProps=[]) - return a version of the object sanitized according to the ModelRuleSet. optional allowProps arg is used to bypass sanitization for specific properties.
add(prop='path.to.prop', ruleInitializer = PropRuleSet => ({})) - adds a rule to the ModelRuleSet. prop describes the expected key on the target object. ruleInitializer describes the value this key should hold.