mirror of https://github.com/cupcakearmy/zod.git
Remove comments, clean up utils
This commit is contained in:
parent
e0d709b134
commit
9c3319443c
|
@ -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", () => {
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -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",
|
||||
|
|
|
@ -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,
|
||||
|
@ -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";
|
||||
|
||||
|
@ -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<{
|
||||
|
@ -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: () => ({
|
||||
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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", () => {
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -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",
|
||||
|
|
77
src/types.ts
77
src/types.ts
|
@ -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,
|
||||
|
@ -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";
|
||||
|
||||
|
@ -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<{
|
||||
|
@ -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: () => ({
|
||||
|
@ -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;
|
||||
|
|
|
@ -5,10 +5,11 @@
|
|||
"outDir": "lib",
|
||||
"declaration": true,
|
||||
"declarationMap": false,
|
||||
"sourceMap": false
|
||||
"sourceMap": false,
|
||||
"removeComments": true
|
||||
},
|
||||
"exclude": [
|
||||
"./src/**/__tests__",
|
||||
"playground.ts"
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue