joi-assert
Assert values using Joi schemas
Use Spumko's Joi in assertion statements that validate and sanitize values.
Assertions throw an AssertionError with a compact, readable message if validation fails. This makes Joi schemas usable in assertions for use with frameworks like mocha.
If validation succeeds the sanitized value returned, via Joi's support for default values and unknown property stripping etc. This makes Joi schemas usable as input-assertions in the methods of user facing methods of your modules and APIs.
⚠️ Early phase so users beware.
Usage
Get it from npm:
$ npm install joi$ npm install joi-assert
Basic assertion
// import plain Joivar Joi = ; // import the modulevar joiAssert = ; // get a Joi schemavar schema = Joi; // validate data and throw AssertionError on failure; // assertion returns valid data as onelinervar valid = ;
Error message
// add schema description to error messagevar schema = Joidescription'lower bound'; // additonal message per callinput = ;
Validate and sanitize input
// get a schema using default(), .stripUnknown etcvar schema = Joiobject foo: Joi bar: Joiobject stripUnknown: true; // get valid but dirty input datavar raw = foo: 'abc' nope: null// pass through assertionvar data = ; // data is now clean foo: 'abc' bar: 'hoge'
Bake assertion function
// get a schemavar schema = Joi; // get assertion closurevar fiveTen = joiAssert; // nice;; // cleaninput = ; // kablam!; // get fancyvar clean = 5 6 7 8;
Todo
- Improve vars argument
- Support argument as Object, Array and String
- Consider sprintf style smart args (order etc)
- Test it
- Add options to customise error message:
- Amount of concatenated reports
- Multiline message
Build
Install development dependencies:
$ npm install$ npm install -g grunt-cli
Build and run tests using grunt:
$ grunt test
See the Gruntfile.js
for additional commands.
Contributions
Pull requests with fixes are very welcome, for new features best ask via a issue first.
License
Copyright (c) 2014 Bart van der Schoor
Licensed under the MIT license.