From 0712d80226539b957be770826c57fd573cfbb565 Mon Sep 17 00:00:00 2001 From: Oleh Misarosh Date: Mon, 9 Oct 2023 11:26:36 +0300 Subject: [PATCH] fix: make util tree shakeable --- deno/lib/ZodError.ts | 3 +- deno/lib/__tests__/all-errors.test.ts | 2 +- deno/lib/__tests__/anyunknown.test.ts | 2 +- deno/lib/__tests__/array.test.ts | 2 +- deno/lib/__tests__/base.test.ts | 2 +- deno/lib/__tests__/branded.test.ts | 2 +- deno/lib/__tests__/catch.test.ts | 2 +- deno/lib/__tests__/default.test.ts | 2 +- deno/lib/__tests__/enum.test.ts | 2 +- deno/lib/__tests__/firstparty.test.ts | 2 +- deno/lib/__tests__/function.test.ts | 2 +- deno/lib/__tests__/generics.test.ts | 2 +- deno/lib/__tests__/instanceof.test.ts | 2 +- deno/lib/__tests__/map.test.ts | 2 +- deno/lib/__tests__/nativeEnum.test.ts | 2 +- deno/lib/__tests__/object.test.ts | 2 +- deno/lib/__tests__/partials.test.ts | 2 +- deno/lib/__tests__/pickomit.test.ts | 2 +- deno/lib/__tests__/primitive.test.ts | 2 +- deno/lib/__tests__/promise.test.ts | 2 +- deno/lib/__tests__/readonly.test.ts | 2 +- deno/lib/__tests__/record.test.ts | 2 +- deno/lib/__tests__/refine.test.ts | 2 +- deno/lib/__tests__/set.test.ts | 2 +- deno/lib/__tests__/transformer.test.ts | 2 +- deno/lib/__tests__/tuple.test.ts | 2 +- deno/lib/__tests__/void.test.ts | 2 +- deno/lib/helpers/enumUtil.ts | 28 ++-- deno/lib/helpers/errorUtil.ts | 12 +- deno/lib/helpers/index.ts | 5 + deno/lib/helpers/objectUtil.ts | 40 +++++ deno/lib/helpers/partialUtil.ts | 127 +++++++------- deno/lib/helpers/util.ts | 173 +++++++++---------- deno/lib/locales/en.ts | 3 +- deno/lib/types.ts | 6 +- src/ZodError.ts | 3 +- src/__tests__/all-errors.test.ts | 2 +- src/__tests__/anyunknown.test.ts | 2 +- src/__tests__/array.test.ts | 2 +- src/__tests__/base.test.ts | 2 +- src/__tests__/branded.test.ts | 4 +- src/__tests__/catch.test.ts | 2 +- src/__tests__/default.test.ts | 2 +- src/__tests__/enum.test.ts | 2 +- src/__tests__/firstparty.test.ts | 2 +- src/__tests__/function.test.ts | 2 +- src/__tests__/generics.test.ts | 4 +- src/__tests__/instanceof.test.ts | 2 +- src/__tests__/map.test.ts | 2 +- src/__tests__/nativeEnum.test.ts | 2 +- src/__tests__/object.test.ts | 2 +- src/__tests__/partials.test.ts | 2 +- src/__tests__/pickomit.test.ts | 2 +- src/__tests__/primitive.test.ts | 2 +- src/__tests__/promise.test.ts | 2 +- src/__tests__/readonly.test.ts | 2 +- src/__tests__/record.test.ts | 2 +- src/__tests__/refine.test.ts | 2 +- src/__tests__/set.test.ts | 2 +- src/__tests__/transformer.test.ts | 2 +- src/__tests__/tuple.test.ts | 2 +- src/__tests__/void.test.ts | 2 +- src/helpers/enumUtil.ts | 28 ++-- src/helpers/errorUtil.ts | 12 +- src/helpers/index.ts | 5 + src/helpers/objectUtil.ts | 37 ++++ src/helpers/partialUtil.ts | 127 +++++++------- src/helpers/util.ts | 223 ++++++++++--------------- src/locales/en.ts | 3 +- src/types.ts | 6 +- 70 files changed, 489 insertions(+), 460 deletions(-) create mode 100644 deno/lib/helpers/index.ts create mode 100644 deno/lib/helpers/objectUtil.ts create mode 100644 src/helpers/index.ts create mode 100644 src/helpers/objectUtil.ts diff --git a/deno/lib/ZodError.ts b/deno/lib/ZodError.ts index a6d86361c..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; 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__/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__/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__/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/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..3727c3307 --- /dev/null +++ b/deno/lib/helpers/objectUtil.ts @@ -0,0 +1,40 @@ +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]; + +// type alkjsdf = addQuestionMarks<{ a: any }>; + +export type addQuestionMarks< + T extends object, + R extends keyof T = requiredKeys + // O extends keyof T = optionalKeys +> = Pick, R> & Partial; +// = { [k in O]?: T[k] } & { [k in R]: T[k] }; + +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 = flatten & B>; 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..abbf63875 100644 --- a/deno/lib/helpers/util.ts +++ b/deno/lib/helpers/util.ts @@ -1,106 +1,106 @@ -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 "./index.ts"; + +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 const arrayToEnum = ( + items: U +): { [k in U[number]]: k } => { + const obj: any = {}; + for (const item of items) { + obj[item] = item; } + return obj as any; +}; +<<<<<<< HEAD export type Omit = Pick>; export type OmitKeys = Pick>; export type MakePartial = Omit & Partial>; export type Exactly = T & Record, never>; +======= +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); +}; +>>>>>>> 19fbb9a (fix: make util tree shakeable) - 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 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; - }; + } + return keys; + }; - export type identity = objectUtil.identity; - export type flatten = objectUtil.flatten; +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 noUndefined = T extends undefined ? never : T; +export type identity = objectUtil.identity; +export type flatten = objectUtil.flatten; - 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 type noUndefined = T extends undefined ? never : T; - export function joinValues( - array: T, - separator = " | " - ): string { - return array - .map((val) => (typeof val === "string" ? `'${val}'` : val)) - .join(separator); - } +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 const jsonStringifyReplacer = (_: string, value: any): any => { - if (typeof value === "bigint") { - return value.toString(); - } - return value; - }; +export function joinValues( + array: T, + separator = " | " +): string { + return array + .map((val) => (typeof val === "string" ? `'${val}'` : val)) + .join(separator); } -export namespace objectUtil { - export type MergeShapes = { - [k in Exclude]: U[k]; - } & V; +export const jsonStringifyReplacer = (_: string, value: any): any => { + if (typeof value === "bigint") { + return value.toString(); + } + return value; +}; +<<<<<<< HEAD type optionalKeys = { [k in keyof T]: undefined extends T[k] ? k : never; }[keyof T]; @@ -141,6 +141,9 @@ export namespace objectUtil { } export const ZodParsedType = util.arrayToEnum([ +======= +export const ZodParsedType = arrayToEnum([ +>>>>>>> 19fbb9a (fix: make util tree shakeable) "string", "nan", "number", diff --git a/deno/lib/locales/en.ts b/deno/lib/locales/en.ts index be71e606f..c1d505db3 100644 --- a/deno/lib/locales/en.ts +++ b/deno/lib/locales/en.ts @@ -1,4 +1,5 @@ -import { util, ZodParsedType } from "../helpers/util.ts"; +import { util } from "../helpers/index.ts"; +import { ZodParsedType } from "../helpers/util.ts"; import { ZodErrorMap, ZodIssueCode } from "../ZodError.ts"; const errorMap: ZodErrorMap = (issue, _ctx) => { diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 3bd78a014..28fcdb7a9 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, diff --git a/src/ZodError.ts b/src/ZodError.ts index 1fb23b5d4..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; 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__/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__/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__/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/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/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..23dd1cfd1 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 "."; + +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..db8a63278 100644 --- a/src/locales/en.ts +++ b/src/locales/en.ts @@ -1,4 +1,5 @@ -import { util, ZodParsedType } from "../helpers/util"; +import { util } from "../helpers"; +import { ZodParsedType } from "../helpers/util"; import { ZodErrorMap, ZodIssueCode } from "../ZodError"; const errorMap: ZodErrorMap = (issue, _ctx) => { diff --git a/src/types.ts b/src/types.ts index dfd7c1c54..4679cf83a 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,