index.d.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Type definitions for z-schema v3.16.0
  2. // Project: https://github.com/zaggino/z-schema
  3. // Definitions by: pgonzal <https://github.com/pgonzal>
  4. // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
  5. declare namespace Validator {
  6. export interface Options {
  7. asyncTimeout?: number;
  8. forceAdditional?: boolean;
  9. assumeAdditional?: boolean;
  10. forceItems?: boolean;
  11. forceMinItems?: boolean;
  12. forceMaxItems?: boolean;
  13. forceMinLength?: boolean;
  14. forceMaxLength?: boolean;
  15. forceProperties?: boolean;
  16. ignoreUnresolvableReferences?: boolean;
  17. noExtraKeywords?: boolean;
  18. noTypeless?: boolean;
  19. noEmptyStrings?: boolean;
  20. noEmptyArrays?: boolean;
  21. strictUris?: boolean;
  22. strictMode?: boolean;
  23. reportPathAsArray?: boolean;
  24. breakOnFirstError?: boolean;
  25. pedanticCheck?: boolean;
  26. ignoreUnknownFormats?: boolean;
  27. customValidator?: (report: Report, schema: any, json: any) => void;
  28. }
  29. export interface SchemaError extends Error {
  30. /**
  31. * Implements the Error.name contract. The value is always "z-schema validation error".
  32. */
  33. name: string;
  34. /**
  35. * An identifier indicating the type of error.
  36. * Example: "JSON_OBJECT_VALIDATION_FAILED"
  37. */
  38. message: string;
  39. /**
  40. * Returns details for each error that occurred during validation.
  41. * See Options.breakOnFirstError.
  42. */
  43. details: SchemaErrorDetail[];
  44. }
  45. export interface SchemaErrorDetail {
  46. /**
  47. * Example: "Expected type string but found type array"
  48. */
  49. message: string;
  50. /**
  51. * An error identifier that can be used to format a custom error message.
  52. * Example: "INVALID_TYPE"
  53. */
  54. code: string;
  55. /**
  56. * Format parameters that can be used to format a custom error message.
  57. * Example: ["string","array"]
  58. */
  59. params: Array<string>;
  60. /**
  61. * A JSON path indicating the location of the error.
  62. * Example: "#/projects/1"
  63. */
  64. path: string;
  65. /**
  66. * The schema rule description, which is included for certain errors where
  67. * this information is useful (e.g. to describe a constraint).
  68. */
  69. description: string;
  70. /**
  71. * Returns details for sub-schemas that failed to match. For example, if the schema
  72. * uses the "oneOf" constraint to accept several alternative possibilities, each
  73. * alternative will have its own inner detail object explaining why it failed to match.
  74. */
  75. inner: SchemaErrorDetail[];
  76. }
  77. export const schemaSymbol: unique symbol
  78. export const jsonSymbol: unique symbol
  79. }
  80. declare class Validator {
  81. public lastReport: Report | undefined;
  82. /**
  83. * Register a custom format.
  84. *
  85. * @param name - name of the custom format
  86. * @param validatorFunction - custom format validator function.
  87. * Returns `true` if `value` matches the custom format.
  88. */
  89. public static registerFormat(formatName: string, validatorFunction: (value: any) => boolean): void;
  90. /**
  91. * Unregister a format.
  92. *
  93. * @param name - name of the custom format
  94. */
  95. public static unregisterFormat(name: string): void;
  96. /**
  97. * Get the list of all registered formats.
  98. *
  99. * Both the names of the burned-in formats and the custom format names are
  100. * returned by this function.
  101. *
  102. * @returns {string[]} the list of all registered format names.
  103. */
  104. public static getRegisteredFormats(): string[];
  105. public static getDefaultOptions(): Validator.Options;
  106. constructor(options: Validator.Options);
  107. /**
  108. * @param schema - JSON object representing schema
  109. * @returns {boolean} true if schema is valid.
  110. */
  111. validateSchema(schema: any): boolean;
  112. /**
  113. * @param json - either a JSON string or a parsed JSON object
  114. * @param schema - the JSON object representing the schema
  115. * @returns true if json matches schema
  116. */
  117. validate(json: any, schema: any): boolean;
  118. /**
  119. * @param json - either a JSON string or a parsed JSON object
  120. * @param schema - the JSON object representing the schema
  121. */
  122. validate(json: any, schema: any, callback: (err: any, valid: boolean) => void): void;
  123. /**
  124. * Returns an Error object for the most recent failed validation, or null if the validation was successful.
  125. */
  126. getLastError(): Validator.SchemaError;
  127. /**
  128. * Returns the error details for the most recent validation, or undefined if the validation was successful.
  129. * This is the same list as the SchemaError.details property.
  130. */
  131. getLastErrors(): Validator.SchemaErrorDetail[];
  132. }
  133. /**
  134. * Basic representation of the Report class -- just enough to support customValidator
  135. */
  136. declare class Report {
  137. errors: Validator.SchemaErrorDetail[];
  138. /**
  139. * Returns whether the validation did pass
  140. */
  141. isValid(): boolean;
  142. /**
  143. * @param errorCode - a string representing the code for the custom error, e.g. INVALID_VALUE_SET
  144. * @param errorMessage - string with the message to be returned in the error
  145. * @param params - an array of relevant params for the error, e.g. [fieldName, fieldValue]
  146. * @param subReports - sub-schema involved in the error
  147. * @param schemaDescription - description from the schema used in the validation
  148. * Adds custom error to the errors array in the validation instance and sets valid to false if it is not already set as false
  149. */
  150. addCustomError: (errorCode: string, errorMessage: string, params: string[], subReports: string, schemaDescription: string) => void;
  151. }
  152. export = Validator;