DefinitelyTyped/types/is-my-json-valid/is-my-json-valid-tests.ts
2017-03-24 14:27:52 -07:00

41 lines
810 B
TypeScript

import validator = require('is-my-json-valid');
//
// Usage
//
let jsonSchema = {
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
};
let validate = validator(jsonSchema);
validate = validator(jsonSchema, { verbose: true });
let result = validate({ hello: 'world' });
console.assert(validate({ hello: 'world' }) === true, "is valid");
console.log(validate.errors);
console.log(validate.errors[0].field);
console.log(validate.errors[0].message);
console.log(validate.errors[0].value);
console.log(validate.errors[0].type);
//
// Filtering away additional properties
//
let filter = validator.filter({
required: true,
type: 'object',
properties: {
hello: {type: 'string', required: true}
},
additionalProperties: false
});