Skip to content

Commit

Permalink
Support brands in recursive types
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 4, 2023
1 parent 6ef82ee commit 5463593
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
6 changes: 5 additions & 1 deletion deno/lib/types.ts
Expand Up @@ -4630,7 +4630,11 @@ export type BRAND<T extends string | number | symbol> = {
export class ZodBranded<
T extends ZodTypeAny,
B extends string | number | symbol
> extends ZodType<T["_output"] & BRAND<B>, ZodBrandedDef<T>, T["_input"]> {
> extends ZodType<
T["_output"] & BRAND<B>,
ZodBrandedDef<T>,
T["_input"] & BRAND<B>
> {
_parse(input: ParseInput): ParseReturnType<any> {
const { ctx } = this._processInputParams(input);
const data = ctx.data;
Expand Down
37 changes: 11 additions & 26 deletions playground.ts
@@ -1,28 +1,13 @@
import { z } from "./src";
const example1 = z.custom<number>((x) => typeof x === "number");
example1.parse("asdf");
// const example1 = z
// .custom<number>(
// (x) => {
// console.log(`custom`);
// console.log(x);
// return typeof x === "number";
// },
// {},
// true
// )
// .transform((x) => {
// console.log(`transform`);
// console.log(x);
// return String(x);
// })
// .refine((x) => {
// console.log(`refine`);
// console.log(x);
// console.log(typeof x); // prints 'Object'
// console.log("I get called even though I shouldn't!!!");
// return true;
// })
// .safeParse({}); //will fail because it is not a number
const baseCategorySchema = z.object({
// - name: z.string(),
name: z.string().brand("CategoryName"),
});

// console.log(example1.success); // false (like it should be)
type Category = z.infer<typeof baseCategorySchema> & {
subcategories: Category[];
};

const categorySchema: z.ZodType<Category> = baseCategorySchema.extend({
subcategories: z.lazy(() => categorySchema.array()),
});
6 changes: 5 additions & 1 deletion src/types.ts
Expand Up @@ -4630,7 +4630,11 @@ export type BRAND<T extends string | number | symbol> = {
export class ZodBranded<
T extends ZodTypeAny,
B extends string | number | symbol
> extends ZodType<T["_output"] & BRAND<B>, ZodBrandedDef<T>, T["_input"]> {
> extends ZodType<
T["_output"] & BRAND<B>,
ZodBrandedDef<T>,
T["_input"] & BRAND<B>
> {
_parse(input: ParseInput): ParseReturnType<any> {
const { ctx } = this._processInputParams(input);
const data = ctx.data;
Expand Down

0 comments on commit 5463593

Please sign in to comment.