index.d.ts 3.9 KB

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