simple-validate
v0.0.1
Published
Simple object validator.
Downloads
1
Readme
validate
Simple argument validator.
Install
$ npm install simple-validate
Run the specs
$ jasmine-node spec/
Usage
var validate = require('simple-validate');
var pattern = {
title : validate.required(isString)
description : validate.isAll(isString, hasLengthGreaterThan(5)),
isActive : isBoolean,
tags : isArray
};
validate(pattern, {
title : 'Hi There',
description : 'This is a great post.',
isActive : true
// tags are undefined - but that is ok, validator treats them as optional
});
// => 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.
validate(<pattern>, <args>) -> 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.required(p) -> Function
validate.required(p)(<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.optional(p) -> Function
validate.optional(p)(<value>) -> Boolean
validate.isAll
Predicates -> Predicate
Returns a predicate that is satisfied if all supplied predicates are satisfied for the provided value.
validate.isAll(p1, p2, ...) -> Function
validate.isAll(p1, p2, ...)(<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.isAny(p1, p2, ...) -> Function
validate.isAny(p1, p2, ...)(<value>) -> Boolean
validate.isNot
Predicate -> Predicate
Returns a predicate that inverts the supplied predicate.
validate.isNot(p) -> Function
validate.isNot(p)(<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