Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class include other Class but with .nullish() or .optional() or .nullable() #28

Closed
hepr opened this issue Aug 21, 2024 · 3 comments · Fixed by #29
Closed

Class include other Class but with .nullish() or .optional() or .nullable() #28

hepr opened this issue Aug 21, 2024 · 3 comments · Fixed by #29

Comments

@hepr
Copy link

hepr commented Aug 21, 2024

My classes

export class ProductClass extends Z.class({
entityId: z.string(),
productId: z.string(),
productMain: z.string().nullable().optional(),
maxStockLevel: z.number().nullable().optional(),
minStockLevel: z.number().nullable().optional(),
stockTimeLimit: z.number().nullable().optional(),
sellAccount: z.string().toUpperCase().trim().nullable().optional(),
weight: z.number().min(0).default(0),
....
}) {
}

export class ProductInventoryLocationClass extends Z.class({
warehouseId: z.string(),
location: z.string(),
...
product: ProductClass.nullish(), // Crash with .nullish()
}) {

}

const productInventoryLocation = ProductInventoryLocationClass.parse(response)

Crash only when product field with .nullish() or .optional() or .nullable()

v0.0.14

TypeError: Cannot read properties of undefined (reading '_parse')
❯ ZodOptional._parse node_modules/zod/lib/index.mjs:3765:36
❯ ZodNullable._parse node_modules/zod/lib/index.mjs:3784:36
❯ ZodObject._parse node_modules/zod/lib/index.mjs:2379:37
❯ ZodObject._parseSync node_modules/zod/lib/index.mjs:678:29
❯ ZodObject.safeParse node_modules/zod/lib/index.mjs:708:29
❯ ZodObject.parse node_modules/zod/lib/index.mjs:689:29
❯ Function.parse node_modules/zod-class/src/index.ts:306:38

@sam-goodwin
Copy link
Owner

Thanks for reporting the error, I am able to reproduce it. Will fix.

@sam-goodwin
Copy link
Owner

sam-goodwin commented Aug 26, 2024

I've managed to fix the parse error but can't add nullish because of weird type errors. Also noticed that nullable and optional no longer retain the class and will break your types. Perhaps a typescript compiler change has broken that.

I wouldn't recommend using this library because zod wasn't designed to be extended this way. Perhaps it'll be fixed in a later zod version.

@sam-goodwin
Copy link
Owner

Fixed in 0.0.15. But, I would also caution against using this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants