Package Exports
- z-schema
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 (z-schema) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
z-schema validator
JSON Schema validator for Node.js (draft4 version)
Coded according to:
json-schema documentation, json-schema-core, json-schema-validation, json-schema-hypermedia
Passing all tests here (even optional, except zeroTerminatedFloats):
json-schema/JSON-Schema-Test-Suite
Will try to maintain this as much as possible, all bug reports welcome.
Basic Usage
var report = zSchema.validate(json, schema, function(report) {
if (report.valid === true) ...
});
If report.valid === false
, then errors can be found in report.errors
.
The report object will look something like:
{
"valid": false,
"errors": [
]
}
Advanced (Server) Usage
You can pre-compile schemas (for example on your server startup) so your application is not bothered by schema compilation and validation when validating ingoing / outgoing objects.
var validator = new zSchema();
validator.compileSchema(schema, function (err, compiledSchema) {
assert.isUndefined(err);
...
});
Then you can re-use compiled schemas easily with sync-async validation API.
var report = validator.validateWithCompiled(json, compiledSchema);
assert.isTrue(report.valid);
...
validator.validateWithCompiled(json, compiledSchema, function(err, success, report) {
assert.isTrue(success);
...
});
Note:
Most basic schemas don't have to be compiled for validation to work (although recommended). Async compilation was mostly created to work with schemas that contain references to other files.
Customization
You can add validation for your own custom string formats like this: (these are added to all validator instances, because it would never make sense to have multiple functions to validate format with the same name)
zSchema.registerFormat('xstring', function (str) {
return str === 'xxx';
});
zSchema.validate('xxx', {
'type': 'string',
'format': 'xstring'
}, function (report) {
// report.valid will be true
}
Strict validation
When creating new instance of validator, you can specify some options that will alter the validator behaviour like this:
var validator = new zSchema({
option: true
});
- noExtraKeywords:
true/false
when true, do not allow unknown keywords in schema
- noZeroLengthStrings:
true/false
when true, always adds minLength: 1 to schemas where type is string
- noTypeless:
true/false
when true, every schema must specify a type
- forceAdditional:
true/false
when true, forces not to leave out some keys on schemas (additionalProperties, additionalItems)
- forceProperties:
true/false
when true, forces not to leave out properties or patternProperties on type-object schemas
- forceItems:
true/false
when true, forces not to leave out items on array-type schemas
- forceMaxLength:
true/false
when true, forces not to leave out maxLength on string-type schemas, when format or enum is not specified
Alternatively, you can turn on all of the above options with:
var validator = new zSchema({
strict: true
});