mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
41 lines
810 B
TypeScript
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
|
|
});
|