diff --git a/is-my-json-valid/is-my-json-valid-tests.ts b/is-my-json-valid/is-my-json-valid-tests.ts new file mode 100644 index 000000000..2a58f3da7 --- /dev/null +++ b/is-my-json-valid/is-my-json-valid-tests.ts @@ -0,0 +1,43 @@ +/// + +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 +}); diff --git a/is-my-json-valid/is-my-json-valid.d.ts b/is-my-json-valid/is-my-json-valid.d.ts new file mode 100644 index 000000000..e584b8c4b --- /dev/null +++ b/is-my-json-valid/is-my-json-valid.d.ts @@ -0,0 +1,30 @@ +// Type definitions for is-my-json-valid +// Project: https://github.com/mafintosh/is-my-json-valid +// Definitions by: kruncher +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "is-my-json-valid" { + + interface ValidationError { + field: string; + message: string; + value: any; + type: string; + } + + interface ValidateFunction { + (data: any): boolean; + + errors: ValidationError[]; + } + + interface Validator { + (schema: any, options?: any): ValidateFunction; + + filter(schema: any): any; + } + + var validator: Validator; + export = validator; + +}