Skip to content
Discussion options

You must be logged in to vote

The optional type comes from .default([]) on cc. It allows undefined as input and produces an array after parsing. React Hook Form types the field using the schema's input, so field.value can be undefined even though your database value is string[].

You're already supplying cc in defaultValues. If you don't need the schema itself to accept a missing cc, remove the schema default:

cc: z.array(z.string().email()),

Keep defaultValues.cc as it is. The field is then required in the input type, and an empty array is still valid.

If you need to keep .default([]) for other callers of this schema, use const cc = field.value ?? [] inside the render callback, then use cc for values, .length, and .jo…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by muezz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants