Skip to content

Component that can fetch JSON schemas and validate a JSON against it

License

Notifications You must be signed in to change notification settings

Magomogo/json-schema-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-schema-validator

Build Status

This package uses the tv4 JSON Schema validator, and the formats provided by the tv4-formats. It loads all the referenced JSON schemas over the internet to bootstrap tv4.

Usage

Validator.simple('http://json-schema.org/geo', function (error, v) {
    assert.ifError(error);

    assert(v.validate(
        {latitude: 53.0, longitude: 43.0},
        'http://json-schema.org/geo'
    ).valid);

    done();
});

CLI

echo '{"json": "to validate"}' | json-validate http://some.type.id/

The schema can also be a path to a local file. For example:

echo '{"json": "to validate"}' | json-validate ./node_modules/some-package/some-schema.json

API

Constructor:

v = new Validator(schemaUris);

schemaUris: array of schema Uris to load (can be a string in the case of single URI)


v.fetchSchemas(schemaLoader, callback)

Load schemas over the net with schemaLoader(url, callback) and add to tv4 validator. All URI-s to be loaded: given in constructor and referenced by "$ref" clause in each loaded schema. Circular references get resolved.


v.validate(json, typeId)

Do validation of json against schema defined by typeId. If you only define one schema when creating the validator, you can leave typeId off of the signature. That schema will be selected by default.

Possible type Ids are:

validate() returns the tv4 validation result object. It will throw an Error if the schema for the passed typeId has not been loaded (fetched).


Validator.simple(uris, callback)

Just a shortcut for getting validator bootstrapped using request as schema loader

About

Component that can fetch JSON schemas and validate a JSON against it

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •