Skip to content

Commit 5463593

Browse files
committed
Support brands in recursive types
1 parent 6ef82ee commit 5463593

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

Diff for: deno/lib/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4630,7 +4630,11 @@ export type BRAND<T extends string | number | symbol> = {
46304630
export class ZodBranded<
46314631
T extends ZodTypeAny,
46324632
B extends string | number | symbol
4633-
> extends ZodType<T["_output"] & BRAND<B>, ZodBrandedDef<T>, T["_input"]> {
4633+
> extends ZodType<
4634+
T["_output"] & BRAND<B>,
4635+
ZodBrandedDef<T>,
4636+
T["_input"] & BRAND<B>
4637+
> {
46344638
_parse(input: ParseInput): ParseReturnType<any> {
46354639
const { ctx } = this._processInputParams(input);
46364640
const data = ctx.data;

Diff for: playground.ts

+11-26
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
import { z } from "./src";
2-
const example1 = z.custom<number>((x) => typeof x === "number");
3-
example1.parse("asdf");
4-
// const example1 = z
5-
// .custom<number>(
6-
// (x) => {
7-
// console.log(`custom`);
8-
// console.log(x);
9-
// return typeof x === "number";
10-
// },
11-
// {},
12-
// true
13-
// )
14-
// .transform((x) => {
15-
// console.log(`transform`);
16-
// console.log(x);
17-
// return String(x);
18-
// })
19-
// .refine((x) => {
20-
// console.log(`refine`);
21-
// console.log(x);
22-
// console.log(typeof x); // prints 'Object'
23-
// console.log("I get called even though I shouldn't!!!");
24-
// return true;
25-
// })
26-
// .safeParse({}); //will fail because it is not a number
2+
const baseCategorySchema = z.object({
3+
// - name: z.string(),
4+
name: z.string().brand("CategoryName"),
5+
});
276

28-
// console.log(example1.success); // false (like it should be)
7+
type Category = z.infer<typeof baseCategorySchema> & {
8+
subcategories: Category[];
9+
};
10+
11+
const categorySchema: z.ZodType<Category> = baseCategorySchema.extend({
12+
subcategories: z.lazy(() => categorySchema.array()),
13+
});

Diff for: src/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4630,7 +4630,11 @@ export type BRAND<T extends string | number | symbol> = {
46304630
export class ZodBranded<
46314631
T extends ZodTypeAny,
46324632
B extends string | number | symbol
4633-
> extends ZodType<T["_output"] & BRAND<B>, ZodBrandedDef<T>, T["_input"]> {
4633+
> extends ZodType<
4634+
T["_output"] & BRAND<B>,
4635+
ZodBrandedDef<T>,
4636+
T["_input"] & BRAND<B>
4637+
> {
46344638
_parse(input: ParseInput): ParseReturnType<any> {
46354639
const { ctx } = this._processInputParams(input);
46364640
const data = ctx.data;

0 commit comments

Comments
 (0)