Skip to content

Commit

Permalink
fix: tree shake named exports (#2850)
Browse files Browse the repository at this point in the history
* fix: tree shake named exports

* docs: explain why wrapper functions

* fix: make coerce tree shakeable

* fix: make util tree shakeable

* Fix merge conflicts

---------

Co-authored-by: Colin McDonnell <[email protected]>
  • Loading branch information
olehmisar and colinhacks authored Apr 24, 2024
1 parent 35b25b9 commit dc6365c
Show file tree
Hide file tree
Showing 83 changed files with 890 additions and 866 deletions.
7 changes: 4 additions & 3 deletions deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends any ? keyof T : never;

Expand Down Expand Up @@ -276,10 +277,10 @@ export class ZodError<T = any> 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)) {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/all-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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({
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/anyunknown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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);
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/branded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/catch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from "https://deno.land/x/[email protected]/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");
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from "https://deno.land/x/[email protected]/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");
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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", () => {
Expand Down
5 changes: 2 additions & 3 deletions deno/lib/__tests__/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import { ZodParsedType } from "../helpers/util.ts";
import * as z from "../index.ts";
import { ZodError, ZodIssueCode } from "../ZodError.ts";

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,
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/firstparty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/firstpartyschematypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from "https://deno.land/x/[email protected]/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<T extends ZodFirstPartyTypeKind> =
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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()]);
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/generics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/instanceof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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 () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/nativeEnum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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({
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/partials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/pickomit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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({
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/preprocess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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", () => {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/primitive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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(
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/readonly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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 {
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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());
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/refine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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));
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/tuple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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";

Expand Down
2 changes: 1 addition & 1 deletion deno/lib/__tests__/void.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "https://deno.land/x/[email protected]/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();
Expand Down
23 changes: 23 additions & 0 deletions deno/lib/coerce.ts
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 1 addition & 1 deletion deno/lib/external.ts
Original file line number Diff line number Diff line change
@@ -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";
28 changes: 13 additions & 15 deletions deno/lib/helpers/enumUtil.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
export namespace enumUtil {
type UnionToIntersectionFn<T> = (
T extends unknown ? (k: () => T) => void : never
) extends (k: infer Intersection) => void
? Intersection
: never;
type UnionToIntersectionFn<T> = (
T extends unknown ? (k: () => T) => void : never
) extends (k: infer Intersection) => void
? Intersection
: never;

type GetUnionLast<T> = UnionToIntersectionFn<T> extends () => infer Last
? Last
: never;
type GetUnionLast<T> = UnionToIntersectionFn<T> extends () => infer Last
? Last
: never;

type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never]
? Tuple
: UnionToTuple<Exclude<T, GetUnionLast<T>>, [GetUnionLast<T>, ...Tuple]>;
type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never]
? Tuple
: UnionToTuple<Exclude<T, GetUnionLast<T>>, [GetUnionLast<T>, ...Tuple]>;

type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never;
type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never;

export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>;
}
export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>;
12 changes: 5 additions & 7 deletions deno/lib/helpers/errorUtil.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions deno/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Loading

0 comments on commit dc6365c

Please sign in to comment.