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.
var validator = require('proteus-validator');
<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>
var schema = { type: 'integer' };
// synchronous call
var errors = validator.validateSchema(schema);
// asynchronous call
validator.validateSchema(schema, function(errors) {
...
});
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.
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');
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) {
//...
});
You can create new validator.
var newValidator = validator.createValidator();
Value | JSON Schema Draft v3 | Proteus Validator | Comments |
---|---|---|---|
type | ✔ | ✔ | |
properties | ✔ | ✔ | |
patternProperties | ✔ | ||
additionalProperties | ✔ | ✔ | |
items | ✔ | ✔ | |
additionalItems | ✔ | ||
required | ✔ | ✔ | |
dependencies | ✔ | ||
minimum | ✔ | ✔ | |
maximum | ✔ | ✔ | |
exclusiveMinimum | ✔ | ✔ | |
exclusiveMaximum | ✔ | ✔ | |
minItems | ✔ | ✔ | |
maxItems | ✔ | ✔ | |
uniqueItems | ✔ | ||
pattern | ✔ | ✔ | |
minLength | ✔ | ✔ | |
maxLength | ✔ | ✔ | |
enum | ✔ | ✔ | |
default | ✔ | ||
title | ✔ | ✔ | |
description | ✔ | ✔ | |
format | ✔ | ✔ | |
divisibleBy | ✔ | ||
disallow | ✔ | ||
extends | ✔ | ||
id | ✔ | ||
$ref | ✔ | ||
$schema | ✔ |
Value | JSON Schema Draft v3 | Proteus Validator | Comments |
---|---|---|---|
string | ✔ | ✔ | |
number | ✔ | ✔ | |
integer | ✔ | ✔ | |
boolean | ✔ | ✔ | |
null | ✔ | ✔ | |
any | ✔ | ✔ | |
object | ✔ | ✔ | |
array | ✔ | ✔ | |
Union Types | ✔ |
Value | JSON Schema Draft v3 | Proteus Validator | Comments |
---|---|---|---|
date-time | ✔ | ✔ | |
date | ✔ | ✔ | |
time | ✔ | ✔ | |
utc-millisec | ✔ | ✔ | |
regex | ✔ | ✔ | |
color | ✔ | ✔ | |
style | ✔ | ✔ | |
phone | ✔ | any string is allowed | |
uri | ✔ | ✔ | |
✔ | ✔ | ||
ip-address | ✔ | ✔ | |
ipv6 | ✔ | any string is allowed | |
host-name | ✔ | ✔ |