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.
Object.prototype.toString.callso make sure you use
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
Schema data will be put on the request object,
req.validated, as an
Object containing field : data information.
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.
.middlewareto be invoked when used instead of referenced.
requiredfields, may be subject to change. Let me know in issues how you feel.
String... are supported, check password.type in test for more information.
debugfeature for showing value of fields along with error messages.