proteus-validator

1.0.2 • Public • Published

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();

Features

Definitions

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

Types

Value JSON Schema Draft v3 Proteus Validator Comments
string
number
integer
boolean
null
any
object
array
Union Types

String Formats

Value JSON Schema Draft v3 Proteus Validator Comments
date-time
date
time
utc-millisec
regex
color
style
phone any string is allowed
uri
email
ip-address
ipv6 any string is allowed
host-name

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i proteus-validator

    Weekly Downloads

    4

    Version

    1.0.2

    License

    none

    Last publish

    Collaborators

    • proteus
    • suguru