zod 3.17.8 → 3.17.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/README.md CHANGED
@@ -63,6 +63,7 @@
63
63
  - [Nullables](#nullables)
64
64
  - [Objects](#objects)
65
65
  - [.shape](#shape)
66
+ - [.enum](#enum)
66
67
  - [.extend](#extend)
67
68
  - [.merge](#merge)
68
69
  - [.pick/.omit](#pickomit)
@@ -782,6 +783,15 @@ Dog.shape.name; // => string schema
782
783
  Dog.shape.age; // => number schema
783
784
  ```
784
785
 
786
+ ### `.keyof`
787
+
788
+ Use `.key` to create a `ZodEnum` schema from the keys of an object schema.
789
+
790
+ ```ts
791
+ const keySchema = Dog.keyof();
792
+ keySchema; // ZodEnum<["name", "age"]>
793
+ ```
794
+
785
795
  ### `.extend`
786
796
 
787
797
  You can add additional fields to an object schema with the `.extend` method.
@@ -1790,7 +1800,7 @@ z.optional(z.string());
1790
1800
 
1791
1801
  ### `.nullable`
1792
1802
 
1793
- A convenience method that returns an nullable version of a schema.
1803
+ A convenience method that returns a nullable version of a schema.
1794
1804
 
1795
1805
  ```ts
1796
1806
  const nullableString = z.string().nullable(); // string | null
@@ -0,0 +1,8 @@
1
+ export declare namespace enumUtil {
2
+ type UnionToIntersectionFn<T> = (T extends unknown ? (k: () => T) => void : never) extends (k: infer Intersection) => void ? Intersection : never;
3
+ type GetUnionLast<T> = UnionToIntersectionFn<T> extends () => infer Last ? Last : never;
4
+ type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never] ? Tuple : UnionToTuple<Exclude<T, GetUnionLast<T>>, [GetUnionLast<T>, ...Tuple]>;
5
+ type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never;
6
+ export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>;
7
+ export {};
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/index.mjs CHANGED
@@ -490,11 +490,10 @@ function processCreateParams(params) {
490
490
  const customMap = (iss, ctx) => {
491
491
  if (iss.code !== "invalid_type")
492
492
  return { message: ctx.defaultError };
493
- if (typeof ctx.data === "undefined" && required_error)
494
- return { message: required_error };
495
- if (params.invalid_type_error)
496
- return { message: params.invalid_type_error };
497
- return { message: ctx.defaultError };
493
+ if (typeof ctx.data === "undefined") {
494
+ return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
495
+ }
496
+ return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
498
497
  };
499
498
  return { errorMap: customMap, description };
500
499
  }
@@ -1805,6 +1804,9 @@ class ZodObject extends ZodType {
1805
1804
  shape: () => newShape,
1806
1805
  });
1807
1806
  }
1807
+ keyof() {
1808
+ return createZodEnum(util.objectKeys(this.shape));
1809
+ }
1808
1810
  }
1809
1811
  ZodObject.create = (shape, params) => {
1810
1812
  return new ZodObject({
package/lib/index.umd.js CHANGED
@@ -496,11 +496,10 @@
496
496
  const customMap = (iss, ctx) => {
497
497
  if (iss.code !== "invalid_type")
498
498
  return { message: ctx.defaultError };
499
- if (typeof ctx.data === "undefined" && required_error)
500
- return { message: required_error };
501
- if (params.invalid_type_error)
502
- return { message: params.invalid_type_error };
503
- return { message: ctx.defaultError };
499
+ if (typeof ctx.data === "undefined") {
500
+ return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
501
+ }
502
+ return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
504
503
  };
505
504
  return { errorMap: customMap, description };
506
505
  }
@@ -1811,6 +1810,9 @@
1811
1810
  shape: () => newShape,
1812
1811
  });
1813
1812
  }
1813
+ keyof() {
1814
+ return createZodEnum(util.objectKeys(this.shape));
1815
+ }
1814
1816
  }
1815
1817
  ZodObject.create = (shape, params) => {
1816
1818
  return new ZodObject({
package/lib/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { enumUtil } from "./helpers/enumUtil";
1
2
  import { errorUtil } from "./helpers/errorUtil";
2
3
  import { AsyncParseReturnType, ParseContext, ParseInput, ParseParams, ParseReturnType, ParseStatus, SyncParseReturnType } from "./helpers/parseUtil";
3
4
  import { partialUtil } from "./helpers/partialUtil";
@@ -400,6 +401,7 @@ export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends Unknow
400
401
  required(): ZodObject<{
401
402
  [k in keyof T]: deoptional<T[k]>;
402
403
  }, UnknownKeys, Catchall>;
404
+ keyof(): ZodEnum<enumUtil.UnionToTupleString<keyof T>>;
403
405
  static create: <T_1 extends ZodRawShape>(shape: T_1, params?: RawCreateParams) => ZodObject<T_1, "strip", ZodTypeAny, { [k_1 in keyof objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>]: objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>[k_1]; }, { [k_3 in keyof objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>]: objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>[k_3]; }>;
404
406
  static strictCreate: <T_1 extends ZodRawShape>(shape: T_1, params?: RawCreateParams) => ZodObject<T_1, "strict", ZodTypeAny, { [k_1 in keyof objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>]: objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>[k_1]; }, { [k_3 in keyof objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>]: objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>[k_3]; }>;
405
407
  static lazycreate: <T_1 extends ZodRawShape>(shape: () => T_1, params?: RawCreateParams) => ZodObject<T_1, "strip", ZodTypeAny, { [k_1 in keyof objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>]: objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>[k_1]; }, { [k_3 in keyof objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>]: objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>[k_3]; }>;
@@ -474,7 +476,7 @@ export declare class ZodTuple<T extends [ZodTypeAny, ...ZodTypeAny[]] | [] = [Zo
474
476
  _parse(input: ParseInput): ParseReturnType<this["_output"]>;
475
477
  get items(): T;
476
478
  rest<Rest extends ZodTypeAny>(rest: Rest): ZodTuple<T, Rest>;
477
- static create: <T_1 extends [ZodTypeAny, ...ZodTypeAny[]] | []>(schemas: T_1, params?: RawCreateParams) => ZodTuple<T_1, null>;
479
+ static create: <T_1 extends [] | [ZodTypeAny, ...ZodTypeAny[]]>(schemas: T_1, params?: RawCreateParams) => ZodTuple<T_1, null>;
478
480
  }
479
481
  export interface ZodRecordDef<Key extends KeySchema = ZodString, Value extends ZodTypeAny = ZodTypeAny> extends ZodTypeDef {
480
482
  valueType: Value;
@@ -722,7 +724,7 @@ declare const strictObjectType: <T extends ZodRawShape>(shape: T, params?: RawCr
722
724
  declare const unionType: <T extends readonly [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]>(types: T, params?: RawCreateParams) => ZodUnion<T>;
723
725
  declare const discriminatedUnionType: typeof ZodDiscriminatedUnion.create;
724
726
  declare const intersectionType: <T extends ZodTypeAny, U extends ZodTypeAny>(left: T, right: U, params?: RawCreateParams) => ZodIntersection<T, U>;
725
- declare const tupleType: <T extends [ZodTypeAny, ...ZodTypeAny[]] | []>(schemas: T, params?: RawCreateParams) => ZodTuple<T, null>;
727
+ declare const tupleType: <T extends [] | [ZodTypeAny, ...ZodTypeAny[]]>(schemas: T, params?: RawCreateParams) => ZodTuple<T, null>;
726
728
  declare const recordType: typeof ZodRecord.create;
727
729
  declare const mapType: <Key extends ZodTypeAny = ZodTypeAny, Value extends ZodTypeAny = ZodTypeAny>(keyType: Key, valueType: Value, params?: RawCreateParams) => ZodMap<Key, Value>;
728
730
  declare const setType: <Value extends ZodTypeAny = ZodTypeAny>(valueType: Value, params?: RawCreateParams) => ZodSet<Value>;
package/lib/types.js CHANGED
@@ -41,11 +41,10 @@ function processCreateParams(params) {
41
41
  const customMap = (iss, ctx) => {
42
42
  if (iss.code !== "invalid_type")
43
43
  return { message: ctx.defaultError };
44
- if (typeof ctx.data === "undefined" && required_error)
45
- return { message: required_error };
46
- if (params.invalid_type_error)
47
- return { message: params.invalid_type_error };
48
- return { message: ctx.defaultError };
44
+ if (typeof ctx.data === "undefined") {
45
+ return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
46
+ }
47
+ return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
49
48
  };
50
49
  return { errorMap: customMap, description };
51
50
  }
@@ -1372,6 +1371,9 @@ class ZodObject extends ZodType {
1372
1371
  shape: () => newShape,
1373
1372
  });
1374
1373
  }
1374
+ keyof() {
1375
+ return createZodEnum(util_1.util.objectKeys(this.shape));
1376
+ }
1375
1377
  }
1376
1378
  exports.ZodObject = ZodObject;
1377
1379
  ZodObject.create = (shape, params) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.17.8",
3
+ "version": "3.17.9",
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",