mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-24 05:06:02 +08:00
Add definitions for 'is-my-json-valid'.
This commit is contained in:
43
is-my-json-valid/is-my-json-valid-tests.ts
Normal file
43
is-my-json-valid/is-my-json-valid-tests.ts
Normal 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
30
is-my-json-valid/is-my-json-valid.d.ts
vendored
Normal 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;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user