Skip to content

Commit

Permalink
3.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McDonnell committed Feb 9, 2023
1 parent e71c7be commit e693919
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 154 deletions.
2 changes: 1 addition & 1 deletion deno/lib/__tests__/object.test.ts
Expand Up @@ -229,7 +229,7 @@ test("inferred merged object type with optional properties", async () => {
.object({ a: z.string(), b: z.string().optional() })
.merge(z.object({ a: z.string().optional(), b: z.string() }));
type Merged = z.infer<typeof Merged>;
util.assertEqual<Merged, { a: string | undefined; b: string }>(true);
util.assertEqual<Merged, { a?: string; b: string }>(true);
// todo
// util.assertEqual<Merged, { a?: string; b: string }>(true);
});
Expand Down
165 changes: 90 additions & 75 deletions deno/lib/types.ts
Expand Up @@ -2119,43 +2119,26 @@ export class ZodObject<
*/
nonstrict = this.passthrough;

// augment = AugmentFactory<ZodObjectDef<T, UnknownKeys, Catchall>>(this._def);
// extend = AugmentFactory<ZodObjectDef<T, UnknownKeys, Catchall>>(this._def);
extend<
Augmentation extends ZodRawShape,
// NewShape extends extendShape<T, Augmentation>,
// OldOutput = util.flatten<Omit<Output, keyof Augmentation>>,
// AugOutput = baseObjectOutputType<Augmentation>,
// NewOutput = OldOutput & AugOutput,
NewOutput extends util.flatten<{
[k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
? Augmentation[k]["_output"]
: k extends keyof Output
? Output[k]
: never;
}>,
// OldInput = util.flatten<Omit<Input, keyof Augmentation>>,
// AugInput = baseObjectInputType<Augmentation>,
// NewInput = OldInput & AugInput
NewInput extends util.flatten<{
[k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
? Augmentation[k]["_input"]
: k extends keyof Input
? Input[k]
: never;
}>
// AKeys extends string | number | symbol = keyof Augmentation,

// AKeys extends string | number | symbol = keyof Augmentation
>(
// const AugmentFactory =
// <Def extends ZodObjectDef>(def: Def) =>
// <Augmentation extends ZodRawShape>(
// augmentation: Augmentation
// ): ZodObject<
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
// Def["unknownKeys"],
// Def["catchall"]
// > => {
// return new ZodObject({
// ...def,
// shape: () => ({
// ...def.shape(),
// ...augmentation,
// }),
// }) as any;
// };
extend<Augmentation extends ZodRawShape>(
augmentation: Augmentation
): ZodObject<
extendShape<T, Augmentation>,
UnknownKeys,
Catchall,
NewOutput,
NewInput
> {
): ZodObject<extendShape<T, Augmentation>, UnknownKeys, Catchall> {
return new ZodObject({
...this._def,
shape: () => ({
Expand All @@ -2164,6 +2147,39 @@ export class ZodObject<
}),
}) as any;
}
// extend<
// Augmentation extends ZodRawShape,
// NewOutput extends util.flatten<{
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
// ? Augmentation[k]["_output"]
// : k extends keyof Output
// ? Output[k]
// : never;
// }>,
// NewInput extends util.flatten<{
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
// ? Augmentation[k]["_input"]
// : k extends keyof Input
// ? Input[k]
// : never;
// }>
// >(
// augmentation: Augmentation
// ): ZodObject<
// extendShape<T, Augmentation>,
// UnknownKeys,
// Catchall,
// NewOutput,
// NewInput
// > {
// return new ZodObject({
// ...this._def,
// shape: () => ({
// ...this._def.shape(),
// ...augmentation,
// }),
// }) as any;
// }
/**
* @deprecated Use `.extend` instead
* */
Expand All @@ -2174,49 +2190,13 @@ export class ZodObject<
* inferred type of merged objects. Please
* upgrade if you are experiencing issues.
*/
// merge<Incoming extends AnyZodObject>(merging: Incoming) {
// return this.extend(merging.shape as Incoming["shape"]);
// }
merge<
Incoming extends AnyZodObject,
Augmentation extends Incoming["shape"],
// NewShape extends extendShape<T, Augmentation>,
// OldOutput = util.flatten<Omit<Output, keyof Augmentation>>,
// AugOutput = baseObjectOutputType<Augmentation>,
// NewOutput = OldOutput & AugOutput,
NewOutput extends {
[k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
? Augmentation[k]["_output"]
: k extends keyof Output
? Output[k]
: never;
},
// OldInput = util.flatten<Omit<Input, keyof Augmentation>>,
// AugInput = baseObjectInputType<Augmentation>,
// NewInput = OldInput & AugInput
NewInput extends {
[k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
? Augmentation[k]["_input"]
: k extends keyof Input
? Input[k]
: never;
}
// AKeys extends string | number | symbol = keyof Augmentation,

// AKeys extends string | number | symbol = keyof Augmentation
>(
merge<Incoming extends AnyZodObject, Augmentation extends Incoming["shape"]>(
merging: Incoming
): ZodObject<
extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
extendShape<T, Augmentation>,
Incoming["_def"]["unknownKeys"],
Incoming["_def"]["catchall"],
NewOutput,
NewInput
Incoming["_def"]["catchall"]
> {
// const mergedShape = objectUtil.mergeShapes(
// this._def.shape(),
// merging._def.shape()
// );
const merged: any = new ZodObject({
unknownKeys: merging._def.unknownKeys,
catchall: merging._def.catchall,
Expand All @@ -2226,6 +2206,41 @@ export class ZodObject<
}) as any;
return merged;
}
// merge<
// Incoming extends AnyZodObject,
// Augmentation extends Incoming["shape"],
// NewOutput extends {
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
// ? Augmentation[k]["_output"]
// : k extends keyof Output
// ? Output[k]
// : never;
// },
// NewInput extends {
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
// ? Augmentation[k]["_input"]
// : k extends keyof Input
// ? Input[k]
// : never;
// }
// >(
// merging: Incoming
// ): ZodObject<
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
// Incoming["_def"]["unknownKeys"],
// Incoming["_def"]["catchall"],
// NewOutput,
// NewInput
// > {
// const merged: any = new ZodObject({
// unknownKeys: merging._def.unknownKeys,
// catchall: merging._def.catchall,
// shape: () =>
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
// typeName: ZodFirstPartyTypeKind.ZodObject,
// }) as any;
// return merged;
// }

setKey<Key extends string, Schema extends ZodTypeAny>(
key: Key,
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "zod",
"version": "3.20.5",
"version": "3.20.6",
"description": "TypeScript-first schema declaration and validation library with static type inference",
"main": "./lib/index.js",
"types": "./index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion playground.ts
@@ -1,4 +1,6 @@
// import { z } from "./src";
import { z } from "./src";

z.number().int();

// const arg = z.enum(["a", "b"] as const);
// const arb = arg.exclude(["b"]);
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/object.test.ts
Expand Up @@ -228,7 +228,7 @@ test("inferred merged object type with optional properties", async () => {
.object({ a: z.string(), b: z.string().optional() })
.merge(z.object({ a: z.string().optional(), b: z.string() }));
type Merged = z.infer<typeof Merged>;
util.assertEqual<Merged, { a: string | undefined; b: string }>(true);
util.assertEqual<Merged, { a?: string; b: string }>(true);
// todo
// util.assertEqual<Merged, { a?: string; b: string }>(true);
});
Expand Down

0 comments on commit e693919

Please sign in to comment.