Skip to content

Commit

Permalink
Close #2127
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 5, 2023
1 parent 950bd17 commit 728e56a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
10 changes: 10 additions & 0 deletions deno/lib/__tests__/partials.test.ts
Expand Up @@ -270,3 +270,13 @@ test("partial with mask containing a nonexistent key", () => {
doesntExist: true,
});
});

test("deeppartial array", () => {
const schema = z.object({ array: z.string().array().min(42) }).deepPartial();

// works as expected
schema.parse({});

// should be false, but is true
expect(schema.safeParse({ array: [] }).success).toBe(false);
});
17 changes: 10 additions & 7 deletions deno/lib/types.ts
Expand Up @@ -2226,7 +2226,10 @@ function deepPartialify(schema: ZodTypeAny): any {
shape: () => newShape,
}) as any;
} else if (schema instanceof ZodArray) {
return ZodArray.create(deepPartialify(schema.element));
return new ZodArray({
...schema._def,
type: deepPartialify(schema.element),
});
} else if (schema instanceof ZodOptional) {
return ZodOptional.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodNullable) {
Expand Down Expand Up @@ -3781,7 +3784,7 @@ export class ZodFunction<
return this._def.returns;
}

args<Items extends Parameters<typeof ZodTuple["create"]>[0]>(
args<Items extends Parameters<(typeof ZodTuple)["create"]>[0]>(
...items: Items
): ZodFunction<ZodTuple<Items, ZodUnknown>, Returns> {
return new ZodFunction({
Expand Down Expand Up @@ -4919,18 +4922,18 @@ const oboolean = () => booleanType().optional();

export const coerce = {
string: ((arg) =>
ZodString.create({ ...arg, coerce: true })) as typeof ZodString["create"],
ZodString.create({ ...arg, coerce: true })) as (typeof ZodString)["create"],
number: ((arg) =>
ZodNumber.create({ ...arg, coerce: true })) as typeof ZodNumber["create"],
ZodNumber.create({ ...arg, coerce: true })) as (typeof ZodNumber)["create"],
boolean: ((arg) =>
ZodBoolean.create({
...arg,
coerce: true,
})) as typeof ZodBoolean["create"],
})) as (typeof ZodBoolean)["create"],
bigint: ((arg) =>
ZodBigInt.create({ ...arg, coerce: true })) as typeof ZodBigInt["create"],
ZodBigInt.create({ ...arg, coerce: true })) as (typeof ZodBigInt)["create"],
date: ((arg) =>
ZodDate.create({ ...arg, coerce: true })) as typeof ZodDate["create"],
ZodDate.create({ ...arg, coerce: true })) as (typeof ZodDate)["create"],
};

export {
Expand Down
16 changes: 12 additions & 4 deletions playground.ts
@@ -1,6 +1,14 @@
import { z } from "./src";

z.number().catch((ctx) => {
ctx;
});
export const arg = j.parse({});
const schema = z.object({ array: z.string().array().min(42) }).deepPartial();

console.log(schema.shape.array._def);
console.log(schema.shape.array._def.innerType._def.minLength);

// works as expected
console.log(schema.safeParse({}).success); // true

// should be false, but is true
console.log(schema.safeParse({ array: [] }).success); // true

console.log(z.string().array().min(42).safeParse([]).success);
10 changes: 10 additions & 0 deletions src/__tests__/partials.test.ts
Expand Up @@ -269,3 +269,13 @@ test("partial with mask containing a nonexistent key", () => {
doesntExist: true,
});
});

test("deeppartial array", () => {
const schema = z.object({ array: z.string().array().min(42) }).deepPartial();

// works as expected
schema.parse({});

// should be false, but is true
expect(schema.safeParse({ array: [] }).success).toBe(false);
});
17 changes: 10 additions & 7 deletions src/types.ts
Expand Up @@ -2226,7 +2226,10 @@ function deepPartialify(schema: ZodTypeAny): any {
shape: () => newShape,
}) as any;
} else if (schema instanceof ZodArray) {
return ZodArray.create(deepPartialify(schema.element));
return new ZodArray({
...schema._def,
type: deepPartialify(schema.element),
});
} else if (schema instanceof ZodOptional) {
return ZodOptional.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodNullable) {
Expand Down Expand Up @@ -3781,7 +3784,7 @@ export class ZodFunction<
return this._def.returns;
}

args<Items extends Parameters<typeof ZodTuple["create"]>[0]>(
args<Items extends Parameters<(typeof ZodTuple)["create"]>[0]>(
...items: Items
): ZodFunction<ZodTuple<Items, ZodUnknown>, Returns> {
return new ZodFunction({
Expand Down Expand Up @@ -4919,18 +4922,18 @@ const oboolean = () => booleanType().optional();

export const coerce = {
string: ((arg) =>
ZodString.create({ ...arg, coerce: true })) as typeof ZodString["create"],
ZodString.create({ ...arg, coerce: true })) as (typeof ZodString)["create"],
number: ((arg) =>
ZodNumber.create({ ...arg, coerce: true })) as typeof ZodNumber["create"],
ZodNumber.create({ ...arg, coerce: true })) as (typeof ZodNumber)["create"],
boolean: ((arg) =>
ZodBoolean.create({
...arg,
coerce: true,
})) as typeof ZodBoolean["create"],
})) as (typeof ZodBoolean)["create"],
bigint: ((arg) =>
ZodBigInt.create({ ...arg, coerce: true })) as typeof ZodBigInt["create"],
ZodBigInt.create({ ...arg, coerce: true })) as (typeof ZodBigInt)["create"],
date: ((arg) =>
ZodDate.create({ ...arg, coerce: true })) as typeof ZodDate["create"],
ZodDate.create({ ...arg, coerce: true })) as (typeof ZodDate)["create"],
};

export {
Expand Down

0 comments on commit 728e56a

Please sign in to comment.