auto-form
auto-form copied to clipboard
Error shown in a wrong field in Zod 4 Array schema
Since upgrading to the latest v5,Error shown in a wrong field in Zod 4 Array schema.
Schema is:
z.array(
z.object({
text: z.string().describe('Text'),
url: z
.string()
.regex(/^\/[^\s]*$/, 'URL must be a relative path starting with /')
.describe('URL (e.g., /docs/x)'),
type: z.enum(['text', 'video']).optional().describe('Type'),
})
)
and i'm seeing an error that is suppose to be only in the url field also in the type field
The issue is caused by the FieldWrapper showing errors for array and object fields, which isn’t necessary.
Please update your FieldWrapper component to the following:
import React from "react";
import { Label } from "@/components/ui/label";
import { FieldWrapperProps } from "@autoform/react";
const DISABLED_LABELS = ["boolean", "object", "array"];
export const FieldWrapper: React.FC<FieldWrapperProps> = ({
label,
children,
id,
field,
error,
}) => {
const isDisabled = DISABLED_LABELS.includes(field.type);
const hideError = ["array", "object"].includes(field.type);
return (
<div className="space-y-2">
{!isDisabled && (
<Label htmlFor={id}>
{label}
{field.required && <span className="text-destructive"> *</span>}
</Label>
)}
{children}
{field.fieldConfig?.description && (
<p className="text-sm text-muted-foreground">
{field.fieldConfig.description}
</p>
)}
{!hideError && error && (
<p className="text-sm text-destructive">{error}</p>
)}
</div>
);
};
Example: https://codesandbox.io/p/github/adityacodepublic/project2/dualError
The fixes for this problem will be included in the upcoming changes.