Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/docs/components/ecosystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const apiLibraries: ZodResource[] = [
// description: "Zod validator middleware for Hono",
// slug: "honojs/middleware",
// },

];

const formIntegrations: ZodResource[] = [
Expand Down
41 changes: 0 additions & 41 deletions packages/zod/src/v3/helpers/partialUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,6 @@ import type {
} from "../types.js";

export namespace partialUtil {
// export type DeepPartial<T extends AnyZodObject> = T extends AnyZodObject
// ? ZodObject<
// { [k in keyof T["_shape"]]: InternalDeepPartial<T["_shape"][k]> },
// T["_unknownKeys"],
// T["_catchall"]
// >
// : T extends ZodArray<infer Type, infer Card>
// ? ZodArray<InternalDeepPartial<Type>, Card>
// : ZodOptional<T>;

// {
// // optional: T extends ZodOptional<ZodTypeAny> ? T : ZodOptional<T>;
// // array: T extends ZodArray<infer Type> ? ZodArray<DeepPartial<Type>> : never;
// object: T extends AnyZodObject
// ? ZodObject<
// { [k in keyof T["_shape"]]: DeepPartial<T["_shape"][k]> },
// T["_unknownKeys"],
// T["_catchall"]
// >
// : never;
// rest: ReturnType<T["optional"]>; // ZodOptional<T>;
// }[T extends AnyZodObject
// ? "object" // T extends ZodOptional<any> // ? 'optional' // :
// : "rest"];

export type DeepPartial<T extends ZodTypeAny> = T extends ZodObject<ZodRawShape>
? ZodObject<
{ [k in keyof T["shape"]]: ZodOptional<DeepPartial<T["shape"][k]>> },
Expand All @@ -56,20 +31,4 @@ export namespace partialUtil {
: never
: never
: T;
// {
// // optional: T extends ZodOptional<ZodTypeAny> ? T : ZodOptional<T>;
// // array: T extends ZodArray<infer Type> ? ZodArray<DeepPartial<Type>> : never;
// object: T extends ZodObject<infer Shape, infer Params, infer Catchall>
// ? ZodOptional<
// ZodObject<
// { [k in keyof Shape]: DeepPartial<Shape[k]> },
// Params,
// Catchall
// >
// >
// : never;
// rest: ReturnType<T["optional"]>;
// }[T extends ZodObject<any>
// ? "object" // T extends ZodOptional<any> // ? 'optional' // :
// : "rest"];
}
11 changes: 8 additions & 3 deletions packages/zod/src/v4/classic/tests/lazy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ test("opt passthrough", () => {
c: "default",
});

expect(z.lazy(() => z.string())._zod.optionality).toEqual(undefined);
expect(z.lazy(() => z.string().optional())._zod.optionality).toEqual("optional");
expect(z.lazy(() => z.string().default("asdf"))._zod.optionality).toEqual("defaulted");
expect(z.lazy(() => z.string())._zod.optin).toEqual(undefined);
expect(z.lazy(() => z.string())._zod.optout).toEqual(undefined);

expect(z.lazy(() => z.string().optional())._zod.optin).toEqual("optional");
expect(z.lazy(() => z.string().optional())._zod.optout).toEqual("optional");

expect(z.lazy(() => z.string().default("asdf"))._zod.optin).toEqual("optional");
expect(z.lazy(() => z.string().default("asdf"))._zod.optout).toEqual(undefined);
});

////////////// LAZY //////////////
Expand Down
85 changes: 85 additions & 0 deletions packages/zod/src/v4/classic/tests/optional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,88 @@ test("unwrap", () => {
const unwrapped = z.string().optional().unwrap();
expect(unwrapped).toBeInstanceOf(z.ZodString);
});

