validate
Simple argument validator.
Install
$ npm install simple-validate
Run the specs
$ jasmine-node spec/
Usage
var validate = ; var pattern = title : validate description : validate isActive : isBoolean tags : isArray; ;// => true
The validator runs each argument against the defined validation pattern, asserting a true outcome for each. Properties defined in the validation pattern are assumed to be optional unless declared otherwise.
Note, this module is best used with a functional library to provide predicates (isString
, isNull
, etc.), such as lodash
or ramda
.
A more advanced example can also be found in the examples directory.
Available Methods
validate
Object -> Object -> Boolean
Validates arguments against the provided pattern.
-> Boolean
Logical Utilities
Note: all logical utilities must be called incrementally (fn(v1)(v2)
) as shown in the examples below.
validate.required
Predicate -> Predicate
Returns a predicate that is satisfied if the supplied predicate is satisfied and the provided value is not undefined. This should be used to denote that a property is required, since otherwise properties as assumed to be optional.
validate -> Functionvalidate<value> -> Boolean
validate.optional
Predicate -> Predicate
Returns a predicate that is satisfied if the supplied predicate is satisfied or the provided value is undefined. Note: using this utility is probably not necessary to use often, since validate
assumes all properties are optional by default. This is the shorthand equivalent to isAny(isUndefined, p)
.
validate -> Functionvalidate<value> -> Boolean
validate.isAll
Predicates -> Predicate
Returns a predicate that is satisfied if all supplied predicates are satisfied for the provided value.
validate -> Functionvalidate<value> -> Boolean
validate.isAny
Predicates -> Predicate
Returns a predicate that is satisfied if any of the supplied predicates are satisfied for the provided value.
validate -> Functionvalidate<value> -> Boolean
validate.isNot
Predicate -> Predicate
Returns a predicate that inverts the supplied predicate.
validate -> Functionvalidate<value> -> Boolean
TODO
- Add custom error handling
validate.withErrors(<pattern>, <values>)
. Consider addinggetErrors
, etc. Will probably include helper method to map pattern values for general usage. - Document new error handling methods.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request