proteus-validator
v1.0.2
Published
proteus-validator ==============================
Downloads
10
Readme
proteus-validator
Proteus Validator is JSON Schema Validator which provides an interface for validating JSON objects against JSON Schema Draft 3. It runs both in a browser and on Node.js.
Usage
Prepare
Node.js
var validator = require('proteus-validator');
browser
<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript" src="https://github.com/caolan/async/tree/master/lib/async.js"></script>
<script type="text/javascript" src="proteus-validator/lib/validator.js"></script>
validate schema
var schema = { type: 'integer' };
// synchronous call
var errors = validator.validateSchema(schema);
// asynchronous call
validator.validateSchema(schema, function(errors) {
...
});
validate json objects
var schema = { type: 'integer' };
var instance = 1;
// synchronous call
var errors = validator.validate(schema, instance);
// asynchronous call
validator.validate(schema, instance, function(errors) {
//...
});
This also runs schema validation. If you are going to validate many times by the same schema, it is recommended to register schema by registSchema method.
regist schema
var schema = { type: 'integer' };
var errors = validator.registSchema('schema1', schema);
// to use registered schema, send registered schema name instead of schema itself.
var errors = validator.validate('schema1', instance);
// you can also unregist schema
validator.unregistSchema('schema1');
others
addValidation
You can create your own validation.
validator.addValidation('customValidation', function(schema, instance) {
console.log(schema.customValidation); // someschema
console.log(instance); // somevalue
});
validator.validate({
type: 'object',
properties: {
prop: { type: 'string', customValidation: 'someschema' }
}
},
{ prop: 'somevalue' },
function(errors) {
//...
});
createValidator
You can create new validator.
var newValidator = validator.createValidator();