mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-03 03:44:29 +08: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
|
|
});
|