Skip to content

Commit

Permalink
Remove comments, clean up utils
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 6, 2023
1 parent e0d709b commit 9c33194
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 142 deletions.
2 changes: 1 addition & 1 deletion deno/lib/__tests__/pickomit.test.ts
Expand Up @@ -80,7 +80,7 @@ test("omit parse - fail", () => {
test("nonstrict inference", () => {
const laxfish = fish.pick({ name: true }).catchall(z.any());
type laxfish = z.infer<typeof laxfish>;
util.assertEqual<laxfish, { [k: string]: any; name: string }>(true);
util.assertEqual<laxfish, { name: string } & { [k: string]: any }>(true);
});

test("nonstrict parsing - pass", () => {
Expand Down
45 changes: 41 additions & 4 deletions deno/lib/helpers/util.ts
Expand Up @@ -66,10 +66,8 @@ export namespace util {
return undefined;
};

export type identity<T> = T;
export type flatten<T> = identity<{
[k in keyof T]: T[k];
}>;
export type identity<T> = objectUtil.identity<T>;
export type flatten<T> = objectUtil.flatten<T>;

export type noUndefined<T> = T extends undefined ? never : T;

Expand All @@ -96,6 +94,45 @@ export namespace util {
};
}

export namespace objectUtil {
export type MergeShapes<U, V> = {
[k in Exclude<keyof U, keyof V>]: U[k];
} & V;

type optionalKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? k : never;
}[keyof T];

type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];

export type addQuestionMarks<T extends object> = Partial<
Pick<T, optionalKeys<T>>
> &
Pick<T, requiredKeys<T>>;

export type identity<T> = T;
export type flatten<T> = identity<{ [k in keyof T]: T[k] }>;

export type noNeverKeys<T> = {
[k in keyof T]: [T[k]] extends [never] ? never : k;
}[keyof T];

export type noNever<T> = identity<{
[k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
}>;

export const mergeShapes = <U, T>(first: U, second: T): T & U => {
return {
...first,
...second, // second overwrites first
};
};

export type extendShape<A, B> = flatten<Omit<A, keyof B> & B>;
}

export const ZodParsedType = util.arrayToEnum([
"string",
"nan",
Expand Down
77 changes: 18 additions & 59 deletions deno/lib/types.ts
Expand Up @@ -22,7 +22,7 @@ import {
} from "./helpers/parseUtil.ts";
import { partialUtil } from "./helpers/partialUtil.ts";
import { Primitive } from "./helpers/typeAliases.ts";
import { getParsedType, util, ZodParsedType } from "./helpers/util.ts";
import { getParsedType, objectUtil, util, ZodParsedType } from "./helpers/util.ts";
import {
IssueData,
StringValidation,
Expand Down Expand Up @@ -2110,47 +2110,6 @@ export type ZodNonEmptyArray<T extends ZodTypeAny> = ZodArray<T, "atleastone">;
////////// //////////
/////////////////////////////////////////
/////////////////////////////////////////
export namespace objectUtil {
export type MergeShapes<U extends ZodRawShape, V extends ZodRawShape> = {
[k in Exclude<keyof U, keyof V>]: U[k];
} & V;

type optionalKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? k : never;
}[keyof T];

type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];

export type addQuestionMarks<T extends object> = Partial<
Pick<T, optionalKeys<T>>
> &
Pick<T, requiredKeys<T>>;

export type identity<T> = T;
export type flatten<T extends object> = identity<{ [k in keyof T]: T[k] }>;

export type noNeverKeys<T extends ZodRawShape> = {
[k in keyof T]: [T[k]] extends [never] ? never : k;
}[keyof T];

export type noNever<T extends ZodRawShape> = identity<{
[k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
}>;

export const mergeShapes = <U extends ZodRawShape, T extends ZodRawShape>(
first: U,
second: T
): T & U => {
return {
...first,
...second, // second overwrites first
};
};
}

export type extendShape<A, B> = util.flatten<Omit<A, keyof B> & B>;

export type UnknownKeysParam = "passthrough" | "strict" | "strip";

Expand All @@ -2173,25 +2132,23 @@ export type mergeTypes<A, B> = {
: never;
};

export type processType<T extends object> = util.flatten<
objectUtil.addQuestionMarks<T>
>;
export type baseObjectOutputType<Shape extends ZodRawShape> =
objectUtil.addQuestionMarks<{
[k in keyof Shape]: Shape[k]["_output"];
}>;
objectUtil.flatten<
objectUtil.addQuestionMarks<{
[k in keyof Shape]: Shape[k]["_output"];
}>
>;

export type objectOutputType<
Shape extends ZodRawShape,
Catchall extends ZodTypeAny,
UnknownKeys extends UnknownKeysParam = UnknownKeysParam
> = ZodTypeAny extends Catchall
? objectUtil.flatten<baseObjectOutputType<Shape>> & Passthrough<UnknownKeys>
: objectUtil.flatten<
baseObjectOutputType<Shape> & {
[k: string]: Catchall["_output"];
} & Passthrough<UnknownKeys>
>;
> = (ZodTypeAny extends Catchall
? baseObjectOutputType<Shape> & Passthrough<UnknownKeys>
: baseObjectOutputType<Shape> & {
[k: string]: Catchall["_output"];
}) &
Passthrough<UnknownKeys>;

export type baseObjectInputType<Shape extends ZodRawShape> = objectUtil.flatten<
objectUtil.addQuestionMarks<{
Expand Down Expand Up @@ -2451,7 +2408,7 @@ export class ZodObject<
// };
extend<Augmentation extends ZodRawShape>(
augmentation: Augmentation
): ZodObject<extendShape<T, Augmentation>, UnknownKeys, Catchall> {
): ZodObject<objectUtil.extendShape<T, Augmentation>, UnknownKeys, Catchall> {
return new ZodObject({
...this._def,
shape: () => ({
Expand Down Expand Up @@ -2506,15 +2463,17 @@ export class ZodObject<
merge<Incoming extends AnyZodObject, Augmentation extends Incoming["shape"]>(
merging: Incoming
): ZodObject<
extendShape<T, Augmentation>,
objectUtil.extendShape<T, Augmentation>,
Incoming["_def"]["unknownKeys"],
Incoming["_def"]["catchall"]
> {
const merged: any = new ZodObject({
unknownKeys: merging._def.unknownKeys,
catchall: merging._def.catchall,
shape: () =>
objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
shape: () => ({
...this._def.shape(),
...merging._def.shape(),
}),
typeName: ZodFirstPartyTypeKind.ZodObject,
}) as any;
return merged;
Expand Down
13 changes: 1 addition & 12 deletions playground.ts
@@ -1,14 +1,3 @@
import { z } from "./src";

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);
z;
2 changes: 1 addition & 1 deletion src/__tests__/pickomit.test.ts
Expand Up @@ -79,7 +79,7 @@ test("omit parse - fail", () => {
test("nonstrict inference", () => {
const laxfish = fish.pick({ name: true }).catchall(z.any());
type laxfish = z.infer<typeof laxfish>;
util.assertEqual<laxfish, { [k: string]: any; name: string }>(true);
util.assertEqual<laxfish, { name: string } & { [k: string]: any }>(true);
});

test("nonstrict parsing - pass", () => {
Expand Down
45 changes: 41 additions & 4 deletions src/helpers/util.ts
Expand Up @@ -66,10 +66,8 @@ export namespace util {
return undefined;
};

export type identity<T> = T;
export type flatten<T> = identity<{
[k in keyof T]: T[k];
}>;
export type identity<T> = objectUtil.identity<T>;
export type flatten<T> = objectUtil.flatten<T>;

export type noUndefined<T> = T extends undefined ? never : T;

Expand All @@ -96,6 +94,45 @@ export namespace util {
};
}

export namespace objectUtil {
export type MergeShapes<U, V> = {
[k in Exclude<keyof U, keyof V>]: U[k];
} & V;

type optionalKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? k : never;
}[keyof T];

type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];

export type addQuestionMarks<T extends object> = Partial<
Pick<T, optionalKeys<T>>
> &
Pick<T, requiredKeys<T>>;

export type identity<T> = T;
export type flatten<T> = identity<{ [k in keyof T]: T[k] }>;

export type noNeverKeys<T> = {
[k in keyof T]: [T[k]] extends [never] ? never : k;
}[keyof T];

export type noNever<T> = identity<{
[k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
}>;

export const mergeShapes = <U, T>(first: U, second: T): T & U => {
return {
...first,
...second, // second overwrites first
};
};

export type extendShape<A, B> = flatten<Omit<A, keyof B> & B>;
}

export const ZodParsedType = util.arrayToEnum([
"string",
"nan",
Expand Down
77 changes: 18 additions & 59 deletions src/types.ts
Expand Up @@ -22,7 +22,7 @@ import {
} from "./helpers/parseUtil";
import { partialUtil } from "./helpers/partialUtil";
import { Primitive } from "./helpers/typeAliases";
import { getParsedType, util, ZodParsedType } from "./helpers/util";
import { getParsedType, objectUtil, util, ZodParsedType } from "./helpers/util";
import {
IssueData,
StringValidation,
Expand Down Expand Up @@ -2109,47 +2109,6 @@ export type ZodNonEmptyArray<T extends ZodTypeAny> = ZodArray<T, "atleastone">;
////////// //////////
/////////////////////////////////////////
/////////////////////////////////////////
export namespace objectUtil {
export type MergeShapes<U extends ZodRawShape, V extends ZodRawShape> = {
[k in Exclude<keyof U, keyof V>]: U[k];
} & V;

type optionalKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? k : never;
}[keyof T];

type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];

export type addQuestionMarks<T extends object> = Partial<
Pick<T, optionalKeys<T>>
> &
Pick<T, requiredKeys<T>>;

export type identity<T> = T;
export type flatten<T extends object> = identity<{ [k in keyof T]: T[k] }>;

export type noNeverKeys<T extends ZodRawShape> = {
[k in keyof T]: [T[k]] extends [never] ? never : k;
}[keyof T];

export type noNever<T extends ZodRawShape> = identity<{
[k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
}>;

export const mergeShapes = <U extends ZodRawShape, T extends ZodRawShape>(
first: U,
second: T
): T & U => {
return {
...first,
...second, // second overwrites first
};
};
}

export type extendShape<A, B> = util.flatten<Omit<A, keyof B> & B>;

export type UnknownKeysParam = "passthrough" | "strict" | "strip";

Expand All @@ -2172,25 +2131,23 @@ export type mergeTypes<A, B> = {
: never;
};

export type processType<T extends object> = util.flatten<
objectUtil.addQuestionMarks<T>
>;
export type baseObjectOutputType<Shape extends ZodRawShape> =
objectUtil.addQuestionMarks<{
[k in keyof Shape]: Shape[k]["_output"];
}>;
objectUtil.flatten<
objectUtil.addQuestionMarks<{
[k in keyof Shape]: Shape[k]["_output"];
}>
>;

export type objectOutputType<
Shape extends ZodRawShape,
Catchall extends ZodTypeAny,
UnknownKeys extends UnknownKeysParam = UnknownKeysParam
> = ZodTypeAny extends Catchall
? objectUtil.flatten<baseObjectOutputType<Shape>> & Passthrough<UnknownKeys>
: objectUtil.flatten<
baseObjectOutputType<Shape> & {
[k: string]: Catchall["_output"];
} & Passthrough<UnknownKeys>
>;
> = (ZodTypeAny extends Catchall
? baseObjectOutputType<Shape> & Passthrough<UnknownKeys>
: baseObjectOutputType<Shape> & {
[k: string]: Catchall["_output"];
}) &
Passthrough<UnknownKeys>;

export type baseObjectInputType<Shape extends ZodRawShape> = objectUtil.flatten<
objectUtil.addQuestionMarks<{
Expand Down Expand Up @@ -2450,7 +2407,7 @@ export class ZodObject<
// };
extend<Augmentation extends ZodRawShape>(
augmentation: Augmentation
): ZodObject<extendShape<T, Augmentation>, UnknownKeys, Catchall> {
): ZodObject<objectUtil.extendShape<T, Augmentation>, UnknownKeys, Catchall> {
return new ZodObject({
...this._def,
shape: () => ({
Expand Down Expand Up @@ -2505,15 +2462,17 @@ export class ZodObject<
merge<Incoming extends AnyZodObject, Augmentation extends Incoming["shape"]>(
merging: Incoming
): ZodObject<
extendShape<T, Augmentation>,
objectUtil.extendShape<T, Augmentation>,
Incoming["_def"]["unknownKeys"],
Incoming["_def"]["catchall"]
> {
const merged: any = new ZodObject({
unknownKeys: merging._def.unknownKeys,
catchall: merging._def.catchall,
shape: () =>
objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
shape: () => ({
...this._def.shape(),
...merging._def.shape(),
}),
typeName: ZodFirstPartyTypeKind.ZodObject,
}) as any;
return merged;
Expand Down

0 comments on commit 9c33194

Please sign in to comment.