Skip to content

feat: zodResolver carries arrays, coerced numbers and password rules - #17

Closed
mertcanaltin wants to merge 2 commits into
feat/zod-reportfrom
feat/zod-arrays
Closed

mertcanaltin wants to merge 2 commits into
feat/zod-reportfrom
feat/zod-arrays

Conversation

@mertcanaltin

Copy link
Copy Markdown
Member

Stacked on #16; the diff here is the one commit on top of it.

Three more things forms are written with, each found by running the scan of #16 over the corpus of 150 public files and reading its reasons.

Arrays. z.array(...) of anything carried, with min, max, length and nonempty. What had to be matched, each found by the differential and now held by it:

  • an item's issues come before the array's own, in item order;
  • an array longer than its maximum reports only that: zod does not read the items of an oversized array (capSize in zod's source), so their issues are dropped;
  • a length check also runs on a value that is not an array but has a length, a string say, worded by what it was given;
  • the data handed back is rebuilt item by item, unknown keys of an item dropped.

In the tables an item is a * segment, and an instance path is mapped to its pattern on the way down.

z.coerce.number(). zod takes Number(value) of whatever came before it checks, so "" is 0 and "abc" is NaN. The same is done to a copy of the document before the compiled check runs (the mechanism trim uses), an optional one left alone when it is not there. Coercion to a string or a boolean is refused: a missing key becomes "undefined" or false there, which a JSON Schema has no way to say.

Several regex checks on one field, the usual way to write a password rule. The plan could not tell them apart when it asked zod about each; it now matches the issue by its pattern.

Also: the refusals that read like internals now say what the developer wrote (a field that takes a missing value without being optional, the build has no sample value that passes this field's rules), and the build-time check has more sample values (a name starting with @, a password, digits).

Reach on the corpus (156 calls, each file alone in a directory): 52 replaced before this, 56 after. Of the 100 left: 64 import the schema from a module the one-file setup does not have, 12 declare it inside the component, 10 have a function in it (a refine, mostly), 4 use url, and 10 others.

Size: the run-time code that maps paths and orders issues is written into every file with a replaced call, with or without an array in it. The five-field form of #12 is 15,312 B under Turbopack and 15,837 B under webpack, 189 and 195 B more than before this. One code path was kept on purpose: it is the one the build-time check and the differential run, and a second one for forms without arrays would be held by neither.

Tests: arrays, coerced numbers and password rules in the generated forms; a fixed case for coerced numbers; a build with arrays of strings and of objects; seven ways of breaking the new run-time code, each caught. npm test: 264 tests, 262 pass, 2 skipped.

Not covered: an array of optional items, two length checks of one kind on an array, a tuple, a set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant