zod
zod copied to clipboard
deepPartial doesn't work with superRefine
Given a schema with an object child which has superRefine, the deepPartial on the parent schema does not make the child's entries optional
Hi @dstoyanoff, You mean:
import { z } from 'zod';
const user = z.object({
username: z.string().superRefine((value) => value.length >= 1),
location: z.object({
latitude: z.number(),
longitude: z.number(),
}),
strings: z.array(z.object({ value: z.string() })),
});
const deepPartialUser = user.deepPartial();
type AAA = z.infer<typeof deepPartialUser>;
Hello @chanthinh, its more like this:
import { z } from 'zod';
const user = z.object({
username: z.string(),
location: z.object({
latitude: z.number(),
longitude: z.number(),
}).superRefine(() => false),
strings: z.array(z.object({ value: z.string() })),
});
const deepPartialUser = user.deepPartial();
type AAA = z.infer<typeof deepPartialUser>;
here is a codesandbox - https://codesandbox.io/p/devbox/polished-rain-738nnl?file=%2Fsrc%2Findex.ts%3A23%2C1
@dstoyanoff, You can check this one: https://stackblitz.com/edit/typescript-zgzrzk?file=schemas%2FschemaWithDefaultType.ts
and change strict to false
Hope this help you
{
"include": ["./src/*"],
"compilerOptions": {
"esModuleInterop": true,
"strict": false
}
}
Thanks @chanthinh, but Zod itself recommends that strict is enabled, so this change is rather controversial.
@dstoyanoff, yah I know, glad to help you
and change strict to false
I highly recommend that you don't do this. This will cause other problems and so strict should always be used for Zod.
This does seem to be a bug, however, I think that deepPartial
is in process of being deprecated. So I'm not sure this will be fixed, but I could be wrong.
and change strict to false
I highly recommend that you don't do this. This will cause other problems and so strict should always be used for Zod.
This does seem to be a bug, however, I think that
deepPartial
is in process of being deprecated. So I'm not sure this will be fixed, but I could be wrong.
I see that it's getting deprecated, but at the moment there is no suggested alternative in the docs. Happy for an alternative solution to deepPartial.
Hi @dstoyanoff, You mean:
import { z } from 'zod'; const user = z.object({ username: z.string().superRefine((value) => value.length >= 1), location: z.object({ latitude: z.number(), longitude: z.number(), }), strings: z.array(z.object({ value: z.string() })), }); const deepPartialUser = user.deepPartial(); type AAA = z.infer<typeof deepPartialUser>;
@chanthinh "refine" method suffices for custom validation, just want to know why superRefine is used in this case, which is better for customizing error codes.