UNPKG

3.95 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 ValidatorResultError extends Error {
31 instance: any;
32 schema: Schema;
33 options: Options;
34 errors: ValidationError;
35}
36
37export declare class ValidationError {
38 constructor(message?: string, instance?: any, schema?: Schema, propertyPath?: any, name?: string, argument?: any);
39 path: (string|number)[];
40 property: string;
41 message: string;
42 schema: string|Schema;
43 instance: any;
44 name: string;
45 argument: any;
46 toString(): string;
47 stack: string;
48}
49
50export declare class SchemaError extends Error{
51 constructor(msg: string, schema: Schema);
52 schema: Schema;
53 message: string;
54}
55
56export declare function validate(instance: any, schema: any, options?: Options): ValidatorResult
57
58export interface Schema {
59 $id?: string
60 id?: string
61 $schema?: string
62 $ref?: string
63 title?: string
64 description?: string
65 multipleOf?: number
66 maximum?: number
67 exclusiveMaximum?: number | boolean
68 minimum?: number
69 exclusiveMinimum?: number | boolean
70 maxLength?: number
71 minLength?: number
72 pattern?: string | RegExp
73 additionalItems?: boolean | Schema
74 items?: Schema | Schema[]
75 contains?: Schema
76 maxItems?: number
77 minItems?: number
78 uniqueItems?: boolean
79 maxProperties?: number
80 minProperties?: number
81 required?: string[] | boolean
82 propertyNames?: boolean | Schema
83 additionalProperties?: boolean | Schema
84 definitions?: {
85 [name: string]: Schema
86 }
87 properties?: {
88 [name: string]: Schema
89 }
90 patternProperties?: {
91 [name: string]: Schema
92 }
93 dependencies?: {
94 [name: string]: Schema | string[]
95 }
96 const?: any
97 'enum'?: any[]
98 type?: string | string[]
99 format?: string
100 allOf?: Schema[]
101 anyOf?: Schema[]
102 oneOf?: Schema[]
103 not?: Schema
104 if?: Schema
105 then?: Schema
106 else?: Schema
107 default?: any
108 examples?: any[]
109}
110
111export interface Options {
112 skipAttributes?: string[];
113 allowUnknownAttributes?: boolean;
114 preValidateProperty?: PreValidatePropertyFunction;
115 rewrite?: RewriteFunction;
116 base?: string;
117 throwError?: boolean;
118 required?: boolean;
119 throwFirst?: boolean;
120 throwAll?: boolean;
121 nestedErrors?: boolean;
122}
123
124export interface RewriteFunction {
125 (instance: any, schema: Schema, options: Options, ctx: SchemaContext): any;
126}
127
128export interface PreValidatePropertyFunction {
129 (instance: any, key: string, schema: Schema, options: Options, ctx: SchemaContext): any;
130}
131
132export interface SchemaContext {
133 schema: Schema;
134 options: Options;
135 propertyPath: string;
136 base: string;
137 schemas: {[base: string]: Schema};
138 makeChild: (schema: Schema, key: string) => SchemaContext;
139}
140
141export interface CustomFormat {
142 (input: any): boolean;
143}
144
145export interface CustomProperty {
146 (instance: any, schema: Schema, options: Options, ctx: SchemaContext): string|ValidatorResult;
147}
148
149export interface ErrorDetail {
150 message: string;
151 name: string;
152 argument: string;
153}