@k8ordo/form

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 the value a checked box submits
  • z.file(): type="file"
  • A string with input: "password" metadata: type="password"
  • z.enum([…]): no type; spread it onto a <select>
  • z.array(z.enum([…])): no type; 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 as step. .int() alone gives step="1", and neither gives step="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 as type="date"
  • .mime() on a file. accept only filters the file picker, and the browser never checks the type
  • Size bounds (.min(), .max()) on a file
  • A z.iso.datetime() that no datetime-local control can satisfy
  • .min() on a checkbox group; declare minChecked to run it in the browser too
  • Constraints after a .transform() or z.custom(), which the schema does not expose
Warning

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. Use z.coerce
  • z.record, tuples, nullable objects: there is no single name to submit under
  • A repeat nested inside a repeat: the indexes in name cannot be determined
  • Keys holding . or brackets: they cannot be told apart from the separators in name
  • z.stringbool().default(true): unchecking the box could never submit false

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
k8ordo

React libraries that use Baseline features without holding back.