1.0.2 • Published 11 years ago

proteus-validator v1.0.2

Weekly downloads
17
License
-
Repository
github
Last release
11 years ago

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

Types

String Formats

1.0.2

11 years ago

1.0.1

11 years ago

1.0.0

11 years ago

0.1.0

11 years ago