zod-compiler 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,294 @@
1
+ declare enum ZcParsedType {
2
+ string = "string",
3
+ nan = "nan",
4
+ number = "number",
5
+ integer = "integer",
6
+ float = "float",
7
+ boolean = "boolean",
8
+ date = "date",
9
+ bigint = "bigint",
10
+ symbol = "symbol",
11
+ function = "function",
12
+ undefined = "undefined",
13
+ null = "null",
14
+ array = "array",
15
+ object = "object",
16
+ unknown = "unknown",
17
+ promise = "promise",
18
+ void = "void",
19
+ never = "never",
20
+ map = "map",
21
+ set = "set"
22
+ }
23
+ declare function typeOf(data: any): ZcParsedType;
24
+ declare function mergeValues(a: any, b: any): {
25
+ valid: true;
26
+ data: any;
27
+ } | {
28
+ valid: false;
29
+ };
30
+ declare function isValidJWT(jwt: string, alg?: string): boolean;
31
+ declare function floatSafeRemainder(val: number, step: number): number;
32
+ declare const helpers: {
33
+ readonly typeOf: typeof typeOf;
34
+ readonly mergeValues: typeof mergeValues;
35
+ readonly isValidJWT: typeof isValidJWT;
36
+ readonly floatSafeRemainder: typeof floatSafeRemainder;
37
+ };
38
+
39
+ type Primitive = string | number | symbol | bigint | boolean | null | undefined;
40
+ type Scalars = Primitive | Primitive[];
41
+ type typeToFlattenedError<T, U = string> = {
42
+ formErrors: U[];
43
+ fieldErrors: {
44
+ [P in keyof T]?: U[];
45
+ };
46
+ };
47
+ declare enum ZcIssueCode {
48
+ invalid_type = "invalid_type",
49
+ invalid_literal = "invalid_literal",
50
+ custom = "custom",
51
+ invalid_union = "invalid_union",
52
+ invalid_union_discriminator = "invalid_union_discriminator",
53
+ invalid_enum_value = "invalid_enum_value",
54
+ unrecognized_keys = "unrecognized_keys",
55
+ invalid_arguments = "invalid_arguments",
56
+ invalid_return_type = "invalid_return_type",
57
+ invalid_date = "invalid_date",
58
+ invalid_string = "invalid_string",
59
+ too_small = "too_small",
60
+ too_big = "too_big",
61
+ invalid_intersection_types = "invalid_intersection_types",
62
+ not_multiple_of = "not_multiple_of",
63
+ not_finite = "not_finite"
64
+ }
65
+ interface ZcIssueBase {
66
+ path: (string | number)[];
67
+ message?: string;
68
+ }
69
+ interface ZcInvalidTypeIssue extends ZcIssueBase {
70
+ code: ZcIssueCode.invalid_type;
71
+ expected: ZcParsedType;
72
+ received: ZcParsedType;
73
+ }
74
+ interface ZcInvalidLiteralIssue extends ZcIssueBase {
75
+ code: ZcIssueCode.invalid_literal;
76
+ expected: unknown;
77
+ received: unknown;
78
+ }
79
+ interface ZcUnrecognizedKeysIssue extends ZcIssueBase {
80
+ code: ZcIssueCode.unrecognized_keys;
81
+ keys: string[];
82
+ }
83
+ interface ZcInvalidUnionIssue extends ZcIssueBase {
84
+ code: ZcIssueCode.invalid_union;
85
+ unionErrors: ZcError[];
86
+ }
87
+ interface ZcInvalidUnionDiscriminatorIssue extends ZcIssueBase {
88
+ code: ZcIssueCode.invalid_union_discriminator;
89
+ options: Primitive[];
90
+ }
91
+ interface ZcInvalidEnumValueIssue extends ZcIssueBase {
92
+ code: ZcIssueCode.invalid_enum_value;
93
+ received: string | number;
94
+ options: (string | number)[];
95
+ }
96
+ interface ZcInvalidArgumentsIssue extends ZcIssueBase {
97
+ code: ZcIssueCode.invalid_arguments;
98
+ argumentsError: ZcError;
99
+ }
100
+ interface ZcInvalidReturnTypeIssue extends ZcIssueBase {
101
+ code: ZcIssueCode.invalid_return_type;
102
+ returnTypeError: ZcError;
103
+ }
104
+ interface ZcInvalidDateIssue extends ZcIssueBase {
105
+ code: ZcIssueCode.invalid_date;
106
+ }
107
+ type StringValidation = 'email' | 'url' | 'emoji' | 'uuid' | 'nanoid' | 'regex' | 'cuid' | 'cuid2' | 'ulid' | 'datetime' | 'date' | 'time' | 'duration' | 'ip' | 'cidr' | 'base64' | 'jwt' | 'base64url' | {
108
+ includes: string;
109
+ position?: number;
110
+ } | {
111
+ startsWith: string;
112
+ } | {
113
+ endsWith: string;
114
+ };
115
+ interface ZcInvalidStringIssue extends ZcIssueBase {
116
+ code: ZcIssueCode.invalid_string;
117
+ validation: StringValidation;
118
+ }
119
+ interface ZcTooSmallIssue extends ZcIssueBase {
120
+ code: ZcIssueCode.too_small;
121
+ minimum: number | bigint;
122
+ inclusive: boolean;
123
+ exact?: boolean;
124
+ type: 'array' | 'string' | 'number' | 'set' | 'date' | 'bigint';
125
+ }
126
+ interface ZcTooBigIssue extends ZcIssueBase {
127
+ code: ZcIssueCode.too_big;
128
+ maximum: number | bigint;
129
+ inclusive: boolean;
130
+ exact?: boolean;
131
+ type: 'array' | 'string' | 'number' | 'set' | 'date' | 'bigint';
132
+ }
133
+ interface ZcInvalidIntersectionTypesIssue extends ZcIssueBase {
134
+ code: ZcIssueCode.invalid_intersection_types;
135
+ }
136
+ interface ZcNotMultipleOfIssue extends ZcIssueBase {
137
+ code: ZcIssueCode.not_multiple_of;
138
+ multipleOf: number | bigint;
139
+ }
140
+ interface ZcNotFiniteIssue extends ZcIssueBase {
141
+ code: ZcIssueCode.not_finite;
142
+ }
143
+ interface ZcCustomIssue extends ZcIssueBase {
144
+ code: ZcIssueCode.custom;
145
+ params?: {
146
+ [k: string]: any;
147
+ };
148
+ }
149
+ type DenormalizedError = {
150
+ [k: string]: DenormalizedError | string[];
151
+ };
152
+ type ZcIssueOptionalMessage = ZcInvalidTypeIssue | ZcInvalidLiteralIssue | ZcUnrecognizedKeysIssue | ZcInvalidUnionIssue | ZcInvalidUnionDiscriminatorIssue | ZcInvalidEnumValueIssue | ZcInvalidArgumentsIssue | ZcInvalidReturnTypeIssue | ZcInvalidDateIssue | ZcInvalidStringIssue | ZcTooSmallIssue | ZcTooBigIssue | ZcInvalidIntersectionTypesIssue | ZcNotMultipleOfIssue | ZcNotFiniteIssue | ZcCustomIssue;
153
+ type ZcIssue = ZcIssueOptionalMessage & {
154
+ fatal?: boolean;
155
+ message: string;
156
+ };
157
+ type recursiveZcFormattedError<T> = T extends [any, ...any[]] ? {
158
+ [K in keyof T]?: ZcFormattedError<T[K]>;
159
+ } : T extends any[] ? {
160
+ [k: number]: ZcFormattedError<T[number]>;
161
+ } : T extends object ? {
162
+ [K in keyof T]?: ZcFormattedError<T[K]>;
163
+ } : unknown;
164
+ type ZcFormattedError<T, U = string> = {
165
+ _errors: U[];
166
+ } & recursiveZcFormattedError<NonNullable<T>>;
167
+ declare class ZcError<T = any> extends Error {
168
+ issues: ZcIssue[];
169
+ get errors(): ZcIssue[];
170
+ constructor(issues: ZcIssue[]);
171
+ format(): ZcFormattedError<T>;
172
+ format<U>(mapper: (issue: ZcIssue) => U): ZcFormattedError<T, U>;
173
+ static create(issues: ZcIssue[]): ZcError;
174
+ toString(): string;
175
+ get message(): string;
176
+ get isEmpty(): boolean;
177
+ addIssue(sub: ZcIssue): void;
178
+ addIssues(subs?: ZcIssue[]): void;
179
+ flatten(): typeToFlattenedError<T>;
180
+ flatten<U>(mapper?: (issue: ZcIssue) => U): typeToFlattenedError<T, U>;
181
+ get formErrors(): typeToFlattenedError<T>;
182
+ }
183
+ type IssueData = Omit<ZcIssueOptionalMessage, 'path'> & {
184
+ path?: (string | number)[];
185
+ fatal?: boolean;
186
+ };
187
+ type ErrorMapCtx = {
188
+ defaultError: string;
189
+ data: any;
190
+ };
191
+ type ZcErrorMap = (issue: ZcIssueOptionalMessage, ctx: ErrorMapCtx) => {
192
+ message: string;
193
+ };
194
+
195
+ declare const CUID: RegExp;
196
+ declare const CUID2: RegExp;
197
+ declare const ULID: RegExp;
198
+ declare const UUID: RegExp;
199
+ declare const NANOID: RegExp;
200
+ declare const JWT: RegExp;
201
+ declare const DURATION: RegExp;
202
+ declare const EMAIL: RegExp;
203
+ declare const EMOJI: RegExp;
204
+ declare const IPV4: RegExp;
205
+ declare const IPV4_CIDR: RegExp;
206
+ declare const IPV6: RegExp;
207
+ declare const IPV6_CIDR: RegExp;
208
+ declare const BASE64: RegExp;
209
+ declare const BASE64URL: RegExp;
210
+ declare const DATE: RegExp;
211
+
212
+ declare const regex_BASE64: typeof BASE64;
213
+ declare const regex_BASE64URL: typeof BASE64URL;
214
+ declare const regex_CUID: typeof CUID;
215
+ declare const regex_CUID2: typeof CUID2;
216
+ declare const regex_DATE: typeof DATE;
217
+ declare const regex_DURATION: typeof DURATION;
218
+ declare const regex_EMAIL: typeof EMAIL;
219
+ declare const regex_EMOJI: typeof EMOJI;
220
+ declare const regex_IPV4: typeof IPV4;
221
+ declare const regex_IPV4_CIDR: typeof IPV4_CIDR;
222
+ declare const regex_IPV6: typeof IPV6;
223
+ declare const regex_IPV6_CIDR: typeof IPV6_CIDR;
224
+ declare const regex_JWT: typeof JWT;
225
+ declare const regex_NANOID: typeof NANOID;
226
+ declare const regex_ULID: typeof ULID;
227
+ declare const regex_UUID: typeof UUID;
228
+ declare namespace regex {
229
+ export {
230
+ regex_BASE64 as BASE64,
231
+ regex_BASE64URL as BASE64URL,
232
+ regex_CUID as CUID,
233
+ regex_CUID2 as CUID2,
234
+ regex_DATE as DATE,
235
+ regex_DURATION as DURATION,
236
+ regex_EMAIL as EMAIL,
237
+ regex_EMOJI as EMOJI,
238
+ regex_IPV4 as IPV4,
239
+ regex_IPV4_CIDR as IPV4_CIDR,
240
+ regex_IPV6 as IPV6,
241
+ regex_IPV6_CIDR as IPV6_CIDR,
242
+ regex_JWT as JWT,
243
+ regex_NANOID as NANOID,
244
+ regex_ULID as ULID,
245
+ regex_UUID as UUID,
246
+ };
247
+ }
248
+
249
+ declare const errorMap: ZcErrorMap;
250
+
251
+ declare function setErrorMap(map: ZcErrorMap): void;
252
+ declare function getErrorMap(): ZcErrorMap;
253
+
254
+ declare const enum ParseStatus {
255
+ VALID = 0,
256
+ /** At least one issue has been identified, but parsing can still continue to identify more. */
257
+ DIRTY = 1,
258
+ /** A fatal issue has been encountered and parsing cannot continue. */
259
+ INVALID = 2
260
+ }
261
+ interface ParseContext<T> {
262
+ output: T | null;
263
+ helpers: typeof helpers;
264
+ ZcError: typeof ZcError<T>;
265
+ regex: typeof regex;
266
+ dependencies: readonly any[];
267
+ basePath: (string | number)[];
268
+ errorMaps: ZcErrorMap[];
269
+ issues: ZcIssue[];
270
+ reportIssue(issue: ZcIssue, input?: any): void;
271
+ }
272
+ type SafeParseSuccess<T> = {
273
+ success: true;
274
+ data: T;
275
+ error?: never;
276
+ };
277
+ type SafeParseError<T> = {
278
+ success: false;
279
+ error: ZcError<T>;
280
+ data?: never;
281
+ };
282
+ interface ParseParams {
283
+ path?: (string | number)[];
284
+ errorMap?: ZcErrorMap;
285
+ }
286
+ interface CompiledParser<T> {
287
+ parse(data: unknown, params?: ParseParams): T;
288
+ safeParse(data: unknown, params?: ParseParams): SafeParseSuccess<T> | SafeParseError<T>;
289
+ }
290
+ declare const createContext: <T>(dependencies: readonly any[], parseParams: ParseParams | undefined) => ParseContext<T>;
291
+ declare function standalone<T>(parser: Function, dependencies?: readonly any[]): CompiledParser<T>;
292
+
293
+ export { ParseStatus, ZcError, ZcIssueCode, createContext, standalone as default, errorMap as defaultErrorMap, getErrorMap, setErrorMap };
294
+ export type { CompiledParser, DenormalizedError, ErrorMapCtx, IssueData, ParseContext, ParseParams, Primitive, SafeParseError, SafeParseSuccess, Scalars, StringValidation, ZcCustomIssue, ZcErrorMap, ZcFormattedError, ZcInvalidArgumentsIssue, ZcInvalidDateIssue, ZcInvalidEnumValueIssue, ZcInvalidIntersectionTypesIssue, ZcInvalidLiteralIssue, ZcInvalidReturnTypeIssue, ZcInvalidStringIssue, ZcInvalidTypeIssue, ZcInvalidUnionDiscriminatorIssue, ZcInvalidUnionIssue, ZcIssue, ZcIssueBase, ZcIssueOptionalMessage, ZcNotFiniteIssue, ZcNotMultipleOfIssue, ZcTooBigIssue, ZcTooSmallIssue, ZcUnrecognizedKeysIssue, typeToFlattenedError };