@quentinadam/zod 0.1.7 → 0.1.9
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/dist/zod.d.ts +5 -2
- package/dist/zod.js +70 -14
- package/package.json +1 -1
package/dist/zod.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ type Result<T> = {
|
|
|
12
12
|
} | {
|
|
13
13
|
success: false;
|
|
14
14
|
};
|
|
15
|
-
export declare function
|
|
15
|
+
export declare function inspectValue(value: unknown): string;
|
|
16
16
|
export declare class Schema<T> {
|
|
17
17
|
protected readonly safeParseFn: (value: unknown, context?: Context) => Result<T>;
|
|
18
18
|
constructor(safeParseFn: (value: unknown, context?: Context) => Result<T>);
|
|
@@ -47,6 +47,9 @@ declare function createArraySchema<T>(schema: Schema<T>): Schema<T[]>;
|
|
|
47
47
|
declare function createBigIntSchema(): Schema<bigint>;
|
|
48
48
|
declare function createBooleanSchema(): Schema<boolean>;
|
|
49
49
|
declare function createDateSchema(): Schema<Date>;
|
|
50
|
+
declare function createDiscrimatedUnionSchema<B extends string, T extends Record<B, unknown>[]>(discriminator: B, schemas: {
|
|
51
|
+
[K in keyof T]: ObjectSchema<T[K]>;
|
|
52
|
+
}): Schema<T[number]>;
|
|
50
53
|
declare function createInstanceofSchema<T>(schema: {
|
|
51
54
|
new (...args: any[]): T;
|
|
52
55
|
}): Schema<T>;
|
|
@@ -73,4 +76,4 @@ declare function createUnionSchema<T extends unknown[]>(schemas: {
|
|
|
73
76
|
[K in keyof T]: Schema<T[K]>;
|
|
74
77
|
}): Schema<T[number]>;
|
|
75
78
|
declare function createUnknownSchema(): Schema<unknown>;
|
|
76
|
-
export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
|
|
79
|
+
export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createDiscrimatedUnionSchema as discriminatedUnion, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
|
package/dist/zod.js
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
|
-
export function
|
|
1
|
+
export function inspectValue(value) {
|
|
2
2
|
if (value === undefined)
|
|
3
3
|
return 'undefined';
|
|
4
4
|
if (value === null)
|
|
5
5
|
return 'null';
|
|
6
6
|
if (Array.isArray(value))
|
|
7
7
|
return 'array';
|
|
8
|
+
if (typeof value === 'string') {
|
|
9
|
+
return `string ${value.length > 32 ? JSON.stringify(value.slice(0, 32) + '...') : JSON.stringify(value)}`;
|
|
10
|
+
}
|
|
11
|
+
if (typeof value === 'number') {
|
|
12
|
+
return `number ${value}`;
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === 'bigint') {
|
|
15
|
+
return `bigint ${value.toString()}`;
|
|
16
|
+
}
|
|
17
|
+
if (typeof value === 'boolean') {
|
|
18
|
+
return `boolean ${value}`;
|
|
19
|
+
}
|
|
8
20
|
return typeof value;
|
|
9
21
|
}
|
|
10
22
|
export class Schema {
|
|
@@ -70,7 +82,7 @@ export class ObjectSchema extends Schema {
|
|
|
70
82
|
super((value, context) => {
|
|
71
83
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
72
84
|
if (context !== undefined) {
|
|
73
|
-
context.errors.push({ path: context.path, message: `Expected object, got ${
|
|
85
|
+
context.errors.push({ path: context.path, message: `Expected object, got ${inspectValue(value)}` });
|
|
74
86
|
}
|
|
75
87
|
return { success: false };
|
|
76
88
|
}
|
|
@@ -121,7 +133,7 @@ function createArraySchema(schema) {
|
|
|
121
133
|
return new Schema((value, context) => {
|
|
122
134
|
if (!Array.isArray(value)) {
|
|
123
135
|
if (context !== undefined) {
|
|
124
|
-
context.errors.push({ path: context.path, message: `Expected array, got ${
|
|
136
|
+
context.errors.push({ path: context.path, message: `Expected array, got ${inspectValue(value)}` });
|
|
125
137
|
}
|
|
126
138
|
return { success: false };
|
|
127
139
|
}
|
|
@@ -154,7 +166,7 @@ function createBigIntSchema() {
|
|
|
154
166
|
return new Schema((value, context) => {
|
|
155
167
|
if (typeof value !== 'bigint') {
|
|
156
168
|
if (context !== undefined) {
|
|
157
|
-
context.errors.push({ path: context.path, message: `Expected bigint, got ${
|
|
169
|
+
context.errors.push({ path: context.path, message: `Expected bigint, got ${inspectValue(value)}` });
|
|
158
170
|
}
|
|
159
171
|
return { success: false };
|
|
160
172
|
}
|
|
@@ -165,7 +177,7 @@ function createBooleanSchema() {
|
|
|
165
177
|
return new Schema((value, context) => {
|
|
166
178
|
if (typeof value !== 'boolean') {
|
|
167
179
|
if (context !== undefined) {
|
|
168
|
-
context.errors.push({ path: context.path, message: `Expected boolean, got ${
|
|
180
|
+
context.errors.push({ path: context.path, message: `Expected boolean, got ${inspectValue(value)}` });
|
|
169
181
|
}
|
|
170
182
|
return { success: false };
|
|
171
183
|
}
|
|
@@ -175,6 +187,44 @@ function createBooleanSchema() {
|
|
|
175
187
|
function createDateSchema() {
|
|
176
188
|
return createInstanceofSchema(Date);
|
|
177
189
|
}
|
|
190
|
+
function createDiscrimatedUnionSchema(discriminator, schemas) {
|
|
191
|
+
return new Schema((value, context) => {
|
|
192
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
193
|
+
if (context !== undefined) {
|
|
194
|
+
context.errors.push({
|
|
195
|
+
path: context.path,
|
|
196
|
+
message: `Expected object, got ${inspectValue(value)}`,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return { success: false };
|
|
200
|
+
}
|
|
201
|
+
const object = value;
|
|
202
|
+
const discriminatorValue = object[discriminator];
|
|
203
|
+
const discriminatedSchemas = schemas.filter((schema) => {
|
|
204
|
+
return schema.shape[discriminator].safeParse(discriminatorValue).success;
|
|
205
|
+
});
|
|
206
|
+
if (discriminatedSchemas.length > 1) {
|
|
207
|
+
if (context !== undefined) {
|
|
208
|
+
context.errors.push({
|
|
209
|
+
path: [...context.path, discriminator],
|
|
210
|
+
message: `Ambiguous discriminator value ${inspectValue(discriminatorValue)}`,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return { success: false };
|
|
214
|
+
}
|
|
215
|
+
const discriminatedSchema = discriminatedSchemas[0];
|
|
216
|
+
if (discriminatedSchema === undefined) {
|
|
217
|
+
if (context !== undefined) {
|
|
218
|
+
context.errors.push({
|
|
219
|
+
path: [...context.path, discriminator],
|
|
220
|
+
message: `Invalid discriminator value ${inspectValue(discriminatorValue)}`,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return { success: false };
|
|
224
|
+
}
|
|
225
|
+
return discriminatedSchema.internalSafeParse(value, context);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
178
228
|
// deno-lint-ignore no-explicit-any
|
|
179
229
|
function createInstanceofSchema(schema) {
|
|
180
230
|
return new Schema((value, context) => {
|
|
@@ -182,7 +232,7 @@ function createInstanceofSchema(schema) {
|
|
|
182
232
|
if (context !== undefined) {
|
|
183
233
|
context.errors.push({
|
|
184
234
|
path: context.path,
|
|
185
|
-
message: `Expected instance of ${schema.name}, got ${
|
|
235
|
+
message: `Expected instance of ${schema.name}, got ${inspectValue(value)}`,
|
|
186
236
|
});
|
|
187
237
|
}
|
|
188
238
|
return { success: false };
|
|
@@ -200,7 +250,10 @@ function createLiteralSchema(literal) {
|
|
|
200
250
|
return new Schema((value, context) => {
|
|
201
251
|
if (value !== literal) {
|
|
202
252
|
if (context !== undefined) {
|
|
203
|
-
context.errors.push({
|
|
253
|
+
context.errors.push({
|
|
254
|
+
path: context.path,
|
|
255
|
+
message: `Expected literal ${JSON.stringify(literal)}, got ${inspectValue(value)}`,
|
|
256
|
+
});
|
|
204
257
|
}
|
|
205
258
|
return { success: false };
|
|
206
259
|
}
|
|
@@ -223,7 +276,7 @@ function createNullSchema() {
|
|
|
223
276
|
return new Schema((value, context) => {
|
|
224
277
|
if (value !== null) {
|
|
225
278
|
if (context !== undefined) {
|
|
226
|
-
context.errors.push({ path: context.path, message: `Expected null, got ${
|
|
279
|
+
context.errors.push({ path: context.path, message: `Expected null, got ${inspectValue(value)}` });
|
|
227
280
|
}
|
|
228
281
|
return { success: false };
|
|
229
282
|
}
|
|
@@ -234,7 +287,7 @@ function createNumberSchema() {
|
|
|
234
287
|
return new Schema((value, context) => {
|
|
235
288
|
if (typeof value !== 'number') {
|
|
236
289
|
if (context !== undefined) {
|
|
237
|
-
context.errors.push({ path: context.path, message: `Expected number, got ${
|
|
290
|
+
context.errors.push({ path: context.path, message: `Expected number, got ${inspectValue(value)}` });
|
|
238
291
|
}
|
|
239
292
|
return { success: false };
|
|
240
293
|
}
|
|
@@ -244,7 +297,10 @@ function createNumberSchema() {
|
|
|
244
297
|
function createRecordSchema(schema) {
|
|
245
298
|
return new Schema((record, context) => {
|
|
246
299
|
if (typeof record !== 'object' || record === null || Array.isArray(record)) {
|
|
247
|
-
|
|
300
|
+
if (context !== undefined) {
|
|
301
|
+
context.errors.push({ path: context.path, message: `Expected object, got ${inspectValue(record)}` });
|
|
302
|
+
}
|
|
303
|
+
return { success: false };
|
|
248
304
|
}
|
|
249
305
|
const result = (() => {
|
|
250
306
|
const parsedObject = {};
|
|
@@ -275,7 +331,7 @@ function createStringSchema() {
|
|
|
275
331
|
return new Schema((value, context) => {
|
|
276
332
|
if (typeof value !== 'string') {
|
|
277
333
|
if (context !== undefined) {
|
|
278
|
-
context.errors.push({ path: context.path, message: `Expected string, got ${
|
|
334
|
+
context.errors.push({ path: context.path, message: `Expected string, got ${inspectValue(value)}` });
|
|
279
335
|
}
|
|
280
336
|
return { success: false };
|
|
281
337
|
}
|
|
@@ -286,7 +342,7 @@ function createTupleSchema(schema) {
|
|
|
286
342
|
return new Schema((value, context) => {
|
|
287
343
|
if (!Array.isArray(value)) {
|
|
288
344
|
if (context !== undefined) {
|
|
289
|
-
context.errors.push({ path: context.path, message: `Expected array, got ${
|
|
345
|
+
context.errors.push({ path: context.path, message: `Expected array, got ${inspectValue(value)}` });
|
|
290
346
|
}
|
|
291
347
|
return { success: false };
|
|
292
348
|
}
|
|
@@ -329,7 +385,7 @@ function createUndefinedSchema() {
|
|
|
329
385
|
return new Schema((value, context) => {
|
|
330
386
|
if (value !== undefined) {
|
|
331
387
|
if (context !== undefined) {
|
|
332
|
-
context.errors.push({ path: context.path, message: `Expected undefined, got ${
|
|
388
|
+
context.errors.push({ path: context.path, message: `Expected undefined, got ${inspectValue(value)}` });
|
|
333
389
|
}
|
|
334
390
|
return { success: false };
|
|
335
391
|
}
|
|
@@ -357,4 +413,4 @@ function createUnknownSchema() {
|
|
|
357
413
|
return { success: true, data: value };
|
|
358
414
|
});
|
|
359
415
|
}
|
|
360
|
-
export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
|
|
416
|
+
export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createDiscrimatedUnionSchema as discriminatedUnion, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
|