zod 3.19.0 → 3.19.1

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 CHANGED
@@ -604,7 +604,7 @@ dateSchema.safeParse(new Date("1/12/22")); // success: true
604
604
  dateSchema.safeParse("2022-01-12T00:00:00.000Z"); // success: true
605
605
  ```
606
606
 
607
- ## Zod enums-
607
+ ## Zod enums
608
608
 
609
609
  ```ts
610
610
  const FishEnum = z.enum(["Salmon", "Tuna", "Trout"]);
@@ -1730,8 +1730,6 @@ const stringToNumber = z.string().transform((val) => val.length);
1730
1730
  stringToNumber.parse("string"); // => 6
1731
1731
  ```
1732
1732
 
1733
- > ⚠️ Transform functions must not throw. Make sure to use refinements before the transform or addIssue within the transform to make sure the input can be parsed by the transform.
1734
-
1735
1733
  #### Chaining order
1736
1734
 
1737
1735
  Note that `stringToNumber` above is an instance of the `ZodEffects` subclass. It is NOT an instance of `ZodString`. If you want to use the built-in methods of `ZodString` (e.g. `.email()`) you must apply those methods _before_ any transforms.
@@ -1747,7 +1745,9 @@ emailToDomain.parse("colinhacks@example.com"); // => example.com
1747
1745
 
1748
1746
  #### Validating during transform
1749
1747
 
1750
- Similar to `superRefine`, `transform` can optionally take a `ctx`. This allows you to simultaneously validate and transform the value, which can be simpler than chaining `refine` and `validate`. When calling `ctx.addIssue` make sure to still return a value of the correct type otherwise the inferred type will include `undefined`.
1748
+ The `.transform` method can simultaneously validate and transform the value. This is often simpler and less duplicative than chaining `refine` and `validate`.
1749
+
1750
+ As with `.superRefine`, the transform function receives a `ctx` object with a `addIssue` method that can be used to register validation issues.
1751
1751
 
1752
1752
  ```ts
1753
1753
  const Strings = z.string().transform((val, ctx) => {
package/lib/index.mjs CHANGED
@@ -1607,9 +1607,12 @@ class ZodObject extends ZodType {
1607
1607
  const { status, ctx } = this._processInputParams(input);
1608
1608
  const { shape, keys: shapeKeys } = this._getCached();
1609
1609
  const extraKeys = [];
1610
- for (const key in ctx.data) {
1611
- if (!shapeKeys.includes(key)) {
1612
- extraKeys.push(key);
1610
+ if (!(this._def.catchall instanceof ZodNever &&
1611
+ this._def.unknownKeys === "strip")) {
1612
+ for (const key in ctx.data) {
1613
+ if (!shapeKeys.includes(key)) {
1614
+ extraKeys.push(key);
1615
+ }
1613
1616
  }
1614
1617
  }
1615
1618
  const pairs = [];
package/lib/index.umd.js CHANGED
@@ -1613,9 +1613,12 @@
1613
1613
  const { status, ctx } = this._processInputParams(input);
1614
1614
  const { shape, keys: shapeKeys } = this._getCached();
1615
1615
  const extraKeys = [];
1616
- for (const key in ctx.data) {
1617
- if (!shapeKeys.includes(key)) {
1618
- extraKeys.push(key);
1616
+ if (!(this._def.catchall instanceof ZodNever &&
1617
+ this._def.unknownKeys === "strip")) {
1618
+ for (const key in ctx.data) {
1619
+ if (!shapeKeys.includes(key)) {
1620
+ extraKeys.push(key);
1621
+ }
1619
1622
  }
1620
1623
  }
1621
1624
  const pairs = [];
package/lib/types.d.ts CHANGED
@@ -58,9 +58,9 @@ export declare abstract class ZodType<Output = any, Def extends ZodTypeDef = Zod
58
58
  safeParseAsync(data: unknown, params?: Partial<ParseParams>): Promise<SafeParseReturnType<Input, Output>>;
59
59
  /** Alias of safeParseAsync */
60
60
  spa: (data: unknown, params?: Partial<ParseParams> | undefined) => Promise<SafeParseReturnType<Input, Output>>;
61
- refine<RefinedOutput extends Output>(check: (arg: Output) => arg is RefinedOutput, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)): ZodEffects<this, RefinedOutput, RefinedOutput>;
61
+ refine<RefinedOutput extends Output>(check: (arg: Output) => arg is RefinedOutput, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)): ZodEffects<this, RefinedOutput, Input>;
62
62
  refine(check: (arg: Output) => unknown | Promise<unknown>, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)): ZodEffects<this, Output, Input>;
63
- refinement<RefinedOutput extends Output>(check: (arg: Output) => arg is RefinedOutput, refinementData: IssueData | ((arg: Output, ctx: RefinementCtx) => IssueData)): ZodEffects<this, RefinedOutput, RefinedOutput>;
63
+ refinement<RefinedOutput extends Output>(check: (arg: Output) => arg is RefinedOutput, refinementData: IssueData | ((arg: Output, ctx: RefinementCtx) => IssueData)): ZodEffects<this, RefinedOutput, Input>;
64
64
  refinement(check: (arg: Output) => boolean, refinementData: IssueData | ((arg: Output, ctx: RefinementCtx) => IssueData)): ZodEffects<this, Output, Input>;
65
65
  _refinement(refinement: RefinementEffect<Output>["refinement"]): ZodEffects<this, Output, Input>;
66
66
  superRefine: (refinement: RefinementEffect<Output>["refinement"]) => ZodEffects<this, Output, Input>;
@@ -631,7 +631,7 @@ export declare class ZodEffects<T extends ZodTypeAny, Output = T["_output"], Inp
631
631
  innerType(): T;
632
632
  _parse(input: ParseInput): ParseReturnType<this["_output"]>;
633
633
  static create: <I extends ZodTypeAny>(schema: I, effect: Effect<I["_output"]>, params?: RawCreateParams) => ZodEffects<I, I["_output"], I["_input"]>;
634
- static createWithPreprocess: <I extends ZodTypeAny>(preprocess: (arg: unknown) => unknown, schema: I, params?: RawCreateParams) => ZodEffects<I, I["_output"], I["_input"]>;
634
+ static createWithPreprocess: <I extends ZodTypeAny>(preprocess: (arg: unknown) => unknown, schema: I, params?: RawCreateParams) => ZodEffects<I, I["_output"], unknown>;
635
635
  }
636
636
  export { ZodEffects as ZodTransformer };
637
637
  export interface ZodOptionalDef<T extends ZodTypeAny = ZodTypeAny> extends ZodTypeDef {
@@ -757,7 +757,7 @@ declare const promiseType: <T extends ZodTypeAny>(schema: T, params?: RawCreateP
757
757
  declare const effectsType: <I extends ZodTypeAny>(schema: I, effect: Effect<I["_output"]>, params?: RawCreateParams) => ZodEffects<I, I["_output"], I["_input"]>;
758
758
  declare const optionalType: <T extends ZodTypeAny>(type: T, params?: RawCreateParams) => ZodOptional<T>;
759
759
  declare const nullableType: <T extends ZodTypeAny>(type: T, params?: RawCreateParams) => ZodNullable<T>;
760
- declare const preprocessType: <I extends ZodTypeAny>(preprocess: (arg: unknown) => unknown, schema: I, params?: RawCreateParams) => ZodEffects<I, I["_output"], I["_input"]>;
760
+ declare const preprocessType: <I extends ZodTypeAny>(preprocess: (arg: unknown) => unknown, schema: I, params?: RawCreateParams) => ZodEffects<I, I["_output"], unknown>;
761
761
  declare const ostring: () => ZodOptional<ZodString>;
762
762
  declare const onumber: () => ZodOptional<ZodNumber>;
763
763
  declare const oboolean: () => ZodOptional<ZodBoolean>;
package/lib/types.js CHANGED
@@ -1171,9 +1171,12 @@ class ZodObject extends ZodType {
1171
1171
  const { status, ctx } = this._processInputParams(input);
1172
1172
  const { shape, keys: shapeKeys } = this._getCached();
1173
1173
  const extraKeys = [];
1174
- for (const key in ctx.data) {
1175
- if (!shapeKeys.includes(key)) {
1176
- extraKeys.push(key);
1174
+ if (!(this._def.catchall instanceof ZodNever &&
1175
+ this._def.unknownKeys === "strip")) {
1176
+ for (const key in ctx.data) {
1177
+ if (!shapeKeys.includes(key)) {
1178
+ extraKeys.push(key);
1179
+ }
1177
1180
  }
1178
1181
  }
1179
1182
  const pairs = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.19.0",
3
+ "version": "3.19.1",
4
4
  "description": "TypeScript-first schema declaration and validation library with static type inference",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./index.d.ts",