index.d.ts
1 /* 2 This is type definition for typescript. 3 This is for library users. Thus, properties and methods for internal use is omitted. 4 */ 5 export 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 17 export 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 30 export 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 43 export declare class SchemaError extends Error{ 44 constructor(msg: string, schema: Schema); 45 schema: Schema; 46 message: string; 47 } 48 49 export declare function validate(instance: any, schema: any, options?: Options): ValidatorResult 50 51 export 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 100 export interface Options { 101 skipAttributes?: string[]; 102 allowUnknownAttributes?: boolean; 103 preValidateProperty?: PreValidatePropertyFunction; 104 rewrite?: RewriteFunction; 105 base?: string; 106 throwError?: boolean; 107 throwFirst?: boolean; 108 throwAll?: boolean; 109 nestedErrors?: boolean; 110 } 111 112 export interface RewriteFunction { 113 (instance: any, schema: Schema, options: Options, ctx: SchemaContext): any; 114 } 115 116 export interface PreValidatePropertyFunction { 117 (instance: any, key: string, schema: Schema, options: Options, ctx: SchemaContext): any; 118 } 119 120 export interface SchemaContext { 121 schema: Schema; 122 options: Options; 123 propertyPath: string; 124 base: string; 125 schemas: {[base: string]: Schema}; 126 } 127 128 export interface CustomFormat { 129 (input: any): boolean; 130 } 131 132 export interface CustomProperty { 133 (instance: any, schema: Schema, options: Options, ctx: SchemaContext): string|ValidatorResult; 134 } 135 136 export interface ErrorDetail { 137 message: string; 138 name: string; 139 argument: string; 140 }