question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Suggestion] Stricter boolean coerce option

See original GitHub issue

Since 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:open
  • Created 10 months ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kibertoadcommented, Dec 5, 2022

It is supported since Node 18.0.0 (see https://developer.mozilla.org/en-US/docs/Web/API/FormData)

1reaction
colinhackscommented, Dec 12, 2022

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:

  • The set of input types that need to be supported
  • Whether to call .get vs .getAll on a given field
  • Whether to JSON.parse the value
  • Special FormData coercion? Should "on" be converted to true?
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found