diff --git a/deno/lib/ZodError.ts b/deno/lib/ZodError.ts index 4d1ab5679..53f22579f 100644 --- a/deno/lib/ZodError.ts +++ b/deno/lib/ZodError.ts @@ -1,6 +1,7 @@ import type { TypeOf, ZodType } from "./index.ts"; +import { util } from "./helpers/index.ts"; import { Primitive } from "./helpers/typeAliases.ts"; -import { util, ZodParsedType } from "./helpers/util.ts"; +import { ZodParsedType } from "./helpers/util.ts"; type allKeys = T extends any ? keyof T : never; @@ -276,10 +277,10 @@ export class ZodError extends Error { return fieldErrors; } - static create = (issues: ZodIssue[]) => { + static create(issues: ZodIssue[]) { const error = new ZodError(issues); return error; - }; + } static assert(value: unknown): asserts value is ZodError { if (!(value instanceof ZodError)) { diff --git a/deno/lib/__tests__/all-errors.test.ts b/deno/lib/__tests__/all-errors.test.ts index 7e69248ab..adb71a34c 100644 --- a/deno/lib/__tests__/all-errors.test.ts +++ b/deno/lib/__tests__/all-errors.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; const Test = z.object({ diff --git a/deno/lib/__tests__/anyunknown.test.ts b/deno/lib/__tests__/anyunknown.test.ts index 439aa8860..9c1f2d9a9 100644 --- a/deno/lib/__tests__/anyunknown.test.ts +++ b/deno/lib/__tests__/anyunknown.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("check any inference", () => { diff --git a/deno/lib/__tests__/array.test.ts b/deno/lib/__tests__/array.test.ts index 31594670c..7dd2ca8f5 100644 --- a/deno/lib/__tests__/array.test.ts +++ b/deno/lib/__tests__/array.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; const minTwo = z.string().array().min(2); diff --git a/deno/lib/__tests__/base.test.ts b/deno/lib/__tests__/base.test.ts index beda584d7..3e984f202 100644 --- a/deno/lib/__tests__/base.test.ts +++ b/deno/lib/__tests__/base.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("type guard", () => { diff --git a/deno/lib/__tests__/branded.test.ts b/deno/lib/__tests__/branded.test.ts index ff663ef66..f03a67b18 100644 --- a/deno/lib/__tests__/branded.test.ts +++ b/deno/lib/__tests__/branded.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("branded types", () => { diff --git a/deno/lib/__tests__/catch.test.ts b/deno/lib/__tests__/catch.test.ts index f8ea89911..845c41739 100644 --- a/deno/lib/__tests__/catch.test.ts +++ b/deno/lib/__tests__/catch.test.ts @@ -3,7 +3,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; import { z } from "../index.ts"; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; test("basic catch", () => { expect(z.string().catch("default").parse(undefined)).toBe("default"); diff --git a/deno/lib/__tests__/default.test.ts b/deno/lib/__tests__/default.test.ts index 6ad12cae7..a31ece465 100644 --- a/deno/lib/__tests__/default.test.ts +++ b/deno/lib/__tests__/default.test.ts @@ -3,7 +3,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; import { z } from "../index.ts"; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; test("basic defaults", () => { expect(z.string().default("default").parse(undefined)).toBe("default"); diff --git a/deno/lib/__tests__/enum.test.ts b/deno/lib/__tests__/enum.test.ts index 819d3264f..27b9d6a47 100644 --- a/deno/lib/__tests__/enum.test.ts +++ b/deno/lib/__tests__/enum.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("create enum", () => { diff --git a/deno/lib/__tests__/error.test.ts b/deno/lib/__tests__/error.test.ts index e66b185eb..23cbd121c 100644 --- a/deno/lib/__tests__/error.test.ts +++ b/deno/lib/__tests__/error.test.ts @@ -2,7 +2,6 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { ZodParsedType } from "../helpers/util.ts"; import * as z from "../index.ts"; import { ZodError, ZodIssueCode } from "../ZodError.ts"; @@ -10,8 +9,8 @@ test("error creation", () => { const err1 = ZodError.create([]); err1.addIssue({ code: ZodIssueCode.invalid_type, - expected: ZodParsedType.object, - received: ZodParsedType.string, + expected: z.ZodParsedType.object, + received: z.ZodParsedType.string, path: [], message: "", fatal: true, diff --git a/deno/lib/__tests__/firstparty.test.ts b/deno/lib/__tests__/firstparty.test.ts index 5df8a71bd..c88108ef4 100644 --- a/deno/lib/__tests__/firstparty.test.ts +++ b/deno/lib/__tests__/firstparty.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("first party switch", () => { diff --git a/deno/lib/__tests__/firstpartyschematypes.test.ts b/deno/lib/__tests__/firstpartyschematypes.test.ts index 96e78d796..bd403de82 100644 --- a/deno/lib/__tests__/firstpartyschematypes.test.ts +++ b/deno/lib/__tests__/firstpartyschematypes.test.ts @@ -3,7 +3,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; import { ZodFirstPartySchemaTypes, ZodFirstPartyTypeKind } from "../index.ts"; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; test("Identify missing [ZodFirstPartySchemaTypes]", () => { type ZodFirstPartySchemaForType = diff --git a/deno/lib/__tests__/function.test.ts b/deno/lib/__tests__/function.test.ts index 675891e6d..e2e02921b 100644 --- a/deno/lib/__tests__/function.test.ts +++ b/deno/lib/__tests__/function.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; const args1 = z.tuple([z.string()]); diff --git a/deno/lib/__tests__/generics.test.ts b/deno/lib/__tests__/generics.test.ts index 4b0763fe5..147d716be 100644 --- a/deno/lib/__tests__/generics.test.ts +++ b/deno/lib/__tests__/generics.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("generics", () => { diff --git a/deno/lib/__tests__/instanceof.test.ts b/deno/lib/__tests__/instanceof.test.ts index 2f1f9187c..8057ed040 100644 --- a/deno/lib/__tests__/instanceof.test.ts +++ b/deno/lib/__tests__/instanceof.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("instanceof", async () => { diff --git a/deno/lib/__tests__/map.test.ts b/deno/lib/__tests__/map.test.ts index 9a2a6d783..1186bbd8d 100644 --- a/deno/lib/__tests__/map.test.ts +++ b/deno/lib/__tests__/map.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; import { ZodIssueCode } from "../index.ts"; diff --git a/deno/lib/__tests__/nativeEnum.test.ts b/deno/lib/__tests__/nativeEnum.test.ts index a8bdea148..a41576d14 100644 --- a/deno/lib/__tests__/nativeEnum.test.ts +++ b/deno/lib/__tests__/nativeEnum.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("nativeEnum test with consts", () => { diff --git a/deno/lib/__tests__/object.test.ts b/deno/lib/__tests__/object.test.ts index ff636a078..983aedf32 100644 --- a/deno/lib/__tests__/object.test.ts +++ b/deno/lib/__tests__/object.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; const Test = z.object({ diff --git a/deno/lib/__tests__/partials.test.ts b/deno/lib/__tests__/partials.test.ts index 1825753a9..e6f0ca3b1 100644 --- a/deno/lib/__tests__/partials.test.ts +++ b/deno/lib/__tests__/partials.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; import { ZodNullable, ZodOptional } from "../index.ts"; diff --git a/deno/lib/__tests__/pickomit.test.ts b/deno/lib/__tests__/pickomit.test.ts index db973267a..297ddbb74 100644 --- a/deno/lib/__tests__/pickomit.test.ts +++ b/deno/lib/__tests__/pickomit.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; const fish = z.object({ diff --git a/deno/lib/__tests__/preprocess.test.ts b/deno/lib/__tests__/preprocess.test.ts index 50ac02e05..5def7a01f 100644 --- a/deno/lib/__tests__/preprocess.test.ts +++ b/deno/lib/__tests__/preprocess.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("preprocess", () => { diff --git a/deno/lib/__tests__/primitive.test.ts b/deno/lib/__tests__/primitive.test.ts index 5815f765e..dff496792 100644 --- a/deno/lib/__tests__/primitive.test.ts +++ b/deno/lib/__tests__/primitive.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; import { Mocker } from "./Mocker.ts"; diff --git a/deno/lib/__tests__/promise.test.ts b/deno/lib/__tests__/promise.test.ts index 383de22f1..5103b5465 100644 --- a/deno/lib/__tests__/promise.test.ts +++ b/deno/lib/__tests__/promise.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; const promSchema = z.promise( diff --git a/deno/lib/__tests__/readonly.test.ts b/deno/lib/__tests__/readonly.test.ts index 27d2e0c9c..52e64164b 100644 --- a/deno/lib/__tests__/readonly.test.ts +++ b/deno/lib/__tests__/readonly.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; enum testEnum { diff --git a/deno/lib/__tests__/record.test.ts b/deno/lib/__tests__/record.test.ts index 311f805a7..908c2c044 100644 --- a/deno/lib/__tests__/record.test.ts +++ b/deno/lib/__tests__/record.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; const booleanRecord = z.record(z.boolean()); diff --git a/deno/lib/__tests__/refine.test.ts b/deno/lib/__tests__/refine.test.ts index f505fdbb7..f4d3c16ba 100644 --- a/deno/lib/__tests__/refine.test.ts +++ b/deno/lib/__tests__/refine.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; import { ZodIssueCode } from "../ZodError.ts"; diff --git a/deno/lib/__tests__/set.test.ts b/deno/lib/__tests__/set.test.ts index 0626bac94..6abf2b428 100644 --- a/deno/lib/__tests__/set.test.ts +++ b/deno/lib/__tests__/set.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; import { ZodIssueCode } from "../index.ts"; diff --git a/deno/lib/__tests__/transformer.test.ts b/deno/lib/__tests__/transformer.test.ts index 108b12e91..043645acc 100644 --- a/deno/lib/__tests__/transformer.test.ts +++ b/deno/lib/__tests__/transformer.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; const stringToNumber = z.string().transform((arg) => parseFloat(arg)); diff --git a/deno/lib/__tests__/tuple.test.ts b/deno/lib/__tests__/tuple.test.ts index 77d1845c0..69c1fd2d6 100644 --- a/deno/lib/__tests__/tuple.test.ts +++ b/deno/lib/__tests__/tuple.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; import { ZodError } from "../ZodError.ts"; diff --git a/deno/lib/__tests__/void.test.ts b/deno/lib/__tests__/void.test.ts index d6e5eeed1..a269e6e31 100644 --- a/deno/lib/__tests__/void.test.ts +++ b/deno/lib/__tests__/void.test.ts @@ -2,7 +2,7 @@ import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; const test = Deno.test; -import { util } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; import * as z from "../index.ts"; test("void", () => { const v = z.void(); diff --git a/deno/lib/coerce.ts b/deno/lib/coerce.ts new file mode 100644 index 000000000..b329318e3 --- /dev/null +++ b/deno/lib/coerce.ts @@ -0,0 +1,23 @@ +import { ZodBigInt, ZodBoolean, ZodDate, ZodNumber, ZodString } from "./types.ts"; + +const coerceString = ((arg) => + ZodString.create({ ...arg, coerce: true })) as (typeof ZodString)["create"]; +const coerceNumber = ((arg) => + ZodNumber.create({ ...arg, coerce: true })) as (typeof ZodNumber)["create"]; +const coerceBoolean = ((arg) => + ZodBoolean.create({ + ...arg, + coerce: true, + })) as (typeof ZodBoolean)["create"]; +const coerceBigint = ((arg) => + ZodBigInt.create({ ...arg, coerce: true })) as (typeof ZodBigInt)["create"]; +const coerceDate = ((arg) => + ZodDate.create({ ...arg, coerce: true })) as (typeof ZodDate)["create"]; + +export { + coerceBigint as bigint, + coerceBoolean as boolean, + coerceDate as date, + coerceNumber as number, + coerceString as string, +}; diff --git a/deno/lib/external.ts b/deno/lib/external.ts index b2070f81c..46243a016 100644 --- a/deno/lib/external.ts +++ b/deno/lib/external.ts @@ -1,6 +1,6 @@ export * from "./errors.ts"; +export * from "./helpers/index.ts"; export * from "./helpers/parseUtil.ts"; export * from "./helpers/typeAliases.ts"; -export * from "./helpers/util.ts"; export * from "./types.ts"; export * from "./ZodError.ts"; diff --git a/deno/lib/helpers/enumUtil.ts b/deno/lib/helpers/enumUtil.ts index 205d26664..516f494de 100644 --- a/deno/lib/helpers/enumUtil.ts +++ b/deno/lib/helpers/enumUtil.ts @@ -1,19 +1,17 @@ -export namespace enumUtil { - type UnionToIntersectionFn = ( - T extends unknown ? (k: () => T) => void : never - ) extends (k: infer Intersection) => void - ? Intersection - : never; +type UnionToIntersectionFn = ( + T extends unknown ? (k: () => T) => void : never +) extends (k: infer Intersection) => void + ? Intersection + : never; - type GetUnionLast = UnionToIntersectionFn extends () => infer Last - ? Last - : never; +type GetUnionLast = UnionToIntersectionFn extends () => infer Last + ? Last + : never; - type UnionToTuple = [T] extends [never] - ? Tuple - : UnionToTuple>, [GetUnionLast, ...Tuple]>; +type UnionToTuple = [T] extends [never] + ? Tuple + : UnionToTuple>, [GetUnionLast, ...Tuple]>; - type CastToStringTuple = T extends [string, ...string[]] ? T : never; +type CastToStringTuple = T extends [string, ...string[]] ? T : never; - export type UnionToTupleString = CastToStringTuple>; -} +export type UnionToTupleString = CastToStringTuple>; diff --git a/deno/lib/helpers/errorUtil.ts b/deno/lib/helpers/errorUtil.ts index 8d9afa2a8..9796f4fb3 100644 --- a/deno/lib/helpers/errorUtil.ts +++ b/deno/lib/helpers/errorUtil.ts @@ -1,7 +1,5 @@ -export namespace errorUtil { - export type ErrMessage = string | { message?: string }; - export const errToObj = (message?: ErrMessage) => - typeof message === "string" ? { message } : message || {}; - export const toString = (message?: ErrMessage): string | undefined => - typeof message === "string" ? message : message?.message; -} +export type ErrMessage = string | { message?: string }; +export const errToObj = (message?: ErrMessage) => + typeof message === "string" ? { message } : message || {}; +export const toString = (message?: ErrMessage): string | undefined => + typeof message === "string" ? message : message?.message; diff --git a/deno/lib/helpers/index.ts b/deno/lib/helpers/index.ts new file mode 100644 index 000000000..d745ccef3 --- /dev/null +++ b/deno/lib/helpers/index.ts @@ -0,0 +1,5 @@ +export * as enumUtil from "./enumUtil.ts"; +export * as errorUtil from "./errorUtil.ts"; +export * as objectUtil from "./objectUtil.ts"; +export * as partialUtil from "./partialUtil.ts"; +export * as util from "./util.ts"; diff --git a/deno/lib/helpers/objectUtil.ts b/deno/lib/helpers/objectUtil.ts new file mode 100644 index 000000000..05d61d06e --- /dev/null +++ b/deno/lib/helpers/objectUtil.ts @@ -0,0 +1,37 @@ +export type optionalKeys = { + [k in keyof T]: undefined extends T[k] ? k : never; +}[keyof T]; +export type requiredKeys = { + [k in keyof T]: undefined extends T[k] ? never : k; +}[keyof T]; +export type addQuestionMarks = { + [K in requiredKeys]: T[K]; +} & { + [K in optionalKeys]?: T[K]; +} & { [k in keyof T]?: unknown }; + +export type identity = T; +export type flatten = identity<{ [k in keyof T]: T[k] }>; + +export type noNeverKeys = { + [k in keyof T]: [T[k]] extends [never] ? never : k; +}[keyof T]; + +export type noNever = identity<{ + [k in noNeverKeys]: k extends keyof T ? T[k] : never; +}>; + +export const mergeShapes = (first: U, second: T): T & U => { + return { + ...first, + ...second, // second overwrites first + }; +}; + +export type extendShape = { + [K in keyof A | keyof B]: K extends keyof B + ? B[K] + : K extends keyof A + ? A[K] + : never; +}; diff --git a/deno/lib/helpers/parseUtil.ts b/deno/lib/helpers/parseUtil.ts index 53d39d626..ec58692d1 100644 --- a/deno/lib/helpers/parseUtil.ts +++ b/deno/lib/helpers/parseUtil.ts @@ -1,7 +1,7 @@ import { getErrorMap } from "../errors.ts"; import defaultErrorMap from "../locales/en.ts"; import type { IssueData, ZodErrorMap, ZodIssue } from "../ZodError.ts"; -import type { ZodParsedType } from "./util.ts"; +import type { ZodParsedType } from "../index.ts"; export const makeIssue = (params: { data: any; diff --git a/deno/lib/helpers/partialUtil.ts b/deno/lib/helpers/partialUtil.ts index b9de239fd..4c20fb851 100644 --- a/deno/lib/helpers/partialUtil.ts +++ b/deno/lib/helpers/partialUtil.ts @@ -9,70 +9,67 @@ import type { ZodTypeAny, } from "../index.ts"; -export namespace partialUtil { - // export type DeepPartial = T extends AnyZodObject - // ? ZodObject< - // { [k in keyof T["_shape"]]: InternalDeepPartial }, - // T["_unknownKeys"], - // T["_catchall"] - // > - // : T extends ZodArray - // ? ZodArray, Card> - // : ZodOptional; +// export type DeepPartial = T extends AnyZodObject +// ? ZodObject< +// { [k in keyof T["_shape"]]: InternalDeepPartial }, +// T["_unknownKeys"], +// T["_catchall"] +// > +// : T extends ZodArray +// ? ZodArray, Card> +// : ZodOptional; - // { - // // optional: T extends ZodOptional ? T : ZodOptional; - // // array: T extends ZodArray ? ZodArray> : never; - // object: T extends AnyZodObject - // ? ZodObject< - // { [k in keyof T["_shape"]]: DeepPartial }, - // T["_unknownKeys"], - // T["_catchall"] - // > - // : never; - // rest: ReturnType; // ZodOptional; - // }[T extends AnyZodObject - // ? "object" // T extends ZodOptional // ? 'optional' // : - // : "rest"]; +// { +// // optional: T extends ZodOptional ? T : ZodOptional; +// // array: T extends ZodArray ? ZodArray> : never; +// object: T extends AnyZodObject +// ? ZodObject< +// { [k in keyof T["_shape"]]: DeepPartial }, +// T["_unknownKeys"], +// T["_catchall"] +// > +// : never; +// rest: ReturnType; // ZodOptional; +// }[T extends AnyZodObject +// ? "object" // T extends ZodOptional // ? 'optional' // : +// : "rest"]; - export type DeepPartial = - T extends ZodObject - ? ZodObject< - { [k in keyof T["shape"]]: ZodOptional> }, - T["_def"]["unknownKeys"], - T["_def"]["catchall"] - > - : T extends ZodArray - ? ZodArray, Card> - : T extends ZodOptional - ? ZodOptional> - : T extends ZodNullable - ? ZodNullable> - : T extends ZodTuple - ? { - [k in keyof Items]: Items[k] extends ZodTypeAny - ? DeepPartial - : never; - } extends infer PI - ? PI extends ZodTupleItems - ? ZodTuple - : never - : never - : T; - // { - // // optional: T extends ZodOptional ? T : ZodOptional; - // // array: T extends ZodArray ? ZodArray> : never; - // object: T extends ZodObject - // ? ZodOptional< - // ZodObject< - // { [k in keyof Shape]: DeepPartial }, - // Params, - // Catchall - // > - // > - // : never; - // rest: ReturnType; - // }[T extends ZodObject - // ? "object" // T extends ZodOptional // ? 'optional' // : - // : "rest"]; -} +export type DeepPartial = T extends ZodObject + ? ZodObject< + { [k in keyof T["shape"]]: ZodOptional> }, + T["_def"]["unknownKeys"], + T["_def"]["catchall"] + > + : T extends ZodArray + ? ZodArray, Card> + : T extends ZodOptional + ? ZodOptional> + : T extends ZodNullable + ? ZodNullable> + : T extends ZodTuple + ? { + [k in keyof Items]: Items[k] extends ZodTypeAny + ? DeepPartial + : never; + } extends infer PI + ? PI extends ZodTupleItems + ? ZodTuple + : never + : never + : T; +// { +// // optional: T extends ZodOptional ? T : ZodOptional; +// // array: T extends ZodArray ? ZodArray> : never; +// object: T extends ZodObject +// ? ZodOptional< +// ZodObject< +// { [k in keyof Shape]: DeepPartial }, +// Params, +// Catchall +// > +// > +// : never; +// rest: ReturnType; +// }[T extends ZodObject +// ? "object" // T extends ZodOptional // ? 'optional' // : +// : "rest"]; diff --git a/deno/lib/helpers/util.ts b/deno/lib/helpers/util.ts index 26c1cb185..5e504c1fc 100644 --- a/deno/lib/helpers/util.ts +++ b/deno/lib/helpers/util.ts @@ -1,146 +1,99 @@ -export namespace util { - type AssertEqual = (() => V extends T ? 1 : 2) extends < - V - >() => V extends U ? 1 : 2 - ? true - : false; - - export type isAny = 0 extends 1 & T ? true : false; - export const assertEqual = (val: AssertEqual) => val; - export function assertIs(_arg: T): void {} - export function assertNever(_x: never): never { - throw new Error(); +// import { objectUtil } from "."; + +export type AssertEqual = (() => V extends T ? 1 : 2) extends < + V +>() => V extends U ? 1 : 2 + ? true + : false; + +export type isAny = 0 extends 1 & T ? true : false; +export const assertEqual = (val: AssertEqual) => val; +export function assertIs(_arg: T): void {} +export function assertNever(_x: never): never { + throw new Error(); +} + +export type Omit = Pick>; +export type OmitKeys = Pick>; +export type MakePartial = Omit & + Partial>; +export type Exactly = T & Record, never>; + +export const arrayToEnum = ( + items: U +): { [k in U[number]]: k } => { + const obj: any = {}; + for (const item of items) { + obj[item] = item; + } + return obj as any; +}; + +export const getValidEnumValues = (obj: any) => { + const validKeys = objectKeys(obj).filter( + (k: any) => typeof obj[obj[k]] !== "number" + ); + const filtered: any = {}; + for (const k of validKeys) { + filtered[k] = obj[k]; } + return objectValues(filtered); +}; + +export const objectValues = (obj: any) => { + return objectKeys(obj).map(function (e) { + return obj[e]; + }); +}; - export type Omit = Pick>; - export type OmitKeys = Pick>; - export type MakePartial = Omit & - Partial>; - export type Exactly = T & Record, never>; - - export const arrayToEnum = ( - items: U - ): { [k in U[number]]: k } => { - const obj: any = {}; - for (const item of items) { - obj[item] = item; - } - return obj as any; - }; - - export const getValidEnumValues = (obj: any) => { - const validKeys = objectKeys(obj).filter( - (k: any) => typeof obj[obj[k]] !== "number" - ); - const filtered: any = {}; - for (const k of validKeys) { - filtered[k] = obj[k]; - } - return objectValues(filtered); - }; - - export const objectValues = (obj: any) => { - return objectKeys(obj).map(function (e) { - return obj[e]; - }); - }; - - export const objectKeys: ObjectConstructor["keys"] = - typeof Object.keys === "function" // eslint-disable-line ban/ban - ? (obj: any) => Object.keys(obj) // eslint-disable-line ban/ban - : (object: any) => { - const keys = []; - for (const key in object) { - if (Object.prototype.hasOwnProperty.call(object, key)) { - keys.push(key); - } +export const objectKeys: ObjectConstructor["keys"] = + typeof Object.keys === "function" // eslint-disable-line ban/ban + ? (obj: any) => Object.keys(obj) // eslint-disable-line ban/ban + : (object: any) => { + const keys = []; + for (const key in object) { + if (Object.prototype.hasOwnProperty.call(object, key)) { + keys.push(key); } - return keys; - }; - - export const find = ( - arr: T[], - checker: (arg: T) => any - ): T | undefined => { - for (const item of arr) { - if (checker(item)) return item; - } - return undefined; - }; - - export type identity = objectUtil.identity; - export type flatten = objectUtil.flatten; - - export type noUndefined = T extends undefined ? never : T; - - export const isInteger: NumberConstructor["isInteger"] = - typeof Number.isInteger === "function" - ? (val) => Number.isInteger(val) // eslint-disable-line ban/ban - : (val) => - typeof val === "number" && isFinite(val) && Math.floor(val) === val; - - export function joinValues( - array: T, - separator = " | " - ): string { - return array - .map((val) => (typeof val === "string" ? `'${val}'` : val)) - .join(separator); + } + return keys; + }; + +export const find = (arr: T[], checker: (arg: T) => any): T | undefined => { + for (const item of arr) { + if (checker(item)) return item; } + return undefined; +}; - export const jsonStringifyReplacer = (_: string, value: any): any => { - if (typeof value === "bigint") { - return value.toString(); - } - return value; - }; -} +// export type identity = objectUtil.identity; +// export type flatten = objectUtil.flatten; + +export type noUndefined = T extends undefined ? never : T; -export namespace objectUtil { - export type MergeShapes = { - [k in Exclude]: U[k]; - } & V; - - type optionalKeys = { - [k in keyof T]: undefined extends T[k] ? k : never; - }[keyof T]; - type requiredKeys = { - [k in keyof T]: undefined extends T[k] ? never : k; - }[keyof T]; - export type addQuestionMarks = { - [K in requiredKeys]: T[K]; - } & { - [K in optionalKeys]?: T[K]; - } & { [k in keyof T]?: unknown }; - - export type identity = T; - export type flatten = identity<{ [k in keyof T]: T[k] }>; - - export type noNeverKeys = { - [k in keyof T]: [T[k]] extends [never] ? never : k; - }[keyof T]; - - export type noNever = identity<{ - [k in noNeverKeys]: k extends keyof T ? T[k] : never; - }>; - - export const mergeShapes = (first: U, second: T): T & U => { - return { - ...first, - ...second, // second overwrites first - }; - }; - - export type extendShape = { - [K in keyof A | keyof B]: K extends keyof B - ? B[K] - : K extends keyof A - ? A[K] - : never; - }; +export const isInteger: NumberConstructor["isInteger"] = + typeof Number.isInteger === "function" + ? (val) => Number.isInteger(val) // eslint-disable-line ban/ban + : (val) => + typeof val === "number" && isFinite(val) && Math.floor(val) === val; + +export function joinValues( + array: T, + separator = " | " +): string { + return array + .map((val) => (typeof val === "string" ? `'${val}'` : val)) + .join(separator); } -export const ZodParsedType = util.arrayToEnum([ +export const jsonStringifyReplacer = (_: string, value: any): any => { + if (typeof value === "bigint") { + return value.toString(); + } + return value; +}; + +export const ZodParsedType = arrayToEnum([ "string", "nan", "number", diff --git a/deno/lib/locales/en.ts b/deno/lib/locales/en.ts index be71e606f..ce0413eee 100644 --- a/deno/lib/locales/en.ts +++ b/deno/lib/locales/en.ts @@ -1,11 +1,12 @@ -import { util, ZodParsedType } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; + import { ZodErrorMap, ZodIssueCode } from "../ZodError.ts"; const errorMap: ZodErrorMap = (issue, _ctx) => { let message: string; switch (issue.code) { case ZodIssueCode.invalid_type: - if (issue.received === ZodParsedType.undefined) { + if (issue.received === util.ZodParsedType.undefined) { message = "Required"; } else { message = `Expected ${issue.expected}, received ${issue.received}`; diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 93279db2a..a41b46680 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -1,6 +1,5 @@ import { defaultErrorMap, getErrorMap } from "./errors.ts"; -import { enumUtil } from "./helpers/enumUtil.ts"; -import { errorUtil } from "./helpers/errorUtil.ts"; +import { enumUtil, errorUtil, objectUtil, partialUtil, util } from "./helpers/index.ts"; import { addIssueToContext, AsyncParseReturnType, @@ -20,9 +19,8 @@ import { ParseStatus, SyncParseReturnType, } from "./helpers/parseUtil.ts"; -import { partialUtil } from "./helpers/partialUtil.ts"; import { Primitive } from "./helpers/typeAliases.ts"; -import { getParsedType, objectUtil, util, ZodParsedType } from "./helpers/util.ts"; +import { getParsedType, ZodParsedType } from "./helpers/util.ts"; import { IssueData, StringValidation, @@ -33,6 +31,8 @@ import { ZodIssueCode, } from "./ZodError.ts"; +export { ZodParsedType } from "./helpers/util.ts"; + /////////////////////////////////////// /////////////////////////////////////// ////////// ////////// @@ -1330,14 +1330,14 @@ export class ZodString extends ZodType { return max; } - static create = (params?: RawCreateParams & { coerce?: true }): ZodString => { + static create(params?: RawCreateParams & { coerce?: true }): ZodString { return new ZodString({ checks: [], typeName: ZodFirstPartyTypeKind.ZodString, coerce: params?.coerce ?? false, ...processCreateParams(params), }); - }; + } } ///////////////////////////////////////// @@ -1460,16 +1460,14 @@ export class ZodNumber extends ZodType { return { status: status.value, value: input.data }; } - static create = ( - params?: RawCreateParams & { coerce?: boolean } - ): ZodNumber => { + static create(params?: RawCreateParams & { coerce?: boolean }): ZodNumber { return new ZodNumber({ checks: [], typeName: ZodFirstPartyTypeKind.ZodNumber, coerce: params?.coerce || false, ...processCreateParams(params), }); - }; + } gte(value: number, message?: errorUtil.ErrMessage) { return this.setLimit("min", value, true, errorUtil.toString(message)); @@ -1723,16 +1721,14 @@ export class ZodBigInt extends ZodType { return { status: status.value, value: input.data }; } - static create = ( - params?: RawCreateParams & { coerce?: boolean } - ): ZodBigInt => { + static create(params?: RawCreateParams & { coerce?: boolean }): ZodBigInt { return new ZodBigInt({ checks: [], typeName: ZodFirstPartyTypeKind.ZodBigInt, coerce: params?.coerce ?? false, ...processCreateParams(params), }); - }; + } gte(value: bigint, message?: errorUtil.ErrMessage) { return this.setLimit("min", value, true, errorUtil.toString(message)); @@ -1875,15 +1871,13 @@ export class ZodBoolean extends ZodType { return OK(input.data); } - static create = ( - params?: RawCreateParams & { coerce?: boolean } - ): ZodBoolean => { + static create(params?: RawCreateParams & { coerce?: boolean }): ZodBoolean { return new ZodBoolean({ typeName: ZodFirstPartyTypeKind.ZodBoolean, coerce: params?.coerce || false, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////// @@ -2013,16 +2007,14 @@ export class ZodDate extends ZodType { return max != null ? new Date(max) : null; } - static create = ( - params?: RawCreateParams & { coerce?: boolean } - ): ZodDate => { + static create(params?: RawCreateParams & { coerce?: boolean }): ZodDate { return new ZodDate({ checks: [], coerce: params?.coerce || false, typeName: ZodFirstPartyTypeKind.ZodDate, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////////// @@ -2052,12 +2044,12 @@ export class ZodSymbol extends ZodType { return OK(input.data); } - static create = (params?: RawCreateParams): ZodSymbol => { + static create(params?: RawCreateParams): ZodSymbol { return new ZodSymbol({ typeName: ZodFirstPartyTypeKind.ZodSymbol, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////////// @@ -2091,12 +2083,12 @@ export class ZodUndefined extends ZodType< } params?: RawCreateParams; - static create = (params?: RawCreateParams): ZodUndefined => { + static create(params?: RawCreateParams): ZodUndefined { return new ZodUndefined({ typeName: ZodFirstPartyTypeKind.ZodUndefined, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////// @@ -2124,12 +2116,12 @@ export class ZodNull extends ZodType { } return OK(input.data); } - static create = (params?: RawCreateParams): ZodNull => { + static create(params?: RawCreateParams): ZodNull { return new ZodNull({ typeName: ZodFirstPartyTypeKind.ZodNull, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////// @@ -2149,12 +2141,12 @@ export class ZodAny extends ZodType { _parse(input: ParseInput): ParseReturnType { return OK(input.data); } - static create = (params?: RawCreateParams): ZodAny => { + static create(params?: RawCreateParams): ZodAny { return new ZodAny({ typeName: ZodFirstPartyTypeKind.ZodAny, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////// @@ -2175,12 +2167,12 @@ export class ZodUnknown extends ZodType { return OK(input.data); } - static create = (params?: RawCreateParams): ZodUnknown => { + static create(params?: RawCreateParams): ZodUnknown { return new ZodUnknown({ typeName: ZodFirstPartyTypeKind.ZodUnknown, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////// @@ -2204,12 +2196,12 @@ export class ZodNever extends ZodType { }); return INVALID; } - static create = (params?: RawCreateParams): ZodNever => { + static create(params?: RawCreateParams): ZodNever { return new ZodNever({ typeName: ZodFirstPartyTypeKind.ZodNever, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////// @@ -2238,12 +2230,12 @@ export class ZodVoid extends ZodType { return OK(input.data); } - static create = (params?: RawCreateParams): ZodVoid => { + static create(params?: RawCreateParams): ZodVoid { return new ZodVoid({ typeName: ZodFirstPartyTypeKind.ZodVoid, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////// @@ -2426,10 +2418,11 @@ export class ZodArray< }) as any; } - static create = ( + + static create( schema: T, params?: RawCreateParams - ): ZodArray => { + ): ZodArray { return new ZodArray({ type: schema, minLength: null, @@ -2439,7 +2432,7 @@ export class ZodArray< typeName: ZodFirstPartyTypeKind.ZodArray, ...processCreateParams(params), }); - }; + } } export type ZodNonEmptyArray = ZodArray; @@ -3002,12 +2995,12 @@ export class ZodObject< } keyof(): ZodEnum> { - return createZodEnum( + return ZodEnum.create( util.objectKeys(this.shape) as [string, ...string[]] ) as any; } - static create = ( + static create( shape: T, params?: RawCreateParams ): ZodObject< @@ -3016,7 +3009,7 @@ export class ZodObject< ZodTypeAny, objectOutputType, objectInputType - > => { + > { return new ZodObject({ shape: () => shape, unknownKeys: "strip", @@ -3024,12 +3017,12 @@ export class ZodObject< typeName: ZodFirstPartyTypeKind.ZodObject, ...processCreateParams(params), }) as any; - }; + } - static strictCreate = ( + static strictCreate( shape: T, params?: RawCreateParams - ): ZodObject => { + ): ZodObject { return new ZodObject({ shape: () => shape, unknownKeys: "strict", @@ -3037,12 +3030,12 @@ export class ZodObject< typeName: ZodFirstPartyTypeKind.ZodObject, ...processCreateParams(params), }) as any; - }; + } - static lazycreate = ( + static lazycreate( shape: () => T, params?: RawCreateParams - ): ZodObject => { + ): ZodObject { return new ZodObject({ shape, unknownKeys: "strip", @@ -3050,7 +3043,7 @@ export class ZodObject< typeName: ZodFirstPartyTypeKind.ZodObject, ...processCreateParams(params), }) as any; - }; + } } export type AnyZodObject = ZodObject; @@ -3182,18 +3175,16 @@ export class ZodUnion extends ZodType< return this._def.options; } - static create = < - T extends Readonly<[ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]> - >( + static create>( types: T, params?: RawCreateParams - ): ZodUnion => { + ): ZodUnion { return new ZodUnion({ options: types, typeName: ZodFirstPartyTypeKind.ZodUnion, ...processCreateParams(params), }); - }; + } } ///////////////////////////////////////////////////// @@ -3507,18 +3498,18 @@ export class ZodIntersection< } } - static create = ( + static create( left: T, right: U, params?: RawCreateParams - ): ZodIntersection => { + ): ZodIntersection { return new ZodIntersection({ left: left, right: right, typeName: ZodFirstPartyTypeKind.ZodIntersection, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////// @@ -3637,10 +3628,10 @@ export class ZodTuple< }); } - static create = ( + static create( schemas: T, params?: RawCreateParams - ): ZodTuple => { + ): ZodTuple { if (!Array.isArray(schemas)) { throw new Error("You must pass an array of schemas to z.tuple([ ... ])"); } @@ -3650,7 +3641,7 @@ export class ZodTuple< rest: null, ...processCreateParams(params), }); - }; + } } ///////////////////////////////////////// @@ -3855,21 +3846,21 @@ export class ZodMap< return { status: status.value, value: finalMap }; } } - static create = < + static create< Key extends ZodTypeAny = ZodTypeAny, Value extends ZodTypeAny = ZodTypeAny >( keyType: Key, valueType: Value, params?: RawCreateParams - ): ZodMap => { + ): ZodMap { return new ZodMap({ valueType, keyType, typeName: ZodFirstPartyTypeKind.ZodMap, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////// @@ -3978,10 +3969,10 @@ export class ZodSet extends ZodType< return this.min(1, message) as any; } - static create = ( + static create( valueType: Value, params?: RawCreateParams - ): ZodSet => { + ): ZodSet { return new ZodSet({ valueType, minSize: null, @@ -3989,7 +3980,7 @@ export class ZodSet extends ZodType< typeName: ZodFirstPartyTypeKind.ZodSet, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////////// @@ -4222,16 +4213,16 @@ export class ZodLazy extends ZodType< return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx }); } - static create = ( + static create( getter: () => T, params?: RawCreateParams - ): ZodLazy => { + ): ZodLazy { return new ZodLazy({ getter: getter, typeName: ZodFirstPartyTypeKind.ZodLazy, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////// @@ -4264,16 +4255,16 @@ export class ZodLiteral extends ZodType, T> { return this._def.value; } - static create = ( + static create( value: T, params?: RawCreateParams - ): ZodLiteral => { + ): ZodLiteral { return new ZodLiteral({ value: value, typeName: ZodFirstPartyTypeKind.ZodLiteral, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////// @@ -4310,25 +4301,6 @@ export type FilterEnum = Values extends [] export type typecast = A extends T ? A : never; -function createZodEnum>( - values: T, - params?: RawCreateParams -): ZodEnum>; -function createZodEnum( - values: T, - params?: RawCreateParams -): ZodEnum; -function createZodEnum( - values: [string, ...string[]], - params?: RawCreateParams -) { - return new ZodEnum({ - values, - typeName: ZodFirstPartyTypeKind.ZodEnum, - ...processCreateParams(params), - }); -} - export class ZodEnum extends ZodType< T[number], ZodEnumDef, @@ -4422,7 +4394,21 @@ export class ZodEnum extends ZodType< ) as any; } - static create = createZodEnum; + static create>( + values: T, + params?: RawCreateParams + ): ZodEnum>; + static create( + values: T, + params?: RawCreateParams + ): ZodEnum; + static create(values: [string, ...string[]], params?: RawCreateParams) { + return new ZodEnum({ + values, + typeName: ZodFirstPartyTypeKind.ZodEnum, + ...processCreateParams(params), + }); + } } ///////////////////////////////////////////// @@ -4484,16 +4470,16 @@ export class ZodNativeEnum extends ZodType< return this._def.values; } - static create = ( + static create( values: T, params?: RawCreateParams - ): ZodNativeEnum => { + ): ZodNativeEnum { return new ZodNativeEnum({ values: values, typeName: ZodFirstPartyTypeKind.ZodNativeEnum, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////// @@ -4547,16 +4533,16 @@ export class ZodPromise extends ZodType< ); } - static create = ( + static create( schema: T, params?: RawCreateParams - ): ZodPromise => { + ): ZodPromise { return new ZodPromise({ type: schema, typeName: ZodFirstPartyTypeKind.ZodPromise, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////////// @@ -4737,31 +4723,31 @@ export class ZodEffects< util.assertNever(effect); } - static create = ( + static create( schema: I, effect: Effect, params?: RawCreateParams - ): ZodEffects => { + ): ZodEffects { return new ZodEffects({ schema, typeName: ZodFirstPartyTypeKind.ZodEffects, effect, ...processCreateParams(params), }); - }; + } - static createWithPreprocess = ( + static createWithPreprocess( preprocess: (arg: unknown, ctx: RefinementCtx) => unknown, schema: I, params?: RawCreateParams - ): ZodEffects => { + ): ZodEffects { return new ZodEffects({ schema, effect: { type: "preprocess", transform: preprocess }, typeName: ZodFirstPartyTypeKind.ZodEffects, ...processCreateParams(params), }); - }; + } } export { ZodEffects as ZodTransformer }; @@ -4798,16 +4784,16 @@ export class ZodOptional extends ZodType< return this._def.innerType; } - static create = ( + static create( type: T, params?: RawCreateParams - ): ZodOptional => { + ): ZodOptional { return new ZodOptional({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodOptional, ...processCreateParams(params), }) as any; - }; + } } /////////////////////////////////////////// @@ -4842,16 +4828,16 @@ export class ZodNullable extends ZodType< return this._def.innerType; } - static create = ( + static create( type: T, params?: RawCreateParams - ): ZodNullable => { + ): ZodNullable { return new ZodNullable({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodNullable, ...processCreateParams(params), }) as any; - }; + } } //////////////////////////////////////////// @@ -4890,12 +4876,12 @@ export class ZodDefault extends ZodType< return this._def.innerType; } - static create = ( + static create( type: T, params: RawCreateParams & { default: T["_input"] | (() => util.noUndefined); } - ): ZodDefault => { + ): ZodDefault { return new ZodDefault({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodDefault, @@ -4905,7 +4891,7 @@ export class ZodDefault extends ZodType< : () => params.default as any, ...processCreateParams(params), }) as any; - }; + } } ////////////////////////////////////////// @@ -4982,12 +4968,12 @@ export class ZodCatch extends ZodType< return this._def.innerType; } - static create = ( + static create( type: T, params: RawCreateParams & { catch: T["_output"] | (() => T["_output"]); } - ): ZodCatch => { + ): ZodCatch { return new ZodCatch({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodCatch, @@ -4995,7 +4981,7 @@ export class ZodCatch extends ZodType< typeof params.catch === "function" ? params.catch : () => params.catch, ...processCreateParams(params), }); - }; + } } ///////////////////////////////////////// @@ -5026,12 +5012,12 @@ export class ZodNaN extends ZodType { return { status: "valid", value: input.data }; } - static create = (params?: RawCreateParams): ZodNaN => { + static create(params?: RawCreateParams): ZodNaN { return new ZodNaN({ typeName: ZodFirstPartyTypeKind.ZodNaN, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////// @@ -5194,16 +5180,16 @@ export class ZodReadonly extends ZodType< return result; } - static create = ( + static create( type: T, params?: RawCreateParams - ): ZodReadonly => { + ): ZodReadonly { return new ZodReadonly({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodReadonly, ...processCreateParams(params), }) as any; - }; + } unwrap() { return this._def.innerType; @@ -5252,8 +5238,10 @@ export function custom( export { ZodType as Schema, ZodType as ZodSchema }; +const lateObject: typeof ZodObject.lazycreate = (...args: [any]) => + ZodObject.lazycreate(...args); export const late = { - object: ZodObject.lazycreate, + object: lateObject, }; export enum ZodFirstPartyTypeKind { @@ -5344,59 +5332,71 @@ const instanceOfType = ( } ) => custom>((data) => data instanceof cls, params); -const stringType = ZodString.create; -const numberType = ZodNumber.create; -const nanType = ZodNaN.create; -const bigIntType = ZodBigInt.create; -const booleanType = ZodBoolean.create; -const dateType = ZodDate.create; -const symbolType = ZodSymbol.create; -const undefinedType = ZodUndefined.create; -const nullType = ZodNull.create; -const anyType = ZodAny.create; -const unknownType = ZodUnknown.create; -const neverType = ZodNever.create; -const voidType = ZodVoid.create; -const arrayType = ZodArray.create; -const objectType = ZodObject.create; -const strictObjectType = ZodObject.strictCreate; -const unionType = ZodUnion.create; -const discriminatedUnionType = ZodDiscriminatedUnion.create; -const intersectionType = ZodIntersection.create; -const tupleType = ZodTuple.create; -const recordType = ZodRecord.create; -const mapType = ZodMap.create; -const setType = ZodSet.create; -const functionType = ZodFunction.create; -const lazyType = ZodLazy.create; -const literalType = ZodLiteral.create; -const enumType = ZodEnum.create; -const nativeEnumType = ZodNativeEnum.create; -const promiseType = ZodPromise.create; -const effectsType = ZodEffects.create; -const optionalType = ZodOptional.create; -const nullableType = ZodNullable.create; -const preprocessType = ZodEffects.createWithPreprocess; -const pipelineType = ZodPipeline.create; +////////////////////////////////////////////////////// +// MUST be aliased using wrapper functions. // +// See: https://github.com/colinhacks/zod/pull/2850 // +////////////////////////////////////////////////////// +const stringType: typeof ZodString.create = (...args) => + ZodString.create(...args); +const numberType: typeof ZodNumber.create = (...args) => + ZodNumber.create(...args); +const nanType: typeof ZodNaN.create = (...args) => ZodNaN.create(...args); +const bigIntType: typeof ZodBigInt.create = (...args) => + ZodBigInt.create(...args); +const booleanType: typeof ZodBoolean.create = (...args) => + ZodBoolean.create(...args); +const dateType: typeof ZodDate.create = (...args) => ZodDate.create(...args); +const symbolType: typeof ZodSymbol.create = (...args) => + ZodSymbol.create(...args); +const undefinedType: typeof ZodUndefined.create = (...args) => + ZodUndefined.create(...args); +const nullType: typeof ZodNull.create = (...args) => ZodNull.create(...args); +const anyType: typeof ZodAny.create = (...args) => ZodAny.create(...args); +const unknownType: typeof ZodUnknown.create = (...args) => + ZodUnknown.create(...args); +const neverType: typeof ZodNever.create = (...args) => ZodNever.create(...args); +const voidType: typeof ZodVoid.create = (...args) => ZodVoid.create(...args); +const arrayType: typeof ZodArray.create = (...args) => ZodArray.create(...args); +const objectType: typeof ZodObject.create = (...args) => + ZodObject.create(...args); +const strictObjectType: typeof ZodObject.strictCreate = (...args) => + ZodObject.strictCreate(...args); +const unionType: typeof ZodUnion.create = (...args) => ZodUnion.create(...args); +const discriminatedUnionType: typeof ZodDiscriminatedUnion.create = (...args) => + ZodDiscriminatedUnion.create(...args); +const intersectionType: typeof ZodIntersection.create = (...args) => + ZodIntersection.create(...args); +const tupleType: typeof ZodTuple.create = (...args) => ZodTuple.create(...args); +const recordType: typeof ZodRecord.create = (...args: [any]) => + ZodRecord.create(...args); +const mapType: typeof ZodMap.create = (...args) => ZodMap.create(...args); +const setType: typeof ZodSet.create = (...args) => ZodSet.create(...args); +const functionType: typeof ZodFunction.create = (...args: [any?]) => + ZodFunction.create(...args); +const lazyType: typeof ZodLazy.create = (...args) => ZodLazy.create(...args); +const literalType: typeof ZodLiteral.create = (...args) => + ZodLiteral.create(...args); +const enumType: typeof ZodEnum.create = (...args: [any]) => + ZodEnum.create(...args); +const nativeEnumType: typeof ZodNativeEnum.create = (...args) => + ZodNativeEnum.create(...args); +const promiseType: typeof ZodPromise.create = (...args) => + ZodPromise.create(...args); +const effectsType: typeof ZodEffects.create = (...args) => + ZodEffects.create(...args); +const optionalType: typeof ZodOptional.create = (...args) => + ZodOptional.create(...args); +const nullableType: typeof ZodNullable.create = (...args) => + ZodNullable.create(...args); +const preprocessType: typeof ZodEffects.createWithPreprocess = (...args) => + ZodEffects.createWithPreprocess(...args); +const pipelineType: typeof ZodPipeline.create = (...args) => + ZodPipeline.create(...args); const ostring = () => stringType().optional(); const onumber = () => numberType().optional(); const oboolean = () => booleanType().optional(); -export const coerce = { - string: ((arg) => - ZodString.create({ ...arg, coerce: true })) as (typeof ZodString)["create"], - number: ((arg) => - ZodNumber.create({ ...arg, coerce: true })) as (typeof ZodNumber)["create"], - boolean: ((arg) => - ZodBoolean.create({ - ...arg, - coerce: true, - })) as (typeof ZodBoolean)["create"], - bigint: ((arg) => - ZodBigInt.create({ ...arg, coerce: true })) as (typeof ZodBigInt)["create"], - date: ((arg) => - ZodDate.create({ ...arg, coerce: true })) as (typeof ZodDate)["create"], -}; +export * as coerce from "./coerce.ts"; export { anyType as any, diff --git a/playground.ts b/playground.ts index 4e01473b6..1226d6e20 100644 --- a/playground.ts +++ b/playground.ts @@ -1,3 +1,3 @@ -import { z } from "./src"; +import { z } from "./src/index"; -z; +z.string().parse("asdf"); diff --git a/src/ZodError.ts b/src/ZodError.ts index 0f521a3f2..4a2e8c5ac 100644 --- a/src/ZodError.ts +++ b/src/ZodError.ts @@ -1,6 +1,7 @@ import type { TypeOf, ZodType } from "."; +import { util } from "./helpers"; import { Primitive } from "./helpers/typeAliases"; -import { util, ZodParsedType } from "./helpers/util"; +import { ZodParsedType } from "./helpers/util"; type allKeys = T extends any ? keyof T : never; @@ -276,10 +277,10 @@ export class ZodError extends Error { return fieldErrors; } - static create = (issues: ZodIssue[]) => { + static create(issues: ZodIssue[]) { const error = new ZodError(issues); return error; - }; + } static assert(value: unknown): asserts value is ZodError { if (!(value instanceof ZodError)) { diff --git a/src/__tests__/all-errors.test.ts b/src/__tests__/all-errors.test.ts index 7b5999b42..01d69e220 100644 --- a/src/__tests__/all-errors.test.ts +++ b/src/__tests__/all-errors.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; const Test = z.object({ diff --git a/src/__tests__/anyunknown.test.ts b/src/__tests__/anyunknown.test.ts index 59880f328..84cc305e2 100644 --- a/src/__tests__/anyunknown.test.ts +++ b/src/__tests__/anyunknown.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("check any inference", () => { diff --git a/src/__tests__/array.test.ts b/src/__tests__/array.test.ts index 87e02c796..50cd06646 100644 --- a/src/__tests__/array.test.ts +++ b/src/__tests__/array.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; const minTwo = z.string().array().min(2); diff --git a/src/__tests__/base.test.ts b/src/__tests__/base.test.ts index 9c5eeac6b..eaed3dcf1 100644 --- a/src/__tests__/base.test.ts +++ b/src/__tests__/base.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("type guard", () => { diff --git a/src/__tests__/branded.test.ts b/src/__tests__/branded.test.ts index d30dfeb83..f00a98310 100644 --- a/src/__tests__/branded.test.ts +++ b/src/__tests__/branded.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 -import { expect, test } from "@jest/globals"; +import { test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("branded types", () => { diff --git a/src/__tests__/catch.test.ts b/src/__tests__/catch.test.ts index ae0ebe0a4..af24702c7 100644 --- a/src/__tests__/catch.test.ts +++ b/src/__tests__/catch.test.ts @@ -2,7 +2,7 @@ import { expect, test } from "@jest/globals"; import { z } from ".."; -import { util } from "../helpers/util"; +import { util } from "../helpers"; test("basic catch", () => { expect(z.string().catch("default").parse(undefined)).toBe("default"); diff --git a/src/__tests__/default.test.ts b/src/__tests__/default.test.ts index bd03cbd85..0a9be2fd2 100644 --- a/src/__tests__/default.test.ts +++ b/src/__tests__/default.test.ts @@ -2,7 +2,7 @@ import { expect, test } from "@jest/globals"; import { z } from ".."; -import { util } from "../helpers/util"; +import { util } from "../helpers"; test("basic defaults", () => { expect(z.string().default("default").parse(undefined)).toBe("default"); diff --git a/src/__tests__/enum.test.ts b/src/__tests__/enum.test.ts index e529e9ce4..5f0164f66 100644 --- a/src/__tests__/enum.test.ts +++ b/src/__tests__/enum.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("create enum", () => { diff --git a/src/__tests__/error.test.ts b/src/__tests__/error.test.ts index b1942743b..5419ab1a2 100644 --- a/src/__tests__/error.test.ts +++ b/src/__tests__/error.test.ts @@ -1,7 +1,6 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { ZodParsedType } from "../helpers/util"; import * as z from "../index"; import { ZodError, ZodIssueCode } from "../ZodError"; @@ -9,8 +8,8 @@ test("error creation", () => { const err1 = ZodError.create([]); err1.addIssue({ code: ZodIssueCode.invalid_type, - expected: ZodParsedType.object, - received: ZodParsedType.string, + expected: z.ZodParsedType.object, + received: z.ZodParsedType.string, path: [], message: "", fatal: true, diff --git a/src/__tests__/firstparty.test.ts b/src/__tests__/firstparty.test.ts index 2ad9cf88f..6afd01202 100644 --- a/src/__tests__/firstparty.test.ts +++ b/src/__tests__/firstparty.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("first party switch", () => { diff --git a/src/__tests__/firstpartyschematypes.test.ts b/src/__tests__/firstpartyschematypes.test.ts index 56d3f53f6..82561bee3 100644 --- a/src/__tests__/firstpartyschematypes.test.ts +++ b/src/__tests__/firstpartyschematypes.test.ts @@ -2,7 +2,7 @@ import { test } from "@jest/globals"; import { ZodFirstPartySchemaTypes, ZodFirstPartyTypeKind } from ".."; -import { util } from "../helpers/util"; +import { util } from "../helpers"; test("Identify missing [ZodFirstPartySchemaTypes]", () => { type ZodFirstPartySchemaForType = diff --git a/src/__tests__/function.test.ts b/src/__tests__/function.test.ts index 1c23cb9c5..0c667b5ef 100644 --- a/src/__tests__/function.test.ts +++ b/src/__tests__/function.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; const args1 = z.tuple([z.string()]); diff --git a/src/__tests__/generics.test.ts b/src/__tests__/generics.test.ts index c31c7f605..610f66422 100644 --- a/src/__tests__/generics.test.ts +++ b/src/__tests__/generics.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 -import { expect, test } from "@jest/globals"; +import { test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("generics", () => { diff --git a/src/__tests__/instanceof.test.ts b/src/__tests__/instanceof.test.ts index 4175f487f..72b85db6e 100644 --- a/src/__tests__/instanceof.test.ts +++ b/src/__tests__/instanceof.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("instanceof", async () => { diff --git a/src/__tests__/map.test.ts b/src/__tests__/map.test.ts index 08405db3d..ee1ff583f 100644 --- a/src/__tests__/map.test.ts +++ b/src/__tests__/map.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; import { ZodIssueCode } from "../index"; diff --git a/src/__tests__/nativeEnum.test.ts b/src/__tests__/nativeEnum.test.ts index 11b7aff0d..7e4cede67 100644 --- a/src/__tests__/nativeEnum.test.ts +++ b/src/__tests__/nativeEnum.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("nativeEnum test with consts", () => { diff --git a/src/__tests__/object.test.ts b/src/__tests__/object.test.ts index 879351f83..3a4d40ef5 100644 --- a/src/__tests__/object.test.ts +++ b/src/__tests__/object.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; const Test = z.object({ diff --git a/src/__tests__/partials.test.ts b/src/__tests__/partials.test.ts index 3186c9f97..015162b66 100644 --- a/src/__tests__/partials.test.ts +++ b/src/__tests__/partials.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; import { ZodNullable, ZodOptional } from "../index"; diff --git a/src/__tests__/pickomit.test.ts b/src/__tests__/pickomit.test.ts index 134d03954..d02523704 100644 --- a/src/__tests__/pickomit.test.ts +++ b/src/__tests__/pickomit.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; const fish = z.object({ diff --git a/src/__tests__/preprocess.test.ts b/src/__tests__/preprocess.test.ts index 271d293ae..7379bcb52 100644 --- a/src/__tests__/preprocess.test.ts +++ b/src/__tests__/preprocess.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("preprocess", () => { diff --git a/src/__tests__/primitive.test.ts b/src/__tests__/primitive.test.ts index cd1c4f5fc..6d86c9a72 100644 --- a/src/__tests__/primitive.test.ts +++ b/src/__tests__/primitive.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; import { Mocker } from "./Mocker"; diff --git a/src/__tests__/promise.test.ts b/src/__tests__/promise.test.ts index e0c9d3cc0..2cb302e85 100644 --- a/src/__tests__/promise.test.ts +++ b/src/__tests__/promise.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; const promSchema = z.promise( diff --git a/src/__tests__/readonly.test.ts b/src/__tests__/readonly.test.ts index 267ff190c..d050420e9 100644 --- a/src/__tests__/readonly.test.ts +++ b/src/__tests__/readonly.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; enum testEnum { diff --git a/src/__tests__/record.test.ts b/src/__tests__/record.test.ts index 86f60b267..ade8de15b 100644 --- a/src/__tests__/record.test.ts +++ b/src/__tests__/record.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; const booleanRecord = z.record(z.boolean()); diff --git a/src/__tests__/refine.test.ts b/src/__tests__/refine.test.ts index 4d03439ba..b8b12a0bc 100644 --- a/src/__tests__/refine.test.ts +++ b/src/__tests__/refine.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; import { ZodIssueCode } from "../ZodError"; diff --git a/src/__tests__/set.test.ts b/src/__tests__/set.test.ts index 5526a4f3a..073f98518 100644 --- a/src/__tests__/set.test.ts +++ b/src/__tests__/set.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; import { ZodIssueCode } from "../index"; diff --git a/src/__tests__/transformer.test.ts b/src/__tests__/transformer.test.ts index 0f80aa030..fe56e8116 100644 --- a/src/__tests__/transformer.test.ts +++ b/src/__tests__/transformer.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; const stringToNumber = z.string().transform((arg) => parseFloat(arg)); diff --git a/src/__tests__/tuple.test.ts b/src/__tests__/tuple.test.ts index 364881c4e..fa58a47f4 100644 --- a/src/__tests__/tuple.test.ts +++ b/src/__tests__/tuple.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; import { ZodError } from "../ZodError"; diff --git a/src/__tests__/void.test.ts b/src/__tests__/void.test.ts index c27ecf86a..fa655546a 100644 --- a/src/__tests__/void.test.ts +++ b/src/__tests__/void.test.ts @@ -1,7 +1,7 @@ // @ts-ignore TS6133 import { expect, test } from "@jest/globals"; -import { util } from "../helpers/util"; +import { util } from "../helpers"; import * as z from "../index"; test("void", () => { const v = z.void(); diff --git a/src/coerce.ts b/src/coerce.ts new file mode 100644 index 000000000..a80df1c4a --- /dev/null +++ b/src/coerce.ts @@ -0,0 +1,23 @@ +import { ZodBigInt, ZodBoolean, ZodDate, ZodNumber, ZodString } from "./types"; + +const coerceString = ((arg) => + ZodString.create({ ...arg, coerce: true })) as (typeof ZodString)["create"]; +const coerceNumber = ((arg) => + ZodNumber.create({ ...arg, coerce: true })) as (typeof ZodNumber)["create"]; +const coerceBoolean = ((arg) => + ZodBoolean.create({ + ...arg, + coerce: true, + })) as (typeof ZodBoolean)["create"]; +const coerceBigint = ((arg) => + ZodBigInt.create({ ...arg, coerce: true })) as (typeof ZodBigInt)["create"]; +const coerceDate = ((arg) => + ZodDate.create({ ...arg, coerce: true })) as (typeof ZodDate)["create"]; + +export { + coerceBigint as bigint, + coerceBoolean as boolean, + coerceDate as date, + coerceNumber as number, + coerceString as string, +}; diff --git a/src/external.ts b/src/external.ts index 002e17d9c..1cb0ce5b7 100644 --- a/src/external.ts +++ b/src/external.ts @@ -1,6 +1,6 @@ export * from "./errors"; +export * from "./helpers/index"; export * from "./helpers/parseUtil"; export * from "./helpers/typeAliases"; -export * from "./helpers/util"; export * from "./types"; export * from "./ZodError"; diff --git a/src/helpers/enumUtil.ts b/src/helpers/enumUtil.ts index 205d26664..516f494de 100644 --- a/src/helpers/enumUtil.ts +++ b/src/helpers/enumUtil.ts @@ -1,19 +1,17 @@ -export namespace enumUtil { - type UnionToIntersectionFn = ( - T extends unknown ? (k: () => T) => void : never - ) extends (k: infer Intersection) => void - ? Intersection - : never; +type UnionToIntersectionFn = ( + T extends unknown ? (k: () => T) => void : never +) extends (k: infer Intersection) => void + ? Intersection + : never; - type GetUnionLast = UnionToIntersectionFn extends () => infer Last - ? Last - : never; +type GetUnionLast = UnionToIntersectionFn extends () => infer Last + ? Last + : never; - type UnionToTuple = [T] extends [never] - ? Tuple - : UnionToTuple>, [GetUnionLast, ...Tuple]>; +type UnionToTuple = [T] extends [never] + ? Tuple + : UnionToTuple>, [GetUnionLast, ...Tuple]>; - type CastToStringTuple = T extends [string, ...string[]] ? T : never; +type CastToStringTuple = T extends [string, ...string[]] ? T : never; - export type UnionToTupleString = CastToStringTuple>; -} +export type UnionToTupleString = CastToStringTuple>; diff --git a/src/helpers/errorUtil.ts b/src/helpers/errorUtil.ts index 8d9afa2a8..9796f4fb3 100644 --- a/src/helpers/errorUtil.ts +++ b/src/helpers/errorUtil.ts @@ -1,7 +1,5 @@ -export namespace errorUtil { - export type ErrMessage = string | { message?: string }; - export const errToObj = (message?: ErrMessage) => - typeof message === "string" ? { message } : message || {}; - export const toString = (message?: ErrMessage): string | undefined => - typeof message === "string" ? message : message?.message; -} +export type ErrMessage = string | { message?: string }; +export const errToObj = (message?: ErrMessage) => + typeof message === "string" ? { message } : message || {}; +export const toString = (message?: ErrMessage): string | undefined => + typeof message === "string" ? message : message?.message; diff --git a/src/helpers/index.ts b/src/helpers/index.ts new file mode 100644 index 000000000..9932641f1 --- /dev/null +++ b/src/helpers/index.ts @@ -0,0 +1,5 @@ +export * as enumUtil from "./enumUtil"; +export * as errorUtil from "./errorUtil"; +export * as objectUtil from "./objectUtil"; +export * as partialUtil from "./partialUtil"; +export * as util from "./util"; diff --git a/src/helpers/objectUtil.ts b/src/helpers/objectUtil.ts new file mode 100644 index 000000000..05d61d06e --- /dev/null +++ b/src/helpers/objectUtil.ts @@ -0,0 +1,37 @@ +export type optionalKeys = { + [k in keyof T]: undefined extends T[k] ? k : never; +}[keyof T]; +export type requiredKeys = { + [k in keyof T]: undefined extends T[k] ? never : k; +}[keyof T]; +export type addQuestionMarks = { + [K in requiredKeys]: T[K]; +} & { + [K in optionalKeys]?: T[K]; +} & { [k in keyof T]?: unknown }; + +export type identity = T; +export type flatten = identity<{ [k in keyof T]: T[k] }>; + +export type noNeverKeys = { + [k in keyof T]: [T[k]] extends [never] ? never : k; +}[keyof T]; + +export type noNever = identity<{ + [k in noNeverKeys]: k extends keyof T ? T[k] : never; +}>; + +export const mergeShapes = (first: U, second: T): T & U => { + return { + ...first, + ...second, // second overwrites first + }; +}; + +export type extendShape = { + [K in keyof A | keyof B]: K extends keyof B + ? B[K] + : K extends keyof A + ? A[K] + : never; +}; diff --git a/src/helpers/parseUtil.ts b/src/helpers/parseUtil.ts index f6b105a5b..cd0d5fb44 100644 --- a/src/helpers/parseUtil.ts +++ b/src/helpers/parseUtil.ts @@ -1,7 +1,7 @@ import { getErrorMap } from "../errors"; import defaultErrorMap from "../locales/en"; import type { IssueData, ZodErrorMap, ZodIssue } from "../ZodError"; -import type { ZodParsedType } from "./util"; +import type { ZodParsedType } from ".."; export const makeIssue = (params: { data: any; diff --git a/src/helpers/partialUtil.ts b/src/helpers/partialUtil.ts index ebfa5ec31..124bc0614 100644 --- a/src/helpers/partialUtil.ts +++ b/src/helpers/partialUtil.ts @@ -9,70 +9,67 @@ import type { ZodTypeAny, } from "../index"; -export namespace partialUtil { - // export type DeepPartial = T extends AnyZodObject - // ? ZodObject< - // { [k in keyof T["_shape"]]: InternalDeepPartial }, - // T["_unknownKeys"], - // T["_catchall"] - // > - // : T extends ZodArray - // ? ZodArray, Card> - // : ZodOptional; +// export type DeepPartial = T extends AnyZodObject +// ? ZodObject< +// { [k in keyof T["_shape"]]: InternalDeepPartial }, +// T["_unknownKeys"], +// T["_catchall"] +// > +// : T extends ZodArray +// ? ZodArray, Card> +// : ZodOptional; - // { - // // optional: T extends ZodOptional ? T : ZodOptional; - // // array: T extends ZodArray ? ZodArray> : never; - // object: T extends AnyZodObject - // ? ZodObject< - // { [k in keyof T["_shape"]]: DeepPartial }, - // T["_unknownKeys"], - // T["_catchall"] - // > - // : never; - // rest: ReturnType; // ZodOptional; - // }[T extends AnyZodObject - // ? "object" // T extends ZodOptional // ? 'optional' // : - // : "rest"]; +// { +// // optional: T extends ZodOptional ? T : ZodOptional; +// // array: T extends ZodArray ? ZodArray> : never; +// object: T extends AnyZodObject +// ? ZodObject< +// { [k in keyof T["_shape"]]: DeepPartial }, +// T["_unknownKeys"], +// T["_catchall"] +// > +// : never; +// rest: ReturnType; // ZodOptional; +// }[T extends AnyZodObject +// ? "object" // T extends ZodOptional // ? 'optional' // : +// : "rest"]; - export type DeepPartial = - T extends ZodObject - ? ZodObject< - { [k in keyof T["shape"]]: ZodOptional> }, - T["_def"]["unknownKeys"], - T["_def"]["catchall"] - > - : T extends ZodArray - ? ZodArray, Card> - : T extends ZodOptional - ? ZodOptional> - : T extends ZodNullable - ? ZodNullable> - : T extends ZodTuple - ? { - [k in keyof Items]: Items[k] extends ZodTypeAny - ? DeepPartial - : never; - } extends infer PI - ? PI extends ZodTupleItems - ? ZodTuple - : never - : never - : T; - // { - // // optional: T extends ZodOptional ? T : ZodOptional; - // // array: T extends ZodArray ? ZodArray> : never; - // object: T extends ZodObject - // ? ZodOptional< - // ZodObject< - // { [k in keyof Shape]: DeepPartial }, - // Params, - // Catchall - // > - // > - // : never; - // rest: ReturnType; - // }[T extends ZodObject - // ? "object" // T extends ZodOptional // ? 'optional' // : - // : "rest"]; -} +export type DeepPartial = T extends ZodObject + ? ZodObject< + { [k in keyof T["shape"]]: ZodOptional> }, + T["_def"]["unknownKeys"], + T["_def"]["catchall"] + > + : T extends ZodArray + ? ZodArray, Card> + : T extends ZodOptional + ? ZodOptional> + : T extends ZodNullable + ? ZodNullable> + : T extends ZodTuple + ? { + [k in keyof Items]: Items[k] extends ZodTypeAny + ? DeepPartial + : never; + } extends infer PI + ? PI extends ZodTupleItems + ? ZodTuple + : never + : never + : T; +// { +// // optional: T extends ZodOptional ? T : ZodOptional; +// // array: T extends ZodArray ? ZodArray> : never; +// object: T extends ZodObject +// ? ZodOptional< +// ZodObject< +// { [k in keyof Shape]: DeepPartial }, +// Params, +// Catchall +// > +// > +// : never; +// rest: ReturnType; +// }[T extends ZodObject +// ? "object" // T extends ZodOptional // ? 'optional' // : +// : "rest"]; diff --git a/src/helpers/util.ts b/src/helpers/util.ts index 26c1cb185..5e504c1fc 100644 --- a/src/helpers/util.ts +++ b/src/helpers/util.ts @@ -1,146 +1,99 @@ -export namespace util { - type AssertEqual = (() => V extends T ? 1 : 2) extends < - V - >() => V extends U ? 1 : 2 - ? true - : false; - - export type isAny = 0 extends 1 & T ? true : false; - export const assertEqual = (val: AssertEqual) => val; - export function assertIs(_arg: T): void {} - export function assertNever(_x: never): never { - throw new Error(); +// import { objectUtil } from "."; + +export type AssertEqual = (() => V extends T ? 1 : 2) extends < + V +>() => V extends U ? 1 : 2 + ? true + : false; + +export type isAny = 0 extends 1 & T ? true : false; +export const assertEqual = (val: AssertEqual) => val; +export function assertIs(_arg: T): void {} +export function assertNever(_x: never): never { + throw new Error(); +} + +export type Omit = Pick>; +export type OmitKeys = Pick>; +export type MakePartial = Omit & + Partial>; +export type Exactly = T & Record, never>; + +export const arrayToEnum = ( + items: U +): { [k in U[number]]: k } => { + const obj: any = {}; + for (const item of items) { + obj[item] = item; + } + return obj as any; +}; + +export const getValidEnumValues = (obj: any) => { + const validKeys = objectKeys(obj).filter( + (k: any) => typeof obj[obj[k]] !== "number" + ); + const filtered: any = {}; + for (const k of validKeys) { + filtered[k] = obj[k]; } + return objectValues(filtered); +}; + +export const objectValues = (obj: any) => { + return objectKeys(obj).map(function (e) { + return obj[e]; + }); +}; - export type Omit = Pick>; - export type OmitKeys = Pick>; - export type MakePartial = Omit & - Partial>; - export type Exactly = T & Record, never>; - - export const arrayToEnum = ( - items: U - ): { [k in U[number]]: k } => { - const obj: any = {}; - for (const item of items) { - obj[item] = item; - } - return obj as any; - }; - - export const getValidEnumValues = (obj: any) => { - const validKeys = objectKeys(obj).filter( - (k: any) => typeof obj[obj[k]] !== "number" - ); - const filtered: any = {}; - for (const k of validKeys) { - filtered[k] = obj[k]; - } - return objectValues(filtered); - }; - - export const objectValues = (obj: any) => { - return objectKeys(obj).map(function (e) { - return obj[e]; - }); - }; - - export const objectKeys: ObjectConstructor["keys"] = - typeof Object.keys === "function" // eslint-disable-line ban/ban - ? (obj: any) => Object.keys(obj) // eslint-disable-line ban/ban - : (object: any) => { - const keys = []; - for (const key in object) { - if (Object.prototype.hasOwnProperty.call(object, key)) { - keys.push(key); - } +export const objectKeys: ObjectConstructor["keys"] = + typeof Object.keys === "function" // eslint-disable-line ban/ban + ? (obj: any) => Object.keys(obj) // eslint-disable-line ban/ban + : (object: any) => { + const keys = []; + for (const key in object) { + if (Object.prototype.hasOwnProperty.call(object, key)) { + keys.push(key); } - return keys; - }; - - export const find = ( - arr: T[], - checker: (arg: T) => any - ): T | undefined => { - for (const item of arr) { - if (checker(item)) return item; - } - return undefined; - }; - - export type identity = objectUtil.identity; - export type flatten = objectUtil.flatten; - - export type noUndefined = T extends undefined ? never : T; - - export const isInteger: NumberConstructor["isInteger"] = - typeof Number.isInteger === "function" - ? (val) => Number.isInteger(val) // eslint-disable-line ban/ban - : (val) => - typeof val === "number" && isFinite(val) && Math.floor(val) === val; - - export function joinValues( - array: T, - separator = " | " - ): string { - return array - .map((val) => (typeof val === "string" ? `'${val}'` : val)) - .join(separator); + } + return keys; + }; + +export const find = (arr: T[], checker: (arg: T) => any): T | undefined => { + for (const item of arr) { + if (checker(item)) return item; } + return undefined; +}; - export const jsonStringifyReplacer = (_: string, value: any): any => { - if (typeof value === "bigint") { - return value.toString(); - } - return value; - }; -} +// export type identity = objectUtil.identity; +// export type flatten = objectUtil.flatten; + +export type noUndefined = T extends undefined ? never : T; -export namespace objectUtil { - export type MergeShapes = { - [k in Exclude]: U[k]; - } & V; - - type optionalKeys = { - [k in keyof T]: undefined extends T[k] ? k : never; - }[keyof T]; - type requiredKeys = { - [k in keyof T]: undefined extends T[k] ? never : k; - }[keyof T]; - export type addQuestionMarks = { - [K in requiredKeys]: T[K]; - } & { - [K in optionalKeys]?: T[K]; - } & { [k in keyof T]?: unknown }; - - export type identity = T; - export type flatten = identity<{ [k in keyof T]: T[k] }>; - - export type noNeverKeys = { - [k in keyof T]: [T[k]] extends [never] ? never : k; - }[keyof T]; - - export type noNever = identity<{ - [k in noNeverKeys]: k extends keyof T ? T[k] : never; - }>; - - export const mergeShapes = (first: U, second: T): T & U => { - return { - ...first, - ...second, // second overwrites first - }; - }; - - export type extendShape = { - [K in keyof A | keyof B]: K extends keyof B - ? B[K] - : K extends keyof A - ? A[K] - : never; - }; +export const isInteger: NumberConstructor["isInteger"] = + typeof Number.isInteger === "function" + ? (val) => Number.isInteger(val) // eslint-disable-line ban/ban + : (val) => + typeof val === "number" && isFinite(val) && Math.floor(val) === val; + +export function joinValues( + array: T, + separator = " | " +): string { + return array + .map((val) => (typeof val === "string" ? `'${val}'` : val)) + .join(separator); } -export const ZodParsedType = util.arrayToEnum([ +export const jsonStringifyReplacer = (_: string, value: any): any => { + if (typeof value === "bigint") { + return value.toString(); + } + return value; +}; + +export const ZodParsedType = arrayToEnum([ "string", "nan", "number", diff --git a/src/locales/en.ts b/src/locales/en.ts index c7bf16797..92e4fb8a7 100644 --- a/src/locales/en.ts +++ b/src/locales/en.ts @@ -1,11 +1,12 @@ -import { util, ZodParsedType } from "../helpers/util"; +import { util } from "../helpers"; + import { ZodErrorMap, ZodIssueCode } from "../ZodError"; const errorMap: ZodErrorMap = (issue, _ctx) => { let message: string; switch (issue.code) { case ZodIssueCode.invalid_type: - if (issue.received === ZodParsedType.undefined) { + if (issue.received === util.ZodParsedType.undefined) { message = "Required"; } else { message = `Expected ${issue.expected}, received ${issue.received}`; diff --git a/src/types.ts b/src/types.ts index b15b5ddc9..2a29c1c1d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,5 @@ import { defaultErrorMap, getErrorMap } from "./errors"; -import { enumUtil } from "./helpers/enumUtil"; -import { errorUtil } from "./helpers/errorUtil"; +import { enumUtil, errorUtil, objectUtil, partialUtil, util } from "./helpers"; import { addIssueToContext, AsyncParseReturnType, @@ -20,9 +19,8 @@ import { ParseStatus, SyncParseReturnType, } from "./helpers/parseUtil"; -import { partialUtil } from "./helpers/partialUtil"; import { Primitive } from "./helpers/typeAliases"; -import { getParsedType, objectUtil, util, ZodParsedType } from "./helpers/util"; +import { getParsedType, ZodParsedType } from "./helpers/util"; import { IssueData, StringValidation, @@ -33,6 +31,8 @@ import { ZodIssueCode, } from "./ZodError"; +export { ZodParsedType } from "./helpers/util"; + /////////////////////////////////////// /////////////////////////////////////// ////////// ////////// @@ -1330,14 +1330,14 @@ export class ZodString extends ZodType { return max; } - static create = (params?: RawCreateParams & { coerce?: true }): ZodString => { + static create(params?: RawCreateParams & { coerce?: true }): ZodString { return new ZodString({ checks: [], typeName: ZodFirstPartyTypeKind.ZodString, coerce: params?.coerce ?? false, ...processCreateParams(params), }); - }; + } } ///////////////////////////////////////// @@ -1460,16 +1460,14 @@ export class ZodNumber extends ZodType { return { status: status.value, value: input.data }; } - static create = ( - params?: RawCreateParams & { coerce?: boolean } - ): ZodNumber => { + static create(params?: RawCreateParams & { coerce?: boolean }): ZodNumber { return new ZodNumber({ checks: [], typeName: ZodFirstPartyTypeKind.ZodNumber, coerce: params?.coerce || false, ...processCreateParams(params), }); - }; + } gte(value: number, message?: errorUtil.ErrMessage) { return this.setLimit("min", value, true, errorUtil.toString(message)); @@ -1723,16 +1721,14 @@ export class ZodBigInt extends ZodType { return { status: status.value, value: input.data }; } - static create = ( - params?: RawCreateParams & { coerce?: boolean } - ): ZodBigInt => { + static create(params?: RawCreateParams & { coerce?: boolean }): ZodBigInt { return new ZodBigInt({ checks: [], typeName: ZodFirstPartyTypeKind.ZodBigInt, coerce: params?.coerce ?? false, ...processCreateParams(params), }); - }; + } gte(value: bigint, message?: errorUtil.ErrMessage) { return this.setLimit("min", value, true, errorUtil.toString(message)); @@ -1875,15 +1871,13 @@ export class ZodBoolean extends ZodType { return OK(input.data); } - static create = ( - params?: RawCreateParams & { coerce?: boolean } - ): ZodBoolean => { + static create(params?: RawCreateParams & { coerce?: boolean }): ZodBoolean { return new ZodBoolean({ typeName: ZodFirstPartyTypeKind.ZodBoolean, coerce: params?.coerce || false, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////// @@ -2013,16 +2007,14 @@ export class ZodDate extends ZodType { return max != null ? new Date(max) : null; } - static create = ( - params?: RawCreateParams & { coerce?: boolean } - ): ZodDate => { + static create(params?: RawCreateParams & { coerce?: boolean }): ZodDate { return new ZodDate({ checks: [], coerce: params?.coerce || false, typeName: ZodFirstPartyTypeKind.ZodDate, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////////// @@ -2052,12 +2044,12 @@ export class ZodSymbol extends ZodType { return OK(input.data); } - static create = (params?: RawCreateParams): ZodSymbol => { + static create(params?: RawCreateParams): ZodSymbol { return new ZodSymbol({ typeName: ZodFirstPartyTypeKind.ZodSymbol, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////////// @@ -2091,12 +2083,12 @@ export class ZodUndefined extends ZodType< } params?: RawCreateParams; - static create = (params?: RawCreateParams): ZodUndefined => { + static create(params?: RawCreateParams): ZodUndefined { return new ZodUndefined({ typeName: ZodFirstPartyTypeKind.ZodUndefined, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////// @@ -2124,12 +2116,12 @@ export class ZodNull extends ZodType { } return OK(input.data); } - static create = (params?: RawCreateParams): ZodNull => { + static create(params?: RawCreateParams): ZodNull { return new ZodNull({ typeName: ZodFirstPartyTypeKind.ZodNull, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////// @@ -2149,12 +2141,12 @@ export class ZodAny extends ZodType { _parse(input: ParseInput): ParseReturnType { return OK(input.data); } - static create = (params?: RawCreateParams): ZodAny => { + static create(params?: RawCreateParams): ZodAny { return new ZodAny({ typeName: ZodFirstPartyTypeKind.ZodAny, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////// @@ -2175,12 +2167,12 @@ export class ZodUnknown extends ZodType { return OK(input.data); } - static create = (params?: RawCreateParams): ZodUnknown => { + static create(params?: RawCreateParams): ZodUnknown { return new ZodUnknown({ typeName: ZodFirstPartyTypeKind.ZodUnknown, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////// @@ -2204,12 +2196,12 @@ export class ZodNever extends ZodType { }); return INVALID; } - static create = (params?: RawCreateParams): ZodNever => { + static create(params?: RawCreateParams): ZodNever { return new ZodNever({ typeName: ZodFirstPartyTypeKind.ZodNever, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////// @@ -2238,12 +2230,12 @@ export class ZodVoid extends ZodType { return OK(input.data); } - static create = (params?: RawCreateParams): ZodVoid => { + static create(params?: RawCreateParams): ZodVoid { return new ZodVoid({ typeName: ZodFirstPartyTypeKind.ZodVoid, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////// @@ -2426,10 +2418,11 @@ export class ZodArray< }) as any; } - static create = ( + + static create( schema: T, params?: RawCreateParams - ): ZodArray => { + ): ZodArray { return new ZodArray({ type: schema, minLength: null, @@ -2439,7 +2432,7 @@ export class ZodArray< typeName: ZodFirstPartyTypeKind.ZodArray, ...processCreateParams(params), }); - }; + } } export type ZodNonEmptyArray = ZodArray; @@ -3002,12 +2995,12 @@ export class ZodObject< } keyof(): ZodEnum> { - return createZodEnum( + return ZodEnum.create( util.objectKeys(this.shape) as [string, ...string[]] ) as any; } - static create = ( + static create( shape: T, params?: RawCreateParams ): ZodObject< @@ -3016,7 +3009,7 @@ export class ZodObject< ZodTypeAny, objectOutputType, objectInputType - > => { + > { return new ZodObject({ shape: () => shape, unknownKeys: "strip", @@ -3024,12 +3017,12 @@ export class ZodObject< typeName: ZodFirstPartyTypeKind.ZodObject, ...processCreateParams(params), }) as any; - }; + } - static strictCreate = ( + static strictCreate( shape: T, params?: RawCreateParams - ): ZodObject => { + ): ZodObject { return new ZodObject({ shape: () => shape, unknownKeys: "strict", @@ -3037,12 +3030,12 @@ export class ZodObject< typeName: ZodFirstPartyTypeKind.ZodObject, ...processCreateParams(params), }) as any; - }; + } - static lazycreate = ( + static lazycreate( shape: () => T, params?: RawCreateParams - ): ZodObject => { + ): ZodObject { return new ZodObject({ shape, unknownKeys: "strip", @@ -3050,7 +3043,7 @@ export class ZodObject< typeName: ZodFirstPartyTypeKind.ZodObject, ...processCreateParams(params), }) as any; - }; + } } export type AnyZodObject = ZodObject; @@ -3182,18 +3175,16 @@ export class ZodUnion extends ZodType< return this._def.options; } - static create = < - T extends Readonly<[ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]> - >( + static create>( types: T, params?: RawCreateParams - ): ZodUnion => { + ): ZodUnion { return new ZodUnion({ options: types, typeName: ZodFirstPartyTypeKind.ZodUnion, ...processCreateParams(params), }); - }; + } } ///////////////////////////////////////////////////// @@ -3507,18 +3498,18 @@ export class ZodIntersection< } } - static create = ( + static create( left: T, right: U, params?: RawCreateParams - ): ZodIntersection => { + ): ZodIntersection { return new ZodIntersection({ left: left, right: right, typeName: ZodFirstPartyTypeKind.ZodIntersection, ...processCreateParams(params), }); - }; + } } //////////////////////////////////////// @@ -3637,10 +3628,10 @@ export class ZodTuple< }); } - static create = ( + static create( schemas: T, params?: RawCreateParams - ): ZodTuple => { + ): ZodTuple { if (!Array.isArray(schemas)) { throw new Error("You must pass an array of schemas to z.tuple([ ... ])"); } @@ -3650,7 +3641,7 @@ export class ZodTuple< rest: null, ...processCreateParams(params), }); - }; + } } ///////////////////////////////////////// @@ -3855,21 +3846,21 @@ export class ZodMap< return { status: status.value, value: finalMap }; } } - static create = < + static create< Key extends ZodTypeAny = ZodTypeAny, Value extends ZodTypeAny = ZodTypeAny >( keyType: Key, valueType: Value, params?: RawCreateParams - ): ZodMap => { + ): ZodMap { return new ZodMap({ valueType, keyType, typeName: ZodFirstPartyTypeKind.ZodMap, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////// @@ -3978,10 +3969,10 @@ export class ZodSet extends ZodType< return this.min(1, message) as any; } - static create = ( + static create( valueType: Value, params?: RawCreateParams - ): ZodSet => { + ): ZodSet { return new ZodSet({ valueType, minSize: null, @@ -3989,7 +3980,7 @@ export class ZodSet extends ZodType< typeName: ZodFirstPartyTypeKind.ZodSet, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////////// @@ -4222,16 +4213,16 @@ export class ZodLazy extends ZodType< return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx }); } - static create = ( + static create( getter: () => T, params?: RawCreateParams - ): ZodLazy => { + ): ZodLazy { return new ZodLazy({ getter: getter, typeName: ZodFirstPartyTypeKind.ZodLazy, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////// @@ -4264,16 +4255,16 @@ export class ZodLiteral extends ZodType, T> { return this._def.value; } - static create = ( + static create( value: T, params?: RawCreateParams - ): ZodLiteral => { + ): ZodLiteral { return new ZodLiteral({ value: value, typeName: ZodFirstPartyTypeKind.ZodLiteral, ...processCreateParams(params), }); - }; + } } /////////////////////////////////////// @@ -4310,25 +4301,6 @@ export type FilterEnum = Values extends [] export type typecast = A extends T ? A : never; -function createZodEnum>( - values: T, - params?: RawCreateParams -): ZodEnum>; -function createZodEnum( - values: T, - params?: RawCreateParams -): ZodEnum; -function createZodEnum( - values: [string, ...string[]], - params?: RawCreateParams -) { - return new ZodEnum({ - values, - typeName: ZodFirstPartyTypeKind.ZodEnum, - ...processCreateParams(params), - }); -} - export class ZodEnum extends ZodType< T[number], ZodEnumDef, @@ -4422,7 +4394,21 @@ export class ZodEnum extends ZodType< ) as any; } - static create = createZodEnum; + static create>( + values: T, + params?: RawCreateParams + ): ZodEnum>; + static create( + values: T, + params?: RawCreateParams + ): ZodEnum; + static create(values: [string, ...string[]], params?: RawCreateParams) { + return new ZodEnum({ + values, + typeName: ZodFirstPartyTypeKind.ZodEnum, + ...processCreateParams(params), + }); + } } ///////////////////////////////////////////// @@ -4484,16 +4470,16 @@ export class ZodNativeEnum extends ZodType< return this._def.values; } - static create = ( + static create( values: T, params?: RawCreateParams - ): ZodNativeEnum => { + ): ZodNativeEnum { return new ZodNativeEnum({ values: values, typeName: ZodFirstPartyTypeKind.ZodNativeEnum, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////// @@ -4547,16 +4533,16 @@ export class ZodPromise extends ZodType< ); } - static create = ( + static create( schema: T, params?: RawCreateParams - ): ZodPromise => { + ): ZodPromise { return new ZodPromise({ type: schema, typeName: ZodFirstPartyTypeKind.ZodPromise, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////////// @@ -4737,31 +4723,31 @@ export class ZodEffects< util.assertNever(effect); } - static create = ( + static create( schema: I, effect: Effect, params?: RawCreateParams - ): ZodEffects => { + ): ZodEffects { return new ZodEffects({ schema, typeName: ZodFirstPartyTypeKind.ZodEffects, effect, ...processCreateParams(params), }); - }; + } - static createWithPreprocess = ( + static createWithPreprocess( preprocess: (arg: unknown, ctx: RefinementCtx) => unknown, schema: I, params?: RawCreateParams - ): ZodEffects => { + ): ZodEffects { return new ZodEffects({ schema, effect: { type: "preprocess", transform: preprocess }, typeName: ZodFirstPartyTypeKind.ZodEffects, ...processCreateParams(params), }); - }; + } } export { ZodEffects as ZodTransformer }; @@ -4798,16 +4784,16 @@ export class ZodOptional extends ZodType< return this._def.innerType; } - static create = ( + static create( type: T, params?: RawCreateParams - ): ZodOptional => { + ): ZodOptional { return new ZodOptional({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodOptional, ...processCreateParams(params), }) as any; - }; + } } /////////////////////////////////////////// @@ -4842,16 +4828,16 @@ export class ZodNullable extends ZodType< return this._def.innerType; } - static create = ( + static create( type: T, params?: RawCreateParams - ): ZodNullable => { + ): ZodNullable { return new ZodNullable({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodNullable, ...processCreateParams(params), }) as any; - }; + } } //////////////////////////////////////////// @@ -4890,12 +4876,12 @@ export class ZodDefault extends ZodType< return this._def.innerType; } - static create = ( + static create( type: T, params: RawCreateParams & { default: T["_input"] | (() => util.noUndefined); } - ): ZodDefault => { + ): ZodDefault { return new ZodDefault({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodDefault, @@ -4905,7 +4891,7 @@ export class ZodDefault extends ZodType< : () => params.default as any, ...processCreateParams(params), }) as any; - }; + } } ////////////////////////////////////////// @@ -4982,12 +4968,12 @@ export class ZodCatch extends ZodType< return this._def.innerType; } - static create = ( + static create( type: T, params: RawCreateParams & { catch: T["_output"] | (() => T["_output"]); } - ): ZodCatch => { + ): ZodCatch { return new ZodCatch({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodCatch, @@ -4995,7 +4981,7 @@ export class ZodCatch extends ZodType< typeof params.catch === "function" ? params.catch : () => params.catch, ...processCreateParams(params), }); - }; + } } ///////////////////////////////////////// @@ -5026,12 +5012,12 @@ export class ZodNaN extends ZodType { return { status: "valid", value: input.data }; } - static create = (params?: RawCreateParams): ZodNaN => { + static create(params?: RawCreateParams): ZodNaN { return new ZodNaN({ typeName: ZodFirstPartyTypeKind.ZodNaN, ...processCreateParams(params), }); - }; + } } ////////////////////////////////////////// @@ -5194,16 +5180,16 @@ export class ZodReadonly extends ZodType< return result; } - static create = ( + static create( type: T, params?: RawCreateParams - ): ZodReadonly => { + ): ZodReadonly { return new ZodReadonly({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodReadonly, ...processCreateParams(params), }) as any; - }; + } unwrap() { return this._def.innerType; @@ -5252,8 +5238,10 @@ export function custom( export { ZodType as Schema, ZodType as ZodSchema }; +const lateObject: typeof ZodObject.lazycreate = (...args: [any]) => + ZodObject.lazycreate(...args); export const late = { - object: ZodObject.lazycreate, + object: lateObject, }; export enum ZodFirstPartyTypeKind { @@ -5344,59 +5332,71 @@ const instanceOfType = ( } ) => custom>((data) => data instanceof cls, params); -const stringType = ZodString.create; -const numberType = ZodNumber.create; -const nanType = ZodNaN.create; -const bigIntType = ZodBigInt.create; -const booleanType = ZodBoolean.create; -const dateType = ZodDate.create; -const symbolType = ZodSymbol.create; -const undefinedType = ZodUndefined.create; -const nullType = ZodNull.create; -const anyType = ZodAny.create; -const unknownType = ZodUnknown.create; -const neverType = ZodNever.create; -const voidType = ZodVoid.create; -const arrayType = ZodArray.create; -const objectType = ZodObject.create; -const strictObjectType = ZodObject.strictCreate; -const unionType = ZodUnion.create; -const discriminatedUnionType = ZodDiscriminatedUnion.create; -const intersectionType = ZodIntersection.create; -const tupleType = ZodTuple.create; -const recordType = ZodRecord.create; -const mapType = ZodMap.create; -const setType = ZodSet.create; -const functionType = ZodFunction.create; -const lazyType = ZodLazy.create; -const literalType = ZodLiteral.create; -const enumType = ZodEnum.create; -const nativeEnumType = ZodNativeEnum.create; -const promiseType = ZodPromise.create; -const effectsType = ZodEffects.create; -const optionalType = ZodOptional.create; -const nullableType = ZodNullable.create; -const preprocessType = ZodEffects.createWithPreprocess; -const pipelineType = ZodPipeline.create; +////////////////////////////////////////////////////// +// MUST be aliased using wrapper functions. // +// See: https://github.com/colinhacks/zod/pull/2850 // +////////////////////////////////////////////////////// +const stringType: typeof ZodString.create = (...args) => + ZodString.create(...args); +const numberType: typeof ZodNumber.create = (...args) => + ZodNumber.create(...args); +const nanType: typeof ZodNaN.create = (...args) => ZodNaN.create(...args); +const bigIntType: typeof ZodBigInt.create = (...args) => + ZodBigInt.create(...args); +const booleanType: typeof ZodBoolean.create = (...args) => + ZodBoolean.create(...args); +const dateType: typeof ZodDate.create = (...args) => ZodDate.create(...args); +const symbolType: typeof ZodSymbol.create = (...args) => + ZodSymbol.create(...args); +const undefinedType: typeof ZodUndefined.create = (...args) => + ZodUndefined.create(...args); +const nullType: typeof ZodNull.create = (...args) => ZodNull.create(...args); +const anyType: typeof ZodAny.create = (...args) => ZodAny.create(...args); +const unknownType: typeof ZodUnknown.create = (...args) => + ZodUnknown.create(...args); +const neverType: typeof ZodNever.create = (...args) => ZodNever.create(...args); +const voidType: typeof ZodVoid.create = (...args) => ZodVoid.create(...args); +const arrayType: typeof ZodArray.create = (...args) => ZodArray.create(...args); +const objectType: typeof ZodObject.create = (...args) => + ZodObject.create(...args); +const strictObjectType: typeof ZodObject.strictCreate = (...args) => + ZodObject.strictCreate(...args); +const unionType: typeof ZodUnion.create = (...args) => ZodUnion.create(...args); +const discriminatedUnionType: typeof ZodDiscriminatedUnion.create = (...args) => + ZodDiscriminatedUnion.create(...args); +const intersectionType: typeof ZodIntersection.create = (...args) => + ZodIntersection.create(...args); +const tupleType: typeof ZodTuple.create = (...args) => ZodTuple.create(...args); +const recordType: typeof ZodRecord.create = (...args: [any]) => + ZodRecord.create(...args); +const mapType: typeof ZodMap.create = (...args) => ZodMap.create(...args); +const setType: typeof ZodSet.create = (...args) => ZodSet.create(...args); +const functionType: typeof ZodFunction.create = (...args: [any?]) => + ZodFunction.create(...args); +const lazyType: typeof ZodLazy.create = (...args) => ZodLazy.create(...args); +const literalType: typeof ZodLiteral.create = (...args) => + ZodLiteral.create(...args); +const enumType: typeof ZodEnum.create = (...args: [any]) => + ZodEnum.create(...args); +const nativeEnumType: typeof ZodNativeEnum.create = (...args) => + ZodNativeEnum.create(...args); +const promiseType: typeof ZodPromise.create = (...args) => + ZodPromise.create(...args); +const effectsType: typeof ZodEffects.create = (...args) => + ZodEffects.create(...args); +const optionalType: typeof ZodOptional.create = (...args) => + ZodOptional.create(...args); +const nullableType: typeof ZodNullable.create = (...args) => + ZodNullable.create(...args); +const preprocessType: typeof ZodEffects.createWithPreprocess = (...args) => + ZodEffects.createWithPreprocess(...args); +const pipelineType: typeof ZodPipeline.create = (...args) => + ZodPipeline.create(...args); const ostring = () => stringType().optional(); const onumber = () => numberType().optional(); const oboolean = () => booleanType().optional(); -export const coerce = { - string: ((arg) => - ZodString.create({ ...arg, coerce: true })) as (typeof ZodString)["create"], - number: ((arg) => - ZodNumber.create({ ...arg, coerce: true })) as (typeof ZodNumber)["create"], - boolean: ((arg) => - ZodBoolean.create({ - ...arg, - coerce: true, - })) as (typeof ZodBoolean)["create"], - bigint: ((arg) => - ZodBigInt.create({ ...arg, coerce: true })) as (typeof ZodBigInt)["create"], - date: ((arg) => - ZodDate.create({ ...arg, coerce: true })) as (typeof ZodDate)["create"], -}; +export * as coerce from "./coerce"; export { anyType as any,