Schema mapping
The control and attributes each zod schema derives, and what an empty control submits. It also lists the checks the browser skips and the schemas that are rejected.
On this page
Control types
The type in input for each kind of schema:
z.string():type="text"z.email(),z.url():type="email",type="url"z.iso.date(),z.iso.time():type="date",type="time"z.iso.datetime({ local: true }):type="datetime-local"z.coerce.number():type="number"z.boolean(),z.literal(true):type="checkbox"z.stringbool():type="checkbox"with thevaluea checked box submitsz.file():type="file"- A string with
input: "password"metadata:type="password" z.enum([…]): notype; spread it onto a<select>z.array(z.enum([…])): notype; one checkbox per option- Anything else (
z.uuid(),z.coerce.date(), …):type="text"
A check such as .regex() stacked on z.email() or z.iso.date() does not change the control type.
Constraints and attributes
Schema constraints become these attributes:
.min(),.max()on a string:minLength,maxLength.regex():pattern, when it is the only regex and has no flags.min(),.max()on a number:min,max.multipleOf(): that value asstep..int()alone givesstep="1", and neither givesstep="any".mime()on a file:accept- When what an empty control submits does not match the schema:
required
Empty values
parseForm hands the schema these values for an empty control:
- A text control:
"" - An unchecked
z.boolean()checkbox:false - A number, a
z.coerce.bigint()or a file: nothing (undefined) - A radio group with nothing selected, or a
<select>left on its placeholder: nothing (undefined) - An unchecked
z.stringbool()checkbox: nothing (undefined) - A checkbox group with nothing checked:
[]
Checks the browser skips
These checks cannot be expressed as HTML attributes, so they are listed in dropped. The server still runs every one.
- A
.refine()on the whole schema - An exclusive bound on a float (
.gt(),.lt()) - A regex with flags or anchors, and several regexes on one string
- A regex on a control that ignores
pattern, such astype="date" .mime()on a file.acceptonly filters the file picker, and the browser never checks the type- Size bounds (
.min(),.max()) on a file - A
z.iso.datetime()that nodatetime-localcontrol can satisfy .min()on a checkbox group; declareminCheckedto run it in the browser too- Constraints after a
.transform()orz.custom(), which the schema does not expose
Pitfall
A .refine() on a single field, a nested object or a row is not listed in dropped yet. The browser lets it through, and only the server checks it.
Rejected schemas
formFields and parseForm throw on these schemas. The message names the path and the reason, for example [@k8ordo/form] 'rows.tags': 繰り返しの中の繰り返しは name の添字が一意に決まらないため表現できません.
z.number(),z.bigint(),z.date(),z.literal(1): every submitted value is a string, so nothing could pass. Usez.coercez.record, tuples, nullable objects: there is no singlenameto submit under- A repeat nested inside a repeat: the indexes in
namecannot be determined - Keys holding
.or brackets: they cannot be told apart from the separators inname z.stringbool().default(true): unchecking the box could never submitfalse
Not supported yet
- Several files in one control.
z.array(z.file())derives repeated single-file rows - Rules on fields inside repeated rows
- Input masking. The value can be rewritten, but the caret position is not kept