[Suggestion] Stricter boolean coerce option
See original GitHub issueSince we’re currently using Svelte Kit with form actions that make heavy use of the FormData browser api, the new Zod 3.20 release is a very much welcome improvement, specifically the new coercion functionality (since all form post values are serialized as strings over the wire).
One suggestion/request I have is to allow boolean coerce to take a strict
option that would check for explicit boolean strings:
Current:
const schema = z.coerce.boolean();
schema.parse("true"); // => true
schema.parse("false"); // => true
Suggestion:
const schema = z.coerce.boolean({ strict: true });
schema.parse("true"); // => true
schema.parse("false"); // => false
schema.parse("123"); // => ZodError
Issue Analytics
- State:
- Created 10 months ago
- Reactions:4
- Comments:9 (4 by maintainers)
Top Results From Across the Web
typescript-eslint/strict-boolean-expressions.md at main - GitHub
Forbids usage of non-boolean types in expressions where a boolean is expected. boolean and never types are always allowed. Additional types which are ......
Read more >4. Coercion - You Don't Know JS: Types & Grammar [Book]
Boolean (a) and !!a are far better as explicit coercion options. Implicit Coercion. Implicit coercion refers to type conversions that are hidden, ...
Read more >What exactly is Type Coercion in Javascript? - Stack Overflow
Type coercion is the process of converting value from one type to another (such as string to number, object to boolean, and so...
Read more >Parameters — Click Documentation (7.x)
automatic prompting for missing input · act as flags (boolean or otherwise) · option values can be pulled from environment variables, arguments can...
Read more >Boolean - JavaScript - MDN Web Docs
Boolean coercion · Booleans are returned as-is. · undefined turns into false . · null turns into false . · 0 , -0...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It is supported since Node 18.0.0 (see https://developer.mozilla.org/en-US/docs/Web/API/FormData)
Ah good catch.
@pheuter want to try to propose an API/behavior that would work for you? I’ve never gotten very far here. Some open questions:
types
that need to be supported.get
vs.getAll
on a given fieldJSON.parse
the valueFormData
coercion? Should"on"
be converted totrue
?