zod 3.21.2 → 3.21.3
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 +52 -0
- package/lib/index.mjs +0 -8
- package/lib/index.umd.js +0 -8
- package/lib/types.d.ts +12 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2394,6 +2394,58 @@ z.string()
|
|
|
2394
2394
|
|
|
2395
2395
|
The `.pipe()` method returns a `ZodPipeline` instance.
|
|
2396
2396
|
|
|
2397
|
+
#### You can use `.pipe()` to fix common issues with `z.coerce`.
|
|
2398
|
+
|
|
2399
|
+
You can constrain the input to types that work well with your chosen coercion. Then use `.pipe()` to apply the coercion.
|
|
2400
|
+
|
|
2401
|
+
without constrained input:
|
|
2402
|
+
```ts
|
|
2403
|
+
const toDate = z.coerce.date()
|
|
2404
|
+
|
|
2405
|
+
// works intuitively
|
|
2406
|
+
console.log(toDate.safeParse('2023-01-01').success) // true
|
|
2407
|
+
|
|
2408
|
+
// might not be what you want
|
|
2409
|
+
console.log(toDate.safeParse(null).success) // true
|
|
2410
|
+
```
|
|
2411
|
+
|
|
2412
|
+
with constrained input:
|
|
2413
|
+
```ts
|
|
2414
|
+
const datelike = z.union([z.number(), z.string(), z.date()])
|
|
2415
|
+
const datelikeToDate = datelike.pipe(z.coerce.date())
|
|
2416
|
+
|
|
2417
|
+
// still works intuitively
|
|
2418
|
+
console.log(datelikeToDate.safeParse('2023-01-01').success) // true
|
|
2419
|
+
|
|
2420
|
+
// more likely what you want
|
|
2421
|
+
console.log(datelikeToDate.safeParse(null).success) // false
|
|
2422
|
+
```
|
|
2423
|
+
|
|
2424
|
+
You can also use this technique to avoid coercions that throw uncaught errors.
|
|
2425
|
+
|
|
2426
|
+
without constrained input:
|
|
2427
|
+
```ts
|
|
2428
|
+
const toBigInt = z.coerce.bigint()
|
|
2429
|
+
|
|
2430
|
+
// works intuitively
|
|
2431
|
+
console.log( toBigInt.safeParse( '42' ) ) // true
|
|
2432
|
+
|
|
2433
|
+
// probably not what you want
|
|
2434
|
+
console.log( toBigInt.safeParse( null ) ) // throws uncaught error
|
|
2435
|
+
```
|
|
2436
|
+
|
|
2437
|
+
with constrained input:
|
|
2438
|
+
```ts
|
|
2439
|
+
const toNumber = z.number().or( z.string() ).pipe( z.coerce.number() )
|
|
2440
|
+
const toBigInt = z.bigint().or( toNumber ).pipe( z.coerce.bigint() )
|
|
2441
|
+
|
|
2442
|
+
// still works intuitively
|
|
2443
|
+
console.log( toBigInt.safeParse( '42' ).success ) // true
|
|
2444
|
+
|
|
2445
|
+
// error handled by zod, more likely what you want
|
|
2446
|
+
console.log( toBigInt.safeParse( null ).success ) // false
|
|
2447
|
+
```
|
|
2448
|
+
|
|
2397
2449
|
## Guides and concepts
|
|
2398
2450
|
|
|
2399
2451
|
### Type inference
|
package/lib/index.mjs
CHANGED
|
@@ -3463,10 +3463,6 @@ class ZodEffects extends ZodType {
|
|
|
3463
3463
|
path: ctx.path,
|
|
3464
3464
|
parent: ctx,
|
|
3465
3465
|
});
|
|
3466
|
-
// if (base.status === "aborted") return INVALID;
|
|
3467
|
-
// if (base.status === "dirty") {
|
|
3468
|
-
// return { status: "dirty", value: base.value };
|
|
3469
|
-
// }
|
|
3470
3466
|
if (!isValid(base))
|
|
3471
3467
|
return base;
|
|
3472
3468
|
const result = effect.transform(base.value, checkCtx);
|
|
@@ -3481,10 +3477,6 @@ class ZodEffects extends ZodType {
|
|
|
3481
3477
|
.then((base) => {
|
|
3482
3478
|
if (!isValid(base))
|
|
3483
3479
|
return base;
|
|
3484
|
-
// if (base.status === "aborted") return INVALID;
|
|
3485
|
-
// if (base.status === "dirty") {
|
|
3486
|
-
// return { status: "dirty", value: base.value };
|
|
3487
|
-
// }
|
|
3488
3480
|
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
|
|
3489
3481
|
});
|
|
3490
3482
|
}
|
package/lib/index.umd.js
CHANGED
|
@@ -3469,10 +3469,6 @@
|
|
|
3469
3469
|
path: ctx.path,
|
|
3470
3470
|
parent: ctx,
|
|
3471
3471
|
});
|
|
3472
|
-
// if (base.status === "aborted") return INVALID;
|
|
3473
|
-
// if (base.status === "dirty") {
|
|
3474
|
-
// return { status: "dirty", value: base.value };
|
|
3475
|
-
// }
|
|
3476
3472
|
if (!isValid(base))
|
|
3477
3473
|
return base;
|
|
3478
3474
|
const result = effect.transform(base.value, checkCtx);
|
|
@@ -3487,10 +3483,6 @@
|
|
|
3487
3483
|
.then((base) => {
|
|
3488
3484
|
if (!isValid(base))
|
|
3489
3485
|
return base;
|
|
3490
|
-
// if (base.status === "aborted") return INVALID;
|
|
3491
|
-
// if (base.status === "dirty") {
|
|
3492
|
-
// return { status: "dirty", value: base.value };
|
|
3493
|
-
// }
|
|
3494
3486
|
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
|
|
3495
3487
|
});
|
|
3496
3488
|
}
|
package/lib/types.d.ts
CHANGED
|
@@ -477,9 +477,6 @@ export declare type PassthroughType<T extends UnknownKeysParam> = T extends "pas
|
|
|
477
477
|
} : unknown;
|
|
478
478
|
export declare type deoptional<T extends ZodTypeAny> = T extends ZodOptional<infer U> ? deoptional<U> : T extends ZodNullable<infer U> ? ZodNullable<deoptional<U>> : T;
|
|
479
479
|
export declare type SomeZodObject = ZodObject<ZodRawShape, UnknownKeysParam, ZodTypeAny>;
|
|
480
|
-
export declare type objectKeyMask<Obj> = {
|
|
481
|
-
[k in keyof Obj]?: true;
|
|
482
|
-
};
|
|
483
480
|
export declare type noUnrecognized<Obj extends object, Shape extends object> = {
|
|
484
481
|
[k in keyof Obj]: k extends keyof Shape ? Obj[k] : never;
|
|
485
482
|
};
|
|
@@ -502,19 +499,27 @@ export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends Unknow
|
|
|
502
499
|
[k in Key]: Schema;
|
|
503
500
|
}, UnknownKeys, Catchall>;
|
|
504
501
|
catchall<Index extends ZodTypeAny>(index: Index): ZodObject<T, UnknownKeys, Index>;
|
|
505
|
-
pick<Mask extends
|
|
506
|
-
|
|
502
|
+
pick<Mask extends {
|
|
503
|
+
[k in keyof T]?: true;
|
|
504
|
+
}>(mask: Mask): ZodObject<Pick<T, Extract<keyof T, keyof Mask>>, UnknownKeys, Catchall>;
|
|
505
|
+
omit<Mask extends {
|
|
506
|
+
[k in keyof T]?: true;
|
|
507
|
+
}>(mask: Mask): ZodObject<Omit<T, keyof Mask>, UnknownKeys, Catchall>;
|
|
507
508
|
deepPartial(): partialUtil.DeepPartial<this>;
|
|
508
509
|
partial(): ZodObject<{
|
|
509
510
|
[k in keyof T]: ZodOptional<T[k]>;
|
|
510
511
|
}, UnknownKeys, Catchall>;
|
|
511
|
-
partial<Mask extends
|
|
512
|
+
partial<Mask extends {
|
|
513
|
+
[k in keyof T]?: true;
|
|
514
|
+
}>(mask: Mask): ZodObject<objectUtil.noNever<{
|
|
512
515
|
[k in keyof T]: k extends keyof Mask ? ZodOptional<T[k]> : T[k];
|
|
513
516
|
}>, UnknownKeys, Catchall>;
|
|
514
517
|
required(): ZodObject<{
|
|
515
518
|
[k in keyof T]: deoptional<T[k]>;
|
|
516
519
|
}, UnknownKeys, Catchall>;
|
|
517
|
-
required<Mask extends
|
|
520
|
+
required<Mask extends {
|
|
521
|
+
[k in keyof T]?: true;
|
|
522
|
+
}>(mask: Mask): ZodObject<objectUtil.noNever<{
|
|
518
523
|
[k in keyof T]: k extends keyof Mask ? deoptional<T[k]> : T[k];
|
|
519
524
|
}>, UnknownKeys, Catchall>;
|
|
520
525
|
keyof(): ZodEnum<enumUtil.UnionToTupleString<keyof T>>;
|