ajv/.tonic_example.js

20 lines
439 B
JavaScript
Raw Permalink Normal View History

2015-11-29 01:39:50 +03:00
var Ajv = require('ajv');
2017-04-17 21:58:31 +03:00
var ajv = new Ajv({allErrors: true});
2015-11-29 01:39:50 +03:00
var schema = {
2015-12-14 02:24:48 +03:00
"properties": {
"foo": { "type": "string" },
"bar": { "type": "number", "maximum": 3 }
}
2015-11-29 01:39:50 +03:00
};
var validate = ajv.compile(schema);
test({"foo": "abc", "bar": 2});
test({"foo": 2, "bar": 4});
function test(data) {
2015-12-14 02:24:48 +03:00
var valid = validate(data);
if (valid) console.log('Valid!');
else console.log('Invalid: ' + ajv.errorsText(validate.errors));
2015-11-29 01:39:50 +03:00
}