Barebones schema validation library for things such as database schemas, api data schemas, etc.
$ npm install schema-validator
For browsers, download and include the script
validator.js just as you would jquery or another script.
- Required (built-in)
- Default (built-in) - is done during required check, and is placed even if required exists.
- This functionality is subject to change (placed even if required part). Let me know in issues.
- Type - uses
Object.prototype.toString.callso make sure you use
- Length - By itself it must be this long, otherwise as an object supports the following:
- Test (Regular expression tests)
- Supports an array of RegExps as well.
You create a JSON Schema, where
username is a field, and each key:value inside of it is an implementation in validator.
var schema =username:type: Stringrequired: truelength:min: 3max: 36test: /^[a-z0-9]+$/gi;
new Validator against your schema:
var validator = schema;
Note there is also debugging support you can enable by adding the following line:
validatordebug = true;
Now we validate against some given information:
var check = validator;console;
Nesting is supported, it's currently in a testing phase, as seen in the test file:
belt:type: Objectrequired: trueteam:type: Arrayrequired: truelength:min: 1max: 6inventory:type: Arraydefault:length:max: 255
Express Middleware Style:
Schema data will be put on the request object,
req.validated, as an
Object containing field : data information.
Creating an extension
Implementing a feature into Validator is easy, you set the field and a callback.
The callback supports a single argument
options which contains valuable information.
field- The field implementation that is being checked.
key- The schema field being checked.
data- The data passed from an external source.
value- The field implementation data value.
error- Sugar method for
this.errorwhich was previously used.
type- optional argument, it's the error field for the message given. Default is
message- Error message.
After your implementation has been ran, the validator will check for errors, if found it will exit out and return the errors. You can pass along multiple errors per run, for an example check the test implementation.
Make extensibility easier. Move over to github repository. Make implementations use an object as an argument rather than multiple arguments. Implement nesting feature. Might be useful, I personally can't see one... let me know in issues if you can.
- Implement check for Array values.
- Implement support for wildcard, it could be useful to work with all fields that aren't plugins under one object.
- Break up implementations into their own folder and make a compiler.
- Clean code
- Optimize middleware to rely on
- Fix issue #6
- Fix issue #4
.middlewareto be invoked when used instead of referenced.
- I think the current implementation is fairly quick, but I feel there is a faster way using references.
- Supported even on
requiredfields, may be subject to change. Let me know in issues how you feel.
- Supported even on
- Fix test, now can be run with node
- Fix length for Numbers with extended support for Arrays
- Fix loop issue where only one value was returned
- Implement support for natives no longer need to use strings for types
String... are supported, check password.type in test for more information.
debugfeature for showing value of fields along with error messages.
- Simplified roundup.
- Extend comments
- Implement module exporting for various platforms.
- Remove console logging
- Implement plugin system -
- Fix slight bugs in matching validation check
- Make errors Object based for quick key referencing
- Initial Release.