Describe the bug
The function 'toFormikValidationSchema' throws a null ref exception because zod throws an null ref exception.
I'm not sure at which party this should be fixed but the simplest way was to not bubble up the zod exception, if this is the desired behavior.
Zod error
TypeError: Cannot read properties of undefined (reading 'status')
at ParseStatus.mergeArray (index.mjs:408:19)
zod-formik-adapter error
TypeError: Cannot read properties of undefined (reading 'map')
at createValidationError (index.js:68:28)
To Reproduce
I'll try to reproduce it within tests as i have time.
My schema is:
const measuringSchema = z
.object({
measuringId: z.string().uuid().optional(),
value: z.string(),
})
.optional()
const measurementSchema = z.object({
measurementId: z.string().uuid(),
values: z.array(measuringSchema).optional(),
})
const formSchema = z.object({
items: z.array(measurementSchema).optional(),
})
type FormSchemaType = z.infer<typeof formSchema>
My form data is:
const data: FormSchemaType = {
items: [
{
measurementId: 'c66ee4a8-fef9-41a8-94b9-36522338db01',
values: [
{
value: '405',
},
],
},
{
measurementId: '91e8b93c-895e-430c-af28-15f3b0dec3a0',
values: [
{
measuringId: '225fd710-914e-4579-be13-103eb02c76ae',
value: '1',
},
],
},
{
measurementId: '17d0940b-ace1-4e2d-9314-93e30ae67edb',
values: [
+ null!,
{
measuringId: 'b3d25b19-7020-4e5a-88da-fe7648c8b635',
value: '999',
},
],
},
],
}
Expected behavior
No exception and handling all other schema validations. Please correct me.
Desktop (please complete the following information):
- OS: windows
- Browser brave
- Version
Node version:
node v16.17.0
Zod, Formik and zod-formik-adapter versions:
"zod": "^3.17.3",
"formik": "^2.2.9",
"zod-formik-adapter": "^1.1.1",
Additional context
My current fix to avoid this behavior:
function createValidationError(e: z.ZodError) {
+ if (!e?.errors) {
+ return new ValidationError('')
+ }
const error = new ValidationError(e.message)
error.inner = e.errors.map(err => ({
message: err.message,
path: err.path.join('.'),
}))
return error
}