ZodError.d.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import type { TypeOf, ZodType } from ".";
  2. import { Primitive } from "./helpers/typeAliases";
  3. import { util, ZodParsedType } from "./helpers/util";
  4. declare type allKeys<T> = T extends any ? keyof T : never;
  5. export declare type inferFlattenedErrors<T extends ZodType<any, any, any>, U = string> = typeToFlattenedError<TypeOf<T>, U>;
  6. export declare type typeToFlattenedError<T, U = string> = {
  7. formErrors: U[];
  8. fieldErrors: {
  9. [P in allKeys<T>]?: U[];
  10. };
  11. };
  12. export declare const ZodIssueCode: {
  13. invalid_type: "invalid_type";
  14. invalid_literal: "invalid_literal";
  15. custom: "custom";
  16. invalid_union: "invalid_union";
  17. invalid_union_discriminator: "invalid_union_discriminator";
  18. invalid_enum_value: "invalid_enum_value";
  19. unrecognized_keys: "unrecognized_keys";
  20. invalid_arguments: "invalid_arguments";
  21. invalid_return_type: "invalid_return_type";
  22. invalid_date: "invalid_date";
  23. invalid_string: "invalid_string";
  24. too_small: "too_small";
  25. too_big: "too_big";
  26. invalid_intersection_types: "invalid_intersection_types";
  27. not_multiple_of: "not_multiple_of";
  28. not_finite: "not_finite";
  29. };
  30. export declare type ZodIssueCode = keyof typeof ZodIssueCode;
  31. export declare type ZodIssueBase = {
  32. path: (string | number)[];
  33. message?: string;
  34. };
  35. export interface ZodInvalidTypeIssue extends ZodIssueBase {
  36. code: typeof ZodIssueCode.invalid_type;
  37. expected: ZodParsedType;
  38. received: ZodParsedType;
  39. }
  40. export interface ZodInvalidLiteralIssue extends ZodIssueBase {
  41. code: typeof ZodIssueCode.invalid_literal;
  42. expected: unknown;
  43. received: unknown;
  44. }
  45. export interface ZodUnrecognizedKeysIssue extends ZodIssueBase {
  46. code: typeof ZodIssueCode.unrecognized_keys;
  47. keys: string[];
  48. }
  49. export interface ZodInvalidUnionIssue extends ZodIssueBase {
  50. code: typeof ZodIssueCode.invalid_union;
  51. unionErrors: ZodError[];
  52. }
  53. export interface ZodInvalidUnionDiscriminatorIssue extends ZodIssueBase {
  54. code: typeof ZodIssueCode.invalid_union_discriminator;
  55. options: Primitive[];
  56. }
  57. export interface ZodInvalidEnumValueIssue extends ZodIssueBase {
  58. received: string | number;
  59. code: typeof ZodIssueCode.invalid_enum_value;
  60. options: (string | number)[];
  61. }
  62. export interface ZodInvalidArgumentsIssue extends ZodIssueBase {
  63. code: typeof ZodIssueCode.invalid_arguments;
  64. argumentsError: ZodError;
  65. }
  66. export interface ZodInvalidReturnTypeIssue extends ZodIssueBase {
  67. code: typeof ZodIssueCode.invalid_return_type;
  68. returnTypeError: ZodError;
  69. }
  70. export interface ZodInvalidDateIssue extends ZodIssueBase {
  71. code: typeof ZodIssueCode.invalid_date;
  72. }
  73. export declare type StringValidation = "email" | "url" | "emoji" | "uuid" | "regex" | "cuid" | "cuid2" | "ulid" | "datetime" | "ip" | {
  74. includes: string;
  75. position?: number;
  76. } | {
  77. startsWith: string;
  78. } | {
  79. endsWith: string;
  80. };
  81. export interface ZodInvalidStringIssue extends ZodIssueBase {
  82. code: typeof ZodIssueCode.invalid_string;
  83. validation: StringValidation;
  84. }
  85. export interface ZodTooSmallIssue extends ZodIssueBase {
  86. code: typeof ZodIssueCode.too_small;
  87. minimum: number | bigint;
  88. inclusive: boolean;
  89. exact?: boolean;
  90. type: "array" | "string" | "number" | "set" | "date" | "bigint";
  91. }
  92. export interface ZodTooBigIssue extends ZodIssueBase {
  93. code: typeof ZodIssueCode.too_big;
  94. maximum: number | bigint;
  95. inclusive: boolean;
  96. exact?: boolean;
  97. type: "array" | "string" | "number" | "set" | "date" | "bigint";
  98. }
  99. export interface ZodInvalidIntersectionTypesIssue extends ZodIssueBase {
  100. code: typeof ZodIssueCode.invalid_intersection_types;
  101. }
  102. export interface ZodNotMultipleOfIssue extends ZodIssueBase {
  103. code: typeof ZodIssueCode.not_multiple_of;
  104. multipleOf: number | bigint;
  105. }
  106. export interface ZodNotFiniteIssue extends ZodIssueBase {
  107. code: typeof ZodIssueCode.not_finite;
  108. }
  109. export interface ZodCustomIssue extends ZodIssueBase {
  110. code: typeof ZodIssueCode.custom;
  111. params?: {
  112. [k: string]: any;
  113. };
  114. }
  115. export declare type DenormalizedError = {
  116. [k: string]: DenormalizedError | string[];
  117. };
  118. export declare type ZodIssueOptionalMessage = ZodInvalidTypeIssue | ZodInvalidLiteralIssue | ZodUnrecognizedKeysIssue | ZodInvalidUnionIssue | ZodInvalidUnionDiscriminatorIssue | ZodInvalidEnumValueIssue | ZodInvalidArgumentsIssue | ZodInvalidReturnTypeIssue | ZodInvalidDateIssue | ZodInvalidStringIssue | ZodTooSmallIssue | ZodTooBigIssue | ZodInvalidIntersectionTypesIssue | ZodNotMultipleOfIssue | ZodNotFiniteIssue | ZodCustomIssue;
  119. export declare type ZodIssue = ZodIssueOptionalMessage & {
  120. fatal?: boolean;
  121. message: string;
  122. };
  123. export declare const quotelessJson: (obj: any) => string;
  124. declare type recursiveZodFormattedError<T> = T extends [any, ...any[]] ? {
  125. [K in keyof T]?: ZodFormattedError<T[K]>;
  126. } : T extends any[] ? {
  127. [k: number]: ZodFormattedError<T[number]>;
  128. } : T extends object ? {
  129. [K in keyof T]?: ZodFormattedError<T[K]>;
  130. } : unknown;
  131. export declare type ZodFormattedError<T, U = string> = {
  132. _errors: U[];
  133. } & recursiveZodFormattedError<NonNullable<T>>;
  134. export declare type inferFormattedError<T extends ZodType<any, any, any>, U = string> = ZodFormattedError<TypeOf<T>, U>;
  135. export declare class ZodError<T = any> extends Error {
  136. issues: ZodIssue[];
  137. get errors(): ZodIssue[];
  138. constructor(issues: ZodIssue[]);
  139. format(): ZodFormattedError<T>;
  140. format<U>(mapper: (issue: ZodIssue) => U): ZodFormattedError<T, U>;
  141. static create: (issues: ZodIssue[]) => ZodError<any>;
  142. toString(): string;
  143. get message(): string;
  144. get isEmpty(): boolean;
  145. addIssue: (sub: ZodIssue) => void;
  146. addIssues: (subs?: ZodIssue[]) => void;
  147. flatten(): typeToFlattenedError<T>;
  148. flatten<U>(mapper?: (issue: ZodIssue) => U): typeToFlattenedError<T, U>;
  149. get formErrors(): typeToFlattenedError<T, string>;
  150. }
  151. declare type stripPath<T extends object> = T extends any ? util.OmitKeys<T, "path"> : never;
  152. export declare type IssueData = stripPath<ZodIssueOptionalMessage> & {
  153. path?: (string | number)[];
  154. fatal?: boolean;
  155. };
  156. export declare type ErrorMapCtx = {
  157. defaultError: string;
  158. data: any;
  159. };
  160. export declare type ZodErrorMap = (issue: ZodIssueOptionalMessage, _ctx: ErrorMapCtx) => {
  161. message: string;
  162. };
  163. export {};