Package Exports
- djv
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 (djv) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
djv
Dynamic JSON Schema Validator
Current package supports JSON Schema v4 and it contains utils for validating objects against schemas. This is a part of djv packages aimed to work with json-schema.
- djv validate object against schemas
- djvi instantiate objects by schema definition
- jvu utilities for declarative, FP usage
Installation
npm install djvor
<script src="djv.js"></script>There are 2 versions of validator
./lib/djv.jsa default one, not uglified and not transpiled./djv.jsa built one with a webpack, babel and uglify (preferrable for frontend)
Usage
var env = new djv();
var jsonSchema = {
"common": {
"properties": {
"type": {
"enum": ["common"]
}
},
"required": [
"type"
]
}
};
// Use `addSchema` to add json schema
env.addSchema('test', jsonSchema);
env.validate('test#/common', { type: 'common' });
// => undefined
env.validate('test#/common', { type: 'custom' });
// => 'required: data'API
addSchema(name: string, schema: object?) -> resolved: object
Add a schema to a current djv environment,
env.addSchema('test', jsonSchema);
/* => {
fn: function f0(data){...}
name: 'test'
schema: ...
} */validate(name: string, object: object) -> error: string
Check if object is valid against the schema
env.validate('test#/common', { type: 'common' });
// => undefined
env.validate('test#/common', { type: 'custom' });
// => 'required: data'where
- name - schema path in current environment
- object - object to validate
- error - undefined if it is valid
removeSchema(name: string)
Remove a schema or the whole structure from the djv environment
env.removeSchema('test');resolve(name: string?)
Resolve the name by existing environment
env.resolve('test');
// => { name: 'test', schema: {} }, fn: ... }export(name: string?) -> state: object
Export the whole structure object from environment or resolved by a given name
env.export();
// => { test: { name: 'test', schema: {}, ... } }where state is an internal structure or only resolved schema object
import(config: object)
Import all found structure objects to internal environment structure
env.import(config);addFormat(name: string, formatter: string/function)
Add formatter to djv environment. When a string is passed it is interpreted as an expression which when returns true goes with an error, when returns false then a property is valid. When a function is passed it will be executed during schema compilation with a current schema and template helper arguments.
env.addFormat('UpperCase', '%s !== %s.toUpperCase()');
// or
env.addFormat('isOk', function(schema, tpl){
return `!${schema.isOk}`;
});
env.validate('ok', 'valid') // => undefined if schema contains isOk propertysetErrorHandler(errorHandler: function)
Specify custom error handler which will be used in generated functions when problem found.
The function should return a string expression, which will be executed when generated validator function is executed. The simpliest use case is the default one @see template/defaultErrorHandler
function defaultErrorHandler(errorType) {
return `return "${errorType}: ${tpl.data}";`;
}It returns an expression 'return ...', so the output is an error string.
djv({ errorHandler: () => 'return { error: true };' }) // => returns an object
djv({
errorHandler: function customErrorHandler(errorType, property) {
return `errors.push({
type: '${type}',
schema: '${this.schema[this.schema.length - 1]}',
data: '${this.data[this.data.length - 1]}'
});
}
})`;When a custom error handler is used, the template body function adds a error variable inside a generated validator, which can be used to put error information. errorType is always passed to error handler function. Some validate utilities put extra argument, like f.e. currently processed property value. Inside the handler context is a templater instance, which contains this.schema, this.data paths arrays to identify validator position.
@see test/index/setErrorHandler for more examples
Tests
npm test