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.
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();
});
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
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:
- http://json-schema.org/geo
- http://some.site.somewhere/entry-schema#/definitions/diskUUID
- file://./node_modules/some-package/some-schema.json
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