Add definitions for 'is-my-json-valid'.

This commit is contained in:
Lea Hayes
2016-03-16 13:58:05 +00:00
parent 2f47c75835
commit ab87babd69
2 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
/// <reference path="is-my-json-valid.d.ts"/>
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
});

30
is-my-json-valid/is-my-json-valid.d.ts vendored Normal file
View File

@@ -0,0 +1,30 @@
// Type definitions for is-my-json-valid
// Project: https://github.com/mafintosh/is-my-json-valid
// Definitions by: kruncher <https://github.com/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;
}