errors.d.cts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import type { $ZodCheck, $ZodStringFormats } from "./checks.cjs";
  2. import { $constructor } from "./core.cjs";
  3. import type { $ZodType } from "./schemas.cjs";
  4. import type { StandardSchemaV1 } from "./standard-schema.cjs";
  5. import * as util from "./util.cjs";
  6. export interface $ZodIssueBase {
  7. readonly code?: string;
  8. readonly input?: unknown;
  9. readonly path: PropertyKey[];
  10. readonly message: string;
  11. }
  12. export type $ZodInvalidTypeExpected = "string" | "number" | "int" | "boolean" | "bigint" | "symbol" | "undefined" | "null" | "never" | "void" | "date" | "array" | "object" | "tuple" | "record" | "map" | "set" | "file" | "nonoptional" | "nan" | "function" | (string & {});
  13. export interface $ZodIssueInvalidType<Input = unknown> extends $ZodIssueBase {
  14. readonly code: "invalid_type";
  15. readonly expected: $ZodInvalidTypeExpected;
  16. readonly input?: Input;
  17. }
  18. export interface $ZodIssueTooBig<Input = unknown> extends $ZodIssueBase {
  19. readonly code: "too_big";
  20. readonly origin: "number" | "int" | "bigint" | "date" | "string" | "array" | "set" | "file" | (string & {});
  21. readonly maximum: number | bigint;
  22. readonly inclusive?: boolean;
  23. readonly exact?: boolean;
  24. readonly input?: Input;
  25. }
  26. export interface $ZodIssueTooSmall<Input = unknown> extends $ZodIssueBase {
  27. readonly code: "too_small";
  28. readonly origin: "number" | "int" | "bigint" | "date" | "string" | "array" | "set" | "file" | (string & {});
  29. readonly minimum: number | bigint;
  30. /** True if the allowable range includes the minimum */
  31. readonly inclusive?: boolean;
  32. /** True if the allowed value is fixed (e.g.` z.length(5)`), not a range (`z.minLength(5)`) */
  33. readonly exact?: boolean;
  34. readonly input?: Input;
  35. }
  36. export interface $ZodIssueInvalidStringFormat extends $ZodIssueBase {
  37. readonly code: "invalid_format";
  38. readonly format: $ZodStringFormats | (string & {});
  39. readonly pattern?: string;
  40. readonly input?: string;
  41. }
  42. export interface $ZodIssueNotMultipleOf<Input extends number | bigint = number | bigint> extends $ZodIssueBase {
  43. readonly code: "not_multiple_of";
  44. readonly divisor: number;
  45. readonly input?: Input;
  46. }
  47. export interface $ZodIssueUnrecognizedKeys extends $ZodIssueBase {
  48. readonly code: "unrecognized_keys";
  49. readonly keys: string[];
  50. readonly input?: Record<string, unknown>;
  51. }
  52. interface $ZodIssueInvalidUnionNoMatch extends $ZodIssueBase {
  53. readonly code: "invalid_union";
  54. readonly errors: $ZodIssue[][];
  55. readonly input?: unknown;
  56. readonly discriminator?: string | undefined;
  57. readonly inclusive?: true;
  58. }
  59. interface $ZodIssueInvalidUnionMultipleMatch extends $ZodIssueBase {
  60. readonly code: "invalid_union";
  61. readonly errors: [];
  62. readonly input?: unknown;
  63. readonly discriminator?: string | undefined;
  64. readonly inclusive: false;
  65. }
  66. export type $ZodIssueInvalidUnion = $ZodIssueInvalidUnionNoMatch | $ZodIssueInvalidUnionMultipleMatch;
  67. export interface $ZodIssueInvalidKey<Input = unknown> extends $ZodIssueBase {
  68. readonly code: "invalid_key";
  69. readonly origin: "map" | "record";
  70. readonly issues: $ZodIssue[];
  71. readonly input?: Input;
  72. }
  73. export interface $ZodIssueInvalidElement<Input = unknown> extends $ZodIssueBase {
  74. readonly code: "invalid_element";
  75. readonly origin: "map" | "set";
  76. readonly key: unknown;
  77. readonly issues: $ZodIssue[];
  78. readonly input?: Input;
  79. }
  80. export interface $ZodIssueInvalidValue<Input = unknown> extends $ZodIssueBase {
  81. readonly code: "invalid_value";
  82. readonly values: util.Primitive[];
  83. readonly input?: Input;
  84. }
  85. export interface $ZodIssueCustom extends $ZodIssueBase {
  86. readonly code: "custom";
  87. readonly params?: Record<string, any> | undefined;
  88. readonly input?: unknown;
  89. }
  90. export interface $ZodIssueStringCommonFormats extends $ZodIssueInvalidStringFormat {
  91. format: Exclude<$ZodStringFormats, "regex" | "jwt" | "starts_with" | "ends_with" | "includes">;
  92. }
  93. export interface $ZodIssueStringInvalidRegex extends $ZodIssueInvalidStringFormat {
  94. format: "regex";
  95. pattern: string;
  96. }
  97. export interface $ZodIssueStringInvalidJWT extends $ZodIssueInvalidStringFormat {
  98. format: "jwt";
  99. algorithm?: string;
  100. }
  101. export interface $ZodIssueStringStartsWith extends $ZodIssueInvalidStringFormat {
  102. format: "starts_with";
  103. prefix: string;
  104. }
  105. export interface $ZodIssueStringEndsWith extends $ZodIssueInvalidStringFormat {
  106. format: "ends_with";
  107. suffix: string;
  108. }
  109. export interface $ZodIssueStringIncludes extends $ZodIssueInvalidStringFormat {
  110. format: "includes";
  111. includes: string;
  112. }
  113. export type $ZodStringFormatIssues = $ZodIssueStringCommonFormats | $ZodIssueStringInvalidRegex | $ZodIssueStringInvalidJWT | $ZodIssueStringStartsWith | $ZodIssueStringEndsWith | $ZodIssueStringIncludes;
  114. export type $ZodIssue = $ZodIssueInvalidType | $ZodIssueTooBig | $ZodIssueTooSmall | $ZodIssueInvalidStringFormat | $ZodIssueNotMultipleOf | $ZodIssueUnrecognizedKeys | $ZodIssueInvalidUnion | $ZodIssueInvalidKey | $ZodIssueInvalidElement | $ZodIssueInvalidValue | $ZodIssueCustom;
  115. export type $ZodIssueCode = $ZodIssue["code"];
  116. export type $ZodInternalIssue<T extends $ZodIssueBase = $ZodIssue> = T extends any ? RawIssue<T> : never;
  117. type RawIssue<T extends $ZodIssueBase> = T extends any ? util.Flatten<util.MakePartial<T, "message" | "path"> & {
  118. /** The input data */
  119. readonly input: unknown;
  120. /** The schema or check that originated this issue. */
  121. readonly inst?: $ZodType | $ZodCheck;
  122. /** If `true`, Zod will continue executing checks/refinements after this issue. */
  123. readonly continue?: boolean | undefined;
  124. } & Record<string, unknown>> : never;
  125. export type $ZodRawIssue<T extends $ZodIssueBase = $ZodIssue> = $ZodInternalIssue<T>;
  126. export interface $ZodErrorMap<T extends $ZodIssueBase = $ZodIssue> {
  127. (issue: $ZodRawIssue<T>): {
  128. message: string;
  129. } | string | undefined | null;
  130. }
  131. export interface $ZodError<T = unknown> extends Error {
  132. type: T;
  133. issues: $ZodIssue[];
  134. _zod: {
  135. output: T;
  136. def: $ZodIssue[];
  137. };
  138. stack?: string;
  139. name: string;
  140. }
  141. export declare const $ZodError: $constructor<$ZodError>;
  142. interface $ZodRealError<T = any> extends $ZodError<T> {
  143. }
  144. export declare const $ZodRealError: $constructor<$ZodRealError>;
  145. export type $ZodFlattenedError<T, U = string> = _FlattenedError<T, U>;
  146. type _FlattenedError<T, U = string> = {
  147. formErrors: U[];
  148. fieldErrors: {
  149. [P in keyof T]?: U[];
  150. };
  151. };
  152. export declare function flattenError<T>(error: $ZodError<T>): _FlattenedError<T>;
  153. export declare function flattenError<T, U>(error: $ZodError<T>, mapper?: (issue: $ZodIssue) => U): _FlattenedError<T, U>;
  154. type _ZodFormattedError<T, U = string> = T extends [any, ...any[]] ? {
  155. [K in keyof T]?: $ZodFormattedError<T[K], U>;
  156. } : T extends any[] ? {
  157. [k: number]: $ZodFormattedError<T[number], U>;
  158. } : T extends object ? util.Flatten<{
  159. [K in keyof T]?: $ZodFormattedError<T[K], U>;
  160. }> : any;
  161. export type $ZodFormattedError<T, U = string> = {
  162. _errors: U[];
  163. } & util.Flatten<_ZodFormattedError<T, U>>;
  164. export declare function formatError<T>(error: $ZodError<T>): $ZodFormattedError<T>;
  165. export declare function formatError<T, U>(error: $ZodError<T>, mapper?: (issue: $ZodIssue) => U): $ZodFormattedError<T, U>;
  166. export type $ZodErrorTree<T, U = string> = T extends util.Primitive ? {
  167. errors: U[];
  168. } : T extends [any, ...any[]] ? {
  169. errors: U[];
  170. items?: {
  171. [K in keyof T]?: $ZodErrorTree<T[K], U>;
  172. };
  173. } : T extends any[] ? {
  174. errors: U[];
  175. items?: Array<$ZodErrorTree<T[number], U>>;
  176. } : T extends object ? {
  177. errors: U[];
  178. properties?: {
  179. [K in keyof T]?: $ZodErrorTree<T[K], U>;
  180. };
  181. } : {
  182. errors: U[];
  183. };
  184. export declare function treeifyError<T>(error: $ZodError<T>): $ZodErrorTree<T>;
  185. export declare function treeifyError<T, U>(error: $ZodError<T>, mapper?: (issue: $ZodIssue) => U): $ZodErrorTree<T, U>;
  186. /** Format a ZodError as a human-readable string in the following form.
  187. *
  188. * From
  189. *
  190. * ```ts
  191. * ZodError {
  192. * issues: [
  193. * {
  194. * expected: 'string',
  195. * code: 'invalid_type',
  196. * path: [ 'username' ],
  197. * message: 'Invalid input: expected string'
  198. * },
  199. * {
  200. * expected: 'number',
  201. * code: 'invalid_type',
  202. * path: [ 'favoriteNumbers', 1 ],
  203. * message: 'Invalid input: expected number'
  204. * }
  205. * ];
  206. * }
  207. * ```
  208. *
  209. * to
  210. *
  211. * ```
  212. * username
  213. * ✖ Expected number, received string at "username
  214. * favoriteNumbers[0]
  215. * ✖ Invalid input: expected number
  216. * ```
  217. */
  218. export declare function toDotPath(_path: readonly (string | number | symbol | StandardSchemaV1.PathSegment)[]): string;
  219. export declare function prettifyError(error: StandardSchemaV1.FailureResult): string;
  220. export {};