Skip to content

Commit

Permalink
Reverted #1564
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 6, 2023
1 parent bdcff0f commit a5830c6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 85 deletions.
29 changes: 0 additions & 29 deletions deno/lib/__tests__/partials.test.ts
Expand Up @@ -185,20 +185,6 @@ test("required with mask", () => {
expect(requiredObject.shape.country).toBeInstanceOf(z.ZodOptional);
});

test("required with mask containing a nonexistent key", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string().optional(),
});
object.required({
age: true,
// @ts-expect-error should not accept unexpected keys.
doesntExist: true,
});
});

test("required with mask -- ignore falsy values", () => {
const object = z.object({
name: z.string(),
Expand Down Expand Up @@ -256,21 +242,6 @@ test("partial with mask -- ignore falsy values", async () => {
await masked.parseAsync({ country: "US" });
});

test("partial with mask containing a nonexistent key", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string().optional(),
});

object.partial({
age: true,
// @ts-expect-error should not accept unexpected keys.
doesntExist: true,
});
});

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

Expand Down
18 changes: 8 additions & 10 deletions deno/lib/types.ts
Expand Up @@ -2182,8 +2182,6 @@ export type SomeZodObject = ZodObject<
ZodTypeAny
>;

export type objectKeyMask<Obj> = { [k in keyof Obj]?: true };

export type noUnrecognized<Obj extends object, Shape extends object> = {
[k in keyof Obj]: k extends keyof Shape ? Obj[k] : never;
};
Expand Down Expand Up @@ -2550,8 +2548,8 @@ export class ZodObject<
}) as any;
}

pick<Mask extends objectKeyMask<T>>(
mask: noUnrecognized<Mask, T>
pick<Mask extends { [k in keyof T]?: true }>(
mask: Mask
): ZodObject<Pick<T, Extract<keyof T, keyof Mask>>, UnknownKeys, Catchall> {
const shape: any = {};

Expand All @@ -2567,8 +2565,8 @@ export class ZodObject<
}) as any;
}

omit<Mask extends objectKeyMask<T>>(
mask: noUnrecognized<Mask, objectKeyMask<T>>
omit<Mask extends { [k in keyof T]?: true }>(
mask: Mask
): ZodObject<Omit<T, keyof Mask>, UnknownKeys, Catchall> {
const shape: any = {};

Expand Down Expand Up @@ -2596,8 +2594,8 @@ export class ZodObject<
UnknownKeys,
Catchall
>;
partial<Mask extends objectKeyMask<T>>(
mask: noUnrecognized<Mask, objectKeyMask<T>>
partial<Mask extends { [k in keyof T]?: true }>(
mask: Mask
): ZodObject<
objectUtil.noNever<{
[k in keyof T]: k extends keyof Mask ? ZodOptional<T[k]> : T[k];
Expand Down Expand Up @@ -2629,8 +2627,8 @@ export class ZodObject<
UnknownKeys,
Catchall
>;
required<Mask extends objectKeyMask<T>>(
mask: noUnrecognized<Mask, objectKeyMask<T>>
required<Mask extends { [k in keyof T]?: true }>(
mask: Mask
): ZodObject<
objectUtil.noNever<{
[k in keyof T]: k extends keyof Mask ? deoptional<T[k]> : T[k];
Expand Down
8 changes: 1 addition & 7 deletions playground.ts
@@ -1,8 +1,2 @@
import { z } from "./src";

function foo<Schema extends z.AnyZodObject>(schema: Schema) {
return z.object({ bar: schema }).transform((x) => x.bar);
// ^^^
// Property 'bar' does not exist on type
// '{ [k in keyof baseObjectOutputType<{ bar: Schema; }>]: baseObjectOutputType<{ bar: Schema; }>[k]; }'.
}
z;
29 changes: 0 additions & 29 deletions src/__tests__/partials.test.ts
Expand Up @@ -184,20 +184,6 @@ test("required with mask", () => {
expect(requiredObject.shape.country).toBeInstanceOf(z.ZodOptional);
});

test("required with mask containing a nonexistent key", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string().optional(),
});
object.required({
age: true,
// @ts-expect-error should not accept unexpected keys.
doesntExist: true,
});
});

test("required with mask -- ignore falsy values", () => {
const object = z.object({
name: z.string(),
Expand Down Expand Up @@ -255,21 +241,6 @@ test("partial with mask -- ignore falsy values", async () => {
await masked.parseAsync({ country: "US" });
});

test("partial with mask containing a nonexistent key", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string().optional(),
});

object.partial({
age: true,
// @ts-expect-error should not accept unexpected keys.
doesntExist: true,
});
});

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

Expand Down
18 changes: 8 additions & 10 deletions src/types.ts
Expand Up @@ -2182,8 +2182,6 @@ export type SomeZodObject = ZodObject<
ZodTypeAny
>;

export type objectKeyMask<Obj> = { [k in keyof Obj]?: true };

export type noUnrecognized<Obj extends object, Shape extends object> = {
[k in keyof Obj]: k extends keyof Shape ? Obj[k] : never;
};
Expand Down Expand Up @@ -2550,8 +2548,8 @@ export class ZodObject<
}) as any;
}

pick<Mask extends objectKeyMask<T>>(
mask: noUnrecognized<Mask, T>
pick<Mask extends { [k in keyof T]?: true }>(
mask: Mask
): ZodObject<Pick<T, Extract<keyof T, keyof Mask>>, UnknownKeys, Catchall> {
const shape: any = {};

Expand All @@ -2567,8 +2565,8 @@ export class ZodObject<
}) as any;
}

omit<Mask extends objectKeyMask<T>>(
mask: noUnrecognized<Mask, objectKeyMask<T>>
omit<Mask extends { [k in keyof T]?: true }>(
mask: Mask
): ZodObject<Omit<T, keyof Mask>, UnknownKeys, Catchall> {
const shape: any = {};

Expand Down Expand Up @@ -2596,8 +2594,8 @@ export class ZodObject<
UnknownKeys,
Catchall
>;
partial<Mask extends objectKeyMask<T>>(
mask: noUnrecognized<Mask, objectKeyMask<T>>
partial<Mask extends { [k in keyof T]?: true }>(
mask: Mask
): ZodObject<
objectUtil.noNever<{
[k in keyof T]: k extends keyof Mask ? ZodOptional<T[k]> : T[k];
Expand Down Expand Up @@ -2629,8 +2627,8 @@ export class ZodObject<
UnknownKeys,
Catchall
>;
required<Mask extends objectKeyMask<T>>(
mask: noUnrecognized<Mask, objectKeyMask<T>>
required<Mask extends { [k in keyof T]?: true }>(
mask: Mask
): ZodObject<
objectUtil.noNever<{
[k in keyof T]: k extends keyof Mask ? deoptional<T[k]> : T[k];
Expand Down

0 comments on commit a5830c6

Please sign in to comment.