UNPKG

3.71 kBTypeScriptView Raw
1/*
2This is type definition for typescript.
3This is for library users. Thus, properties and methods for internal use is omitted.
4 */
5export declare class Validator {
6 constructor();
7 customFormats: {[formatName: string]: CustomFormat};
8 schemas: {[id: string]: Schema};
9 unresolvedRefs: string[];
10
11 attributes: {[property: string]: CustomProperty};
12
13 addSchema(schema?: Schema, uri?: string): Schema|void;
14 validate(instance: any, schema: Schema, options?: Options, ctx?: SchemaContext): ValidatorResult;
15}
16
17export declare class ValidatorResult {
18 constructor(instance: any, schema: Schema, options: Options, ctx: SchemaContext)
19 instance: any;
20 schema: Schema;
21 propertyPath: string;
22 errors: ValidationError[];
23 throwError: boolean;
24 disableFormat: boolean;
25 valid: boolean;
26 addError(detail: string|ErrorDetail): ValidationError;
27 toString(): string;
28}
29
30export declare class ValidationError {
31 constructor(message?: string, instance?: any, schema?: Schema, propertyPath?: any, name?: string, argument?: any);
32 path: (string|number)[];
33 property: string;
34 message: string;
35 schema: string|Schema;
36 instance: any;
37 name: string;
38 argument: any;
39 toString(): string;
40 stack: string;
41}
42
43export declare class SchemaError extends Error{
44 constructor(msg: string, schema: Schema);
45 schema: Schema;
46 message: string;
47}
48
49export declare function validate(instance: any, schema: any, options?: Options): ValidatorResult
50
51export interface Schema {
52 $id?: string
53 id?: string
54 $schema?: string
55 $ref?: string
56 title?: string
57 description?: string
58 multipleOf?: number
59 maximum?: number
60 exclusiveMaximum?: number | boolean
61 minimum?: number
62 exclusiveMinimum?: number | boolean
63 maxLength?: number
64 minLength?: number
65 pattern?: string | RegExp
66 additionalItems?: boolean | Schema
67 items?: Schema | Schema[]
68 maxItems?: number
69 minItems?: number
70 uniqueItems?: boolean
71 maxProperties?: number
72 minProperties?: number
73 required?: string[] | boolean
74 additionalProperties?: boolean | Schema
75 definitions?: {
76 [name: string]: Schema
77 }
78 properties?: {
79 [name: string]: Schema
80 }
81 patternProperties?: {
82 [name: string]: Schema
83 }
84 dependencies?: {
85 [name: string]: Schema | string[]
86 }
87 const?: any
88 'enum'?: any[]
89 type?: string | string[]
90 format?: string
91 allOf?: Schema[]
92 anyOf?: Schema[]
93 oneOf?: Schema[]
94 not?: Schema
95 if?: Schema
96 then?: Schema
97 else?: Schema
98}
99
100export interface Options {
101 skipAttributes?: string[];
102 allowUnknownAttributes?: boolean;
103 preValidateProperty?: PreValidatePropertyFunction;
104 rewrite?: RewriteFunction;
105 base?: string;
106 throwError?: boolean;
107 required?: boolean;
108 throwFirst?: boolean;
109 throwAll?: boolean;
110 nestedErrors?: boolean;
111}
112
113export interface RewriteFunction {
114 (instance: any, schema: Schema, options: Options, ctx: SchemaContext): any;
115}
116
117export interface PreValidatePropertyFunction {
118 (instance: any, key: string, schema: Schema, options: Options, ctx: SchemaContext): any;
119}
120
121export interface SchemaContext {
122 schema: Schema;
123 options: Options;
124 propertyPath: string;
125 base: string;
126 schemas: {[base: string]: Schema};
127 makeChild: (schema: Schema, key: string) => SchemaContext;
128}
129
130export interface CustomFormat {
131 (input: any): boolean;
132}
133
134export interface CustomProperty {
135 (instance: any, schema: Schema, options: Options, ctx: SchemaContext): string|ValidatorResult;
136}
137
138export interface ErrorDetail {
139 message: string;
140 name: string;
141 argument: string;
142}