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.
- package/README.md +40 -0
- package/dist/index.d.ts +670 -0
- package/dist/index.js +2550 -0
- package/dist/index.mjs +2536 -0
- package/dist/standalone.d.ts +294 -0
- package/dist/standalone.js +523 -0
- package/dist/standalone.mjs +514 -0
- package/package.json +53 -0
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
// Mirrors ZodParsedType from src/helpers/util.ts
|
|
2
|
+
var ZcParsedType = /*#__PURE__*/ function(ZcParsedType) {
|
|
3
|
+
ZcParsedType["string"] = "string";
|
|
4
|
+
ZcParsedType["nan"] = "nan";
|
|
5
|
+
ZcParsedType["number"] = "number";
|
|
6
|
+
ZcParsedType["integer"] = "integer";
|
|
7
|
+
ZcParsedType["float"] = "float";
|
|
8
|
+
ZcParsedType["boolean"] = "boolean";
|
|
9
|
+
ZcParsedType["date"] = "date";
|
|
10
|
+
ZcParsedType["bigint"] = "bigint";
|
|
11
|
+
ZcParsedType["symbol"] = "symbol";
|
|
12
|
+
ZcParsedType["function"] = "function";
|
|
13
|
+
ZcParsedType["undefined"] = "undefined";
|
|
14
|
+
ZcParsedType["null"] = "null";
|
|
15
|
+
ZcParsedType["array"] = "array";
|
|
16
|
+
ZcParsedType["object"] = "object";
|
|
17
|
+
ZcParsedType["unknown"] = "unknown";
|
|
18
|
+
ZcParsedType["promise"] = "promise";
|
|
19
|
+
ZcParsedType["void"] = "void";
|
|
20
|
+
ZcParsedType["never"] = "never";
|
|
21
|
+
ZcParsedType["map"] = "map";
|
|
22
|
+
ZcParsedType["set"] = "set";
|
|
23
|
+
return ZcParsedType;
|
|
24
|
+
}({});
|
|
25
|
+
// Mirrors getParsedType from src/helpers/util.ts
|
|
26
|
+
function typeOf(data) {
|
|
27
|
+
switch(typeof data){
|
|
28
|
+
case 'undefined':
|
|
29
|
+
return "undefined";
|
|
30
|
+
case 'string':
|
|
31
|
+
return "string";
|
|
32
|
+
case 'number':
|
|
33
|
+
return isNaN(data) ? "nan" : "number";
|
|
34
|
+
case 'boolean':
|
|
35
|
+
return "boolean";
|
|
36
|
+
case 'function':
|
|
37
|
+
return "function";
|
|
38
|
+
case 'bigint':
|
|
39
|
+
return "bigint";
|
|
40
|
+
case 'symbol':
|
|
41
|
+
return "symbol";
|
|
42
|
+
case 'object':
|
|
43
|
+
if (Array.isArray(data)) {
|
|
44
|
+
return "array";
|
|
45
|
+
}
|
|
46
|
+
if (data === null) {
|
|
47
|
+
return "null";
|
|
48
|
+
}
|
|
49
|
+
if (typeof data.then === 'function' && typeof data.catch === 'function') {
|
|
50
|
+
return "promise";
|
|
51
|
+
}
|
|
52
|
+
if (data instanceof Map) {
|
|
53
|
+
return "map";
|
|
54
|
+
}
|
|
55
|
+
if (data instanceof Set) {
|
|
56
|
+
return "set";
|
|
57
|
+
}
|
|
58
|
+
if (data instanceof Date) {
|
|
59
|
+
return "date";
|
|
60
|
+
}
|
|
61
|
+
return "object";
|
|
62
|
+
default:
|
|
63
|
+
return "unknown";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Mirrors mergeValues from src/types.ts
|
|
67
|
+
function mergeValues(a, b) {
|
|
68
|
+
if (a === b) {
|
|
69
|
+
return {
|
|
70
|
+
valid: true,
|
|
71
|
+
data: a
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const aType = typeOf(a);
|
|
75
|
+
const bType = typeOf(b);
|
|
76
|
+
if (aType === "object" && bType === "object") {
|
|
77
|
+
const bKeys = Object.keys(b);
|
|
78
|
+
const sharedKeys = Object.keys(a).filter((key)=>bKeys.indexOf(key) !== -1);
|
|
79
|
+
const newObj = {
|
|
80
|
+
...a,
|
|
81
|
+
...b
|
|
82
|
+
};
|
|
83
|
+
for (const key of sharedKeys){
|
|
84
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
85
|
+
if (!sharedValue.valid) {
|
|
86
|
+
return {
|
|
87
|
+
valid: false
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
newObj[key] = sharedValue.data;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
valid: true,
|
|
94
|
+
data: newObj
|
|
95
|
+
};
|
|
96
|
+
} else if (aType === "array" && bType === "array") {
|
|
97
|
+
if (a.length !== b.length) {
|
|
98
|
+
return {
|
|
99
|
+
valid: false
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const newArray = [];
|
|
103
|
+
for(let i = 0; i < a.length; i++){
|
|
104
|
+
const sharedValue = mergeValues(a[i], b[i]);
|
|
105
|
+
if (!sharedValue.valid) {
|
|
106
|
+
return {
|
|
107
|
+
valid: false
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
newArray.push(sharedValue.data);
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
valid: true,
|
|
114
|
+
data: newArray
|
|
115
|
+
};
|
|
116
|
+
} else if (aType === "date" && bType == "date" && +a === +b) {
|
|
117
|
+
return {
|
|
118
|
+
valid: true,
|
|
119
|
+
data: a
|
|
120
|
+
};
|
|
121
|
+
} else {
|
|
122
|
+
return {
|
|
123
|
+
valid: false
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function stringify(obj) {
|
|
128
|
+
return JSON.stringify(obj, (_, value)=>{
|
|
129
|
+
if (typeof value === 'bigint') {
|
|
130
|
+
return value.toString();
|
|
131
|
+
}
|
|
132
|
+
return value;
|
|
133
|
+
}, 2 /* ugggghhhhhhhh */ );
|
|
134
|
+
}
|
|
135
|
+
function joinValues(array, separator = ' | ') {
|
|
136
|
+
return array.map((val)=>typeof val === 'string' ? `'${val}'` : val).join(separator);
|
|
137
|
+
}
|
|
138
|
+
function assertNever(_x) {
|
|
139
|
+
throw new Error();
|
|
140
|
+
}
|
|
141
|
+
function isValidJWT(jwt, alg) {
|
|
142
|
+
try {
|
|
143
|
+
const [header] = jwt.split('.');
|
|
144
|
+
// Convert base64url to base64
|
|
145
|
+
const base64 = header.replace(/-/g, '+').replace(/_/g, '/').padEnd(header.length + (4 - header.length % 4) % 4, '=');
|
|
146
|
+
const decoded = JSON.parse(atob(base64));
|
|
147
|
+
if (typeof decoded !== 'object' || decoded === null) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
if (!decoded.typ || !decoded.alg) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
if (alg && decoded.alg !== alg) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
} catch {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function floatSafeRemainder(val, step) {
|
|
162
|
+
const valDecCount = (val.toString().split('.')[1] || '').length;
|
|
163
|
+
const stepDecCount = (step.toString().split('.')[1] || '').length;
|
|
164
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
165
|
+
const valInt = parseInt(val.toFixed(decCount).replace('.', ''));
|
|
166
|
+
const stepInt = parseInt(step.toFixed(decCount).replace('.', ''));
|
|
167
|
+
return valInt % stepInt / Math.pow(10, decCount);
|
|
168
|
+
}
|
|
169
|
+
const helpers = {
|
|
170
|
+
typeOf,
|
|
171
|
+
mergeValues,
|
|
172
|
+
isValidJWT,
|
|
173
|
+
floatSafeRemainder
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
var ZcIssueCode = /*#__PURE__*/ function(ZcIssueCode) {
|
|
177
|
+
ZcIssueCode["invalid_type"] = "invalid_type";
|
|
178
|
+
ZcIssueCode["invalid_literal"] = "invalid_literal";
|
|
179
|
+
ZcIssueCode["custom"] = "custom";
|
|
180
|
+
ZcIssueCode["invalid_union"] = "invalid_union";
|
|
181
|
+
ZcIssueCode["invalid_union_discriminator"] = "invalid_union_discriminator";
|
|
182
|
+
ZcIssueCode["invalid_enum_value"] = "invalid_enum_value";
|
|
183
|
+
ZcIssueCode["unrecognized_keys"] = "unrecognized_keys";
|
|
184
|
+
ZcIssueCode["invalid_arguments"] = "invalid_arguments";
|
|
185
|
+
ZcIssueCode["invalid_return_type"] = "invalid_return_type";
|
|
186
|
+
ZcIssueCode["invalid_date"] = "invalid_date";
|
|
187
|
+
ZcIssueCode["invalid_string"] = "invalid_string";
|
|
188
|
+
ZcIssueCode["too_small"] = "too_small";
|
|
189
|
+
ZcIssueCode["too_big"] = "too_big";
|
|
190
|
+
ZcIssueCode["invalid_intersection_types"] = "invalid_intersection_types";
|
|
191
|
+
ZcIssueCode["not_multiple_of"] = "not_multiple_of";
|
|
192
|
+
ZcIssueCode["not_finite"] = "not_finite";
|
|
193
|
+
return ZcIssueCode;
|
|
194
|
+
}({});
|
|
195
|
+
class ZcError extends Error {
|
|
196
|
+
get errors() {
|
|
197
|
+
return this.issues;
|
|
198
|
+
}
|
|
199
|
+
constructor(issues){
|
|
200
|
+
super(), this.issues = issues;
|
|
201
|
+
// TODO: why is this required?
|
|
202
|
+
const actualProto = new.target.prototype;
|
|
203
|
+
Object.setPrototypeOf(this, actualProto);
|
|
204
|
+
this.name = 'ZcError';
|
|
205
|
+
}
|
|
206
|
+
format(_mapper) {
|
|
207
|
+
const mapper = _mapper ?? ((issue)=>issue.message);
|
|
208
|
+
const fieldErrors = {
|
|
209
|
+
_errors: []
|
|
210
|
+
};
|
|
211
|
+
const processError = (error)=>{
|
|
212
|
+
for (const issue of error.issues){
|
|
213
|
+
if (issue.code === "invalid_union") {
|
|
214
|
+
issue.unionErrors.map(processError);
|
|
215
|
+
} else if (issue.code === "invalid_return_type") {
|
|
216
|
+
processError(issue.returnTypeError);
|
|
217
|
+
} else if (issue.code === "invalid_arguments") {
|
|
218
|
+
processError(issue.argumentsError);
|
|
219
|
+
} else if (issue.path.length === 0) {
|
|
220
|
+
fieldErrors._errors.push(mapper(issue));
|
|
221
|
+
} else {
|
|
222
|
+
let curr = fieldErrors;
|
|
223
|
+
let i = 0;
|
|
224
|
+
while(i < issue.path.length){
|
|
225
|
+
const el = issue.path[i];
|
|
226
|
+
curr[el] ||= {
|
|
227
|
+
_errors: []
|
|
228
|
+
};
|
|
229
|
+
const terminal = i === issue.path.length - 1;
|
|
230
|
+
if (terminal) {
|
|
231
|
+
curr[el]._errors.push(mapper(issue));
|
|
232
|
+
}
|
|
233
|
+
curr = curr[el];
|
|
234
|
+
i++;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
processError(this);
|
|
240
|
+
return fieldErrors;
|
|
241
|
+
}
|
|
242
|
+
static create(issues) {
|
|
243
|
+
return new ZcError(issues);
|
|
244
|
+
}
|
|
245
|
+
toString() {
|
|
246
|
+
return this.message;
|
|
247
|
+
}
|
|
248
|
+
get message() {
|
|
249
|
+
return stringify(this.issues);
|
|
250
|
+
}
|
|
251
|
+
get isEmpty() {
|
|
252
|
+
return this.issues.length === 0;
|
|
253
|
+
}
|
|
254
|
+
addIssue(sub) {
|
|
255
|
+
// why not push...?
|
|
256
|
+
this.issues = [
|
|
257
|
+
...this.issues,
|
|
258
|
+
sub
|
|
259
|
+
];
|
|
260
|
+
}
|
|
261
|
+
addIssues(subs = []) {
|
|
262
|
+
this.issues = [
|
|
263
|
+
...this.issues,
|
|
264
|
+
...subs
|
|
265
|
+
];
|
|
266
|
+
}
|
|
267
|
+
flatten(mapper = (issue)=>issue.message) {
|
|
268
|
+
const fieldErrors = {};
|
|
269
|
+
const formErrors = [];
|
|
270
|
+
for (const sub of this.issues){
|
|
271
|
+
if (sub.path.length > 0) {
|
|
272
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
273
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
274
|
+
} else {
|
|
275
|
+
formErrors.push(mapper(sub));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return {
|
|
279
|
+
formErrors,
|
|
280
|
+
fieldErrors
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
get formErrors() {
|
|
284
|
+
return this.flatten(); //.formErrors?;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const errorMap = (issue, _ctx)=>{
|
|
289
|
+
let message;
|
|
290
|
+
switch(issue.code){
|
|
291
|
+
case ZcIssueCode.invalid_type:
|
|
292
|
+
if (issue.received === ZcParsedType.undefined) {
|
|
293
|
+
message = 'Required';
|
|
294
|
+
} else {
|
|
295
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
296
|
+
}
|
|
297
|
+
break;
|
|
298
|
+
case ZcIssueCode.invalid_literal:
|
|
299
|
+
message = `Invalid literal value, expected ${stringify(issue.expected)}`;
|
|
300
|
+
break;
|
|
301
|
+
case ZcIssueCode.unrecognized_keys:
|
|
302
|
+
message = `Unrecognized key(s) in object: ${joinValues(issue.keys, ', ')}`;
|
|
303
|
+
break;
|
|
304
|
+
case ZcIssueCode.invalid_union:
|
|
305
|
+
message = `Invalid input`;
|
|
306
|
+
break;
|
|
307
|
+
case ZcIssueCode.invalid_union_discriminator:
|
|
308
|
+
message = `Invalid discriminator value. Expected ${joinValues(issue.options)}`;
|
|
309
|
+
break;
|
|
310
|
+
case ZcIssueCode.invalid_enum_value:
|
|
311
|
+
message = `Invalid enum value. Expected ${joinValues(issue.options)}, received '${issue.received}'`;
|
|
312
|
+
break;
|
|
313
|
+
case ZcIssueCode.invalid_arguments:
|
|
314
|
+
message = `Invalid function arguments`;
|
|
315
|
+
break;
|
|
316
|
+
case ZcIssueCode.invalid_return_type:
|
|
317
|
+
message = `Invalid function return type`;
|
|
318
|
+
break;
|
|
319
|
+
case ZcIssueCode.invalid_date:
|
|
320
|
+
message = `Invalid date`;
|
|
321
|
+
break;
|
|
322
|
+
case ZcIssueCode.invalid_string:
|
|
323
|
+
if (typeof issue.validation === 'object') {
|
|
324
|
+
if ('includes' in issue.validation) {
|
|
325
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
326
|
+
if (typeof issue.validation.position === 'number') {
|
|
327
|
+
message += ` at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
328
|
+
}
|
|
329
|
+
} else if ('startsWith' in issue.validation) {
|
|
330
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
331
|
+
} else if ('endsWith' in issue.validation) {
|
|
332
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
333
|
+
} else {
|
|
334
|
+
assertNever(issue.validation);
|
|
335
|
+
}
|
|
336
|
+
} else if (issue.validation !== 'regex') {
|
|
337
|
+
message = `Invalid ${issue.validation}`;
|
|
338
|
+
} else {
|
|
339
|
+
message = 'Invalid';
|
|
340
|
+
}
|
|
341
|
+
break;
|
|
342
|
+
case ZcIssueCode.too_small:
|
|
343
|
+
if (issue.type === 'array') {
|
|
344
|
+
message = `Array must contain ${issue.exact ? 'exactly' : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
345
|
+
} else if (issue.type === 'string') {
|
|
346
|
+
message = `String must contain ${issue.exact ? 'exactly' : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
347
|
+
} else if (issue.type === 'number') {
|
|
348
|
+
message = `Number must be ${issue.exact ? `exactly equal to` : issue.inclusive ? `greater than or equal to` : `greater than`} ${issue.minimum}`;
|
|
349
|
+
} else if (issue.type === 'date') {
|
|
350
|
+
message = `Date must be ${issue.exact ? `exactly equal to` : issue.inclusive ? `greater than or equal to` : `greater than`} ${new Date(Number(issue.minimum))}`;
|
|
351
|
+
} else {
|
|
352
|
+
message = 'Invalid input';
|
|
353
|
+
}
|
|
354
|
+
break;
|
|
355
|
+
case ZcIssueCode.too_big:
|
|
356
|
+
if (issue.type === 'array') {
|
|
357
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
358
|
+
} else if (issue.type === 'string') {
|
|
359
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
360
|
+
} else if (issue.type === 'number') {
|
|
361
|
+
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
362
|
+
} else if (issue.type === 'bigint') {
|
|
363
|
+
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
364
|
+
} else if (issue.type === 'date') {
|
|
365
|
+
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
366
|
+
} else {
|
|
367
|
+
message = 'Invalid input';
|
|
368
|
+
}
|
|
369
|
+
break;
|
|
370
|
+
case ZcIssueCode.custom:
|
|
371
|
+
message = `Invalid input`;
|
|
372
|
+
break;
|
|
373
|
+
case ZcIssueCode.invalid_intersection_types:
|
|
374
|
+
message = `Intersection results could not be merged`;
|
|
375
|
+
break;
|
|
376
|
+
case ZcIssueCode.not_multiple_of:
|
|
377
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
378
|
+
break;
|
|
379
|
+
case ZcIssueCode.not_finite:
|
|
380
|
+
message = 'Number must be finite';
|
|
381
|
+
break;
|
|
382
|
+
default:
|
|
383
|
+
message = _ctx.defaultError;
|
|
384
|
+
assertNever();
|
|
385
|
+
}
|
|
386
|
+
return {
|
|
387
|
+
message
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
let overrideErrorMap = errorMap;
|
|
392
|
+
function setErrorMap(map) {
|
|
393
|
+
overrideErrorMap = map;
|
|
394
|
+
}
|
|
395
|
+
function getErrorMap() {
|
|
396
|
+
return overrideErrorMap;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const CUID = /^c[^\s-]{8,}$/i;
|
|
400
|
+
const CUID2 = /^[0-9a-z]+$/;
|
|
401
|
+
const ULID = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
402
|
+
const UUID = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
403
|
+
const NANOID = /^[a-z0-9_-]{21}$/i;
|
|
404
|
+
const JWT = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
405
|
+
const DURATION = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
406
|
+
const EMAIL = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
407
|
+
const EMOJI = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u;
|
|
408
|
+
const IPV4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
409
|
+
const IPV4_CIDR = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
|
|
410
|
+
const IPV6 = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
411
|
+
const IPV6_CIDR = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
412
|
+
const BASE64 = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
413
|
+
const BASE64URL = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
414
|
+
const DATE = /^((\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\d|3[01])|(0[469]|11)-(0[1-9]|[12]\d|30)|(02)-(0[1-9]|1\d|2[0-8])))$/;
|
|
415
|
+
|
|
416
|
+
var regex = {
|
|
417
|
+
__proto__: null,
|
|
418
|
+
BASE64: BASE64,
|
|
419
|
+
BASE64URL: BASE64URL,
|
|
420
|
+
CUID: CUID,
|
|
421
|
+
CUID2: CUID2,
|
|
422
|
+
DATE: DATE,
|
|
423
|
+
DURATION: DURATION,
|
|
424
|
+
EMAIL: EMAIL,
|
|
425
|
+
EMOJI: EMOJI,
|
|
426
|
+
IPV4: IPV4,
|
|
427
|
+
IPV4_CIDR: IPV4_CIDR,
|
|
428
|
+
IPV6: IPV6,
|
|
429
|
+
IPV6_CIDR: IPV6_CIDR,
|
|
430
|
+
JWT: JWT,
|
|
431
|
+
NANOID: NANOID,
|
|
432
|
+
ULID: ULID,
|
|
433
|
+
UUID: UUID
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
var ParseStatus = /*#__PURE__*/ function(ParseStatus) {
|
|
437
|
+
ParseStatus[ParseStatus["VALID"] = 0] = "VALID";
|
|
438
|
+
/** At least one issue has been identified, but parsing can still continue to identify more. */ ParseStatus[ParseStatus["DIRTY"] = 1] = "DIRTY";
|
|
439
|
+
/** A fatal issue has been encountered and parsing cannot continue. */ ParseStatus[ParseStatus["INVALID"] = 2] = "INVALID";
|
|
440
|
+
return ParseStatus;
|
|
441
|
+
}({});
|
|
442
|
+
const createContext = (dependencies, parseParams)=>{
|
|
443
|
+
const overrideMap = getErrorMap();
|
|
444
|
+
return {
|
|
445
|
+
output: null,
|
|
446
|
+
ZcError,
|
|
447
|
+
helpers,
|
|
448
|
+
regex,
|
|
449
|
+
dependencies,
|
|
450
|
+
basePath: parseParams?.path ?? [],
|
|
451
|
+
errorMaps: [
|
|
452
|
+
parseParams?.errorMap,
|
|
453
|
+
overrideMap,
|
|
454
|
+
overrideMap === errorMap ? undefined : errorMap
|
|
455
|
+
].filter((x)=>!!x),
|
|
456
|
+
issues: [],
|
|
457
|
+
reportIssue (issue, input = null) {
|
|
458
|
+
const fullPath = [
|
|
459
|
+
...this.basePath,
|
|
460
|
+
...issue.path || []
|
|
461
|
+
];
|
|
462
|
+
const fullIssue = {
|
|
463
|
+
...issue,
|
|
464
|
+
path: fullPath
|
|
465
|
+
};
|
|
466
|
+
if (fullIssue.message !== undefined) {
|
|
467
|
+
this.issues.push(fullIssue);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
let errorMessage = '';
|
|
471
|
+
const maps = this.errorMaps.filter((m)=>!!m).slice().reverse();
|
|
472
|
+
for (const map of maps){
|
|
473
|
+
errorMessage = map(fullIssue, {
|
|
474
|
+
data: input,
|
|
475
|
+
defaultError: errorMessage
|
|
476
|
+
}).message;
|
|
477
|
+
}
|
|
478
|
+
this.issues.push({
|
|
479
|
+
...issue,
|
|
480
|
+
message: errorMessage
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
};
|
|
485
|
+
function standalone(parser, dependencies = []) {
|
|
486
|
+
const typedParser = parser;
|
|
487
|
+
return {
|
|
488
|
+
parse (data, params) {
|
|
489
|
+
const ctx = createContext(dependencies, params);
|
|
490
|
+
const status = typedParser(data, ctx);
|
|
491
|
+
if (status === 0) {
|
|
492
|
+
return ctx.output;
|
|
493
|
+
}
|
|
494
|
+
throw new ZcError(ctx.issues);
|
|
495
|
+
},
|
|
496
|
+
safeParse (data, params) {
|
|
497
|
+
const ctx = createContext(dependencies, params);
|
|
498
|
+
const status = typedParser(data, ctx);
|
|
499
|
+
if (status === 0) {
|
|
500
|
+
return {
|
|
501
|
+
success: true,
|
|
502
|
+
data: ctx.output
|
|
503
|
+
};
|
|
504
|
+
} else {
|
|
505
|
+
return {
|
|
506
|
+
success: false,
|
|
507
|
+
error: new ZcError(ctx.issues)
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export { ParseStatus, ZcError, ZcIssueCode, createContext, standalone as default, errorMap as defaultErrorMap, getErrorMap, setErrorMap };
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zod-compiler",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Compile Zod schemas to fast parsers or TypeScript types",
|
|
5
|
+
"author": "Carson M. <carson@pyke.io>",
|
|
6
|
+
"license": "MIT OR Apache-2.0",
|
|
7
|
+
"keywords": [ "zod", "accelerator", "fast", "schema", "validation", "validator" ],
|
|
8
|
+
"files": [ "dist" ],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/pykeio/zod-compiler"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"require": "./dist/index.js",
|
|
20
|
+
"import": "./dist/index.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./standalone": {
|
|
23
|
+
"types": "./dist/standalone.d.ts",
|
|
24
|
+
"require": "./dist/standalone.js",
|
|
25
|
+
"import": "./dist/standalone.mjs"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"prepublishOnly": "npm run build",
|
|
30
|
+
"build": "bunchee",
|
|
31
|
+
"test": "vitest run"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@swc/helpers": "^0.5.15"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"typescript": "^5",
|
|
38
|
+
"zod": "^3"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^22.14.0",
|
|
42
|
+
"bunchee": "^6.5.0",
|
|
43
|
+
"typescript": "^5.8.2",
|
|
44
|
+
"vitest": "^3.0.9",
|
|
45
|
+
"zod": "^3.24.2"
|
|
46
|
+
},
|
|
47
|
+
"packageManager": "pnpm@10.6.5",
|
|
48
|
+
"pnpm": {
|
|
49
|
+
"onlyBuiltDependencies": [
|
|
50
|
+
"@swc/core"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
}
|