test("optionality", () => {
const a = z.string();
expect(a._zod.optin).toEqual(undefined);
expect(a._zod.optout).toEqual(undefined);

const b = z.string().optional();
expect(b._zod.optin).toEqual("optional");
expect(b._zod.optout).toEqual("optional");

const c = z.string().default("asdf");
expect(c._zod.optin).toEqual("optional");
expect(c._zod.optout).toEqual(undefined);

const d = z.string().optional().nullable();
expect(d._zod.optin).toEqual("optional");
expect(d._zod.optout).toEqual("optional");

const e = z.string().default("asdf").nullable();
expect(e._zod.optin).toEqual("optional");
expect(e._zod.optout).toEqual(undefined);
});

test("pipe optionality", () => {
z.string().optional()._zod.optin;
const a = z.string().optional().pipe(z.string());
expect(a._zod.optin).toEqual("optional");
expect(a._zod.optout).toEqual(undefined);
expectTypeOf<typeof a._zod.optin>().toEqualTypeOf<"optional">();
expectTypeOf<typeof a._zod.optout>().toEqualTypeOf<"optional" | undefined>();

const b = z
.string()
.transform((val) => (Math.random() ? val : undefined))
.pipe(z.string().optional());
expect(b._zod.optin).toEqual(undefined);
expect(b._zod.optout).toEqual("optional");
expectTypeOf<typeof b._zod.optin>().toEqualTypeOf<"optional" | undefined>();
expectTypeOf<typeof b._zod.optout>().toEqualTypeOf<"optional">();

const c = z.string().default("asdf").pipe(z.string());
expect(c._zod.optin).toEqual("optional");
expect(c._zod.optout).toEqual(undefined);

const d = z
.string()
.transform((val) => (Math.random() ? val : undefined))
.pipe(z.string().default("asdf"));
expect(d._zod.optin).toEqual(undefined);
expect(d._zod.optout).toEqual(undefined);
});

test("pipe optionality inside objects", () => {
const schema = z.object({
a: z.string().optional(),
b: z.string().optional().pipe(z.string()),
c: z.string().default("asdf").pipe(z.string()),
d: z
.string()
.transform((val) => (Math.random() ? val : undefined))
.pipe(z.string().optional()),
e: z
.string()
.transform((val) => (Math.random() ? val : undefined))
.pipe(z.string().default("asdf")),
});

type SchemaIn = z.input<typeof schema>;
expectTypeOf<SchemaIn>().toEqualTypeOf<{
a?: string | undefined;
b?: string | undefined;
c?: string | undefined;
d: string;
e: string;
}>();

type SchemaOut = z.output<typeof schema>;
expectTypeOf<SchemaOut>().toEqualTypeOf<{
a?: string | undefined;
b: string;
c: string;
d?: string | undefined;
e: string;
}>();
});
8 changes: 4 additions & 4 deletions packages/zod/src/v4/classic/tests/pickomit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ test("pick parse - fail", () => {

test("pick - remove optional", () => {
const schema = z.object({ a: z.string(), b: z.string().optional() });
expect(schema._zod.def.shape.a._zod.optionality).toEqual(undefined);
expect(schema._zod.def.shape.b!._zod.optionality).toEqual("optional");
expect("a" in schema._zod.def.shape).toEqual(true);
expect("b" in schema._zod.def.shape!).toEqual(true);
const picked = schema.pick({ a: true });
expect(picked._zod.def.shape.a._zod.optionality).toEqual(undefined);
expect("a" in picked._zod.def.shape).toEqual(true);
expect("b" in picked._zod.def.shape!).toEqual(false);
});

Expand Down Expand Up @@ -85,9 +85,9 @@ test("omit parse - fail", () => {

test("omit - remove optional", () => {
const schema = z.object({ a: z.string(), b: z.string().optional() });
expect("a" in schema._zod.def.shape).toEqual(true);
const omitted = schema.omit({ a: true });
expect("a" in omitted._zod.def.shape).toEqual(false);
expect(omitted._zod.def.shape.b!._zod.optionality).toEqual("optional");
});

test("nonstrict inference", () => {
Expand Down
Loading