Package Exports
- rules
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rules) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
rules (node.js)
Snpmall declarative rules framework desing primarily for use whne validating incoming data, such as JSON coming into services.
Instead of trying handle all your validation this framework focussing on making it easy to declare data-level invariants, similiar to the sorts of rules you could put in a schema.
Samples
You create an object to declare the invariants you want to apply. A fluent interface makes it easy to specify the invariants for each property.
#####JavaScript
var personRules = {
name: mustBe().populated().string( { minLength: 5, maxLength: 20} ), [1]
weight: mustBe().populated().numeric({min : 0, max: 130}),
dateOfBirth: mustBe().date({ before: now.subtract("years", 1) })
}#####CoffeeScript
# This schema is not showing how to validate a real address, its just an example that makes it easy to test the framework
addressRules = {
streetOne: mustBe().populated()
streetTwo: -> @.populated().string( minLength: 10, maxLength : 50 ) [2]
streetThree: -> @.populated().string( minLength : 10, maxLength: 50)
town: -> @.populated()
postCode: -> @.populated().matchFor(/.../)
}As shown you can access this fluent interface using twp approaces:
- [1] mustBe() - Acts as the entry point to the fluent interface.
- [2] function - 'this' inside the function being the entry point to the fluent interface.
#####Triggering validation You trigger validation using:
result = rules.apply(person, personRules)The returned object has the per-property details of any validation failures, e.g.:
{ town:
[ { message: 'The value must be populated.',
type: 'not_populated',
value: undefined } ]
}
Examples
The project comes with examples in the examples directory:
node examples/person
coffee examples/addressNote that if you are using sublime you can get the alignment shown in the person example using the sublime text alignment package.
Validators
The framework comes with several validators, to understand them further you may want to run the examples.
populated- Checks the value is notnull,undefined,"", or an empty array.arraynumeric- Optionally you can also pass in object withminand/ormaxvaluesmatchFor- You can pass in an object withpatternand optionallyflags, alternatively you can pass in theRegExpobject to use.date- Optionally you can specify that the date must bebeforeand/orafterspecified dates. To make this easier you usenow.addornow.subtractto specify the dates to use forbefore/after.string- Optionally you can pass inminLengthand/ormaxLength.
Tests
First install mocha:
npm install mocha -gRun the tests:
mocha -R spec spec/ -w -G --recursive -bTODO
- More validators
- UMD support