UNPKG

6.44 kBTypeScriptView Raw
1import type { TypeOf, ZodType } from ".";
2import { Primitive } from "./helpers/typeAliases";
3import { util, ZodParsedType } from "./helpers/util";
4declare type allKeys<T> = T extends any ? keyof T : never;
5export declare type inferFlattenedErrors<T extends ZodType<any, any, any>, U = string> = typeToFlattenedError<TypeOf<T>, U>;
6export declare type typeToFlattenedError<T, U = string> = {
7 formErrors: U[];
8 fieldErrors: {
9 [P in allKeys<T>]?: U[];
10 };
11};
12export 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};
30export declare type ZodIssueCode = keyof typeof ZodIssueCode;
31export declare type ZodIssueBase = {
32 path: (string | number)[];
33 message?: string;
34};
35export interface ZodInvalidTypeIssue extends ZodIssueBase {
36 code: typeof ZodIssueCode.invalid_type;
37 expected: ZodParsedType;
38 received: ZodParsedType;
39}
40export interface ZodInvalidLiteralIssue extends ZodIssueBase {
41 code: typeof ZodIssueCode.invalid_literal;
42 expected: unknown;
43 received: unknown;
44}
45export interface ZodUnrecognizedKeysIssue extends ZodIssueBase {
46 code: typeof ZodIssueCode.unrecognized_keys;
47 keys: string[];
48}
49export interface ZodInvalidUnionIssue extends ZodIssueBase {
50 code: typeof ZodIssueCode.invalid_union;
51 unionErrors: ZodError[];
52}
53export interface ZodInvalidUnionDiscriminatorIssue extends ZodIssueBase {
54 code: typeof ZodIssueCode.invalid_union_discriminator;
55 options: Primitive[];
56}
57export interface ZodInvalidEnumValueIssue extends ZodIssueBase {
58 received: string | number;
59 code: typeof ZodIssueCode.invalid_enum_value;
60 options: (string | number)[];
61}
62export interface ZodInvalidArgumentsIssue extends ZodIssueBase {
63 code: typeof ZodIssueCode.invalid_arguments;
64 argumentsError: ZodError;
65}
66export interface ZodInvalidReturnTypeIssue extends ZodIssueBase {
67 code: typeof ZodIssueCode.invalid_return_type;
68 returnTypeError: ZodError;
69}
70export interface ZodInvalidDateIssue extends ZodIssueBase {
71 code: typeof ZodIssueCode.invalid_date;
72}
73export declare type StringValidation = "email" | "url" | "emoji" | "uuid" | "nanoid" | "regex" | "cuid" | "cuid2" | "ulid" | "datetime" | "date" | "time" | "duration" | "ip" | "base64" | {
74 includes: string;
75 position?: number;
76} | {
77 startsWith: string;
78} | {
79 endsWith: string;
80};
81export interface ZodInvalidStringIssue extends ZodIssueBase {
82 code: typeof ZodIssueCode.invalid_string;
83 validation: StringValidation;
84}
85export 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}
92export 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}
99export interface ZodInvalidIntersectionTypesIssue extends ZodIssueBase {
100 code: typeof ZodIssueCode.invalid_intersection_types;
101}
102export interface ZodNotMultipleOfIssue extends ZodIssueBase {
103 code: typeof ZodIssueCode.not_multiple_of;
104 multipleOf: number | bigint;
105}
106export interface ZodNotFiniteIssue extends ZodIssueBase {
107 code: typeof ZodIssueCode.not_finite;
108}
109export interface ZodCustomIssue extends ZodIssueBase {
110 code: typeof ZodIssueCode.custom;
111 params?: {
112 [k: string]: any;
113 };
114}
115export declare type DenormalizedError = {
116 [k: string]: DenormalizedError | string[];
117};
118export declare type ZodIssueOptionalMessage = ZodInvalidTypeIssue | ZodInvalidLiteralIssue | ZodUnrecognizedKeysIssue | ZodInvalidUnionIssue | ZodInvalidUnionDiscriminatorIssue | ZodInvalidEnumValueIssue | ZodInvalidArgumentsIssue | ZodInvalidReturnTypeIssue | ZodInvalidDateIssue | ZodInvalidStringIssue | ZodTooSmallIssue | ZodTooBigIssue | ZodInvalidIntersectionTypesIssue | ZodNotMultipleOfIssue | ZodNotFiniteIssue | ZodCustomIssue;
119export declare type ZodIssue = ZodIssueOptionalMessage & {
120 fatal?: boolean;
121 message: string;
122};
123export declare const quotelessJson: (obj: any) => string;
124declare 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;
131export declare type ZodFormattedError<T, U = string> = {
132 _errors: U[];
133} & recursiveZodFormattedError<NonNullable<T>>;
134export declare type inferFormattedError<T extends ZodType<any, any, any>, U = string> = ZodFormattedError<TypeOf<T>, U>;
135export 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 static assert(value: unknown): asserts value is ZodError;
143 toString(): string;
144 get message(): string;
145 get isEmpty(): boolean;
146 addIssue: (sub: ZodIssue) => void;
147 addIssues: (subs?: ZodIssue[]) => void;
148 flatten(): typeToFlattenedError<T>;
149 flatten<U>(mapper?: (issue: ZodIssue) => U): typeToFlattenedError<T, U>;
150 get formErrors(): typeToFlattenedError<T, string>;
151}
152declare type stripPath<T extends object> = T extends any ? util.OmitKeys<T, "path"> : never;
153export declare type IssueData = stripPath<ZodIssueOptionalMessage> & {
154 path?: (string | number)[];
155 fatal?: boolean;
156};
157export declare type ErrorMapCtx = {
158 defaultError: string;
159 data: any;
160};
161export declare type ZodErrorMap = (issue: ZodIssueOptionalMessage, _ctx: ErrorMapCtx) => {
162 message: string;
163};
164export {};