Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bool value of "on" as true #784

Open
bdoms opened this issue Dec 14, 2024 · 0 comments
Open

Support bool value of "on" as true #784

bdoms opened this issue Dec 14, 2024 · 0 comments

Comments

@bdoms
Copy link

bdoms commented Dec 14, 2024

Description

When creating a form in HTML we can use the checkbox input, which naturally maps to a bool in Python:

<input type="checkbox" name="is_example" checked />

Using FormData to submit that via a fetch or XMLHttpRequest request will follow the spec and product JSON in the request body like so:

{"is_example": "on"} 

However, msgspec doesn't recognize it, and produces this error:

{"status_code":400,"detail":"Expected `bool`, got `str` - at `$.is_example`"}

This is very confusing, because a checkbox is essentially a bool, and other frameworks will usually do things like accept any truthy value. Luckily, there is a workaround right now by adding value="1" to the checkbox:

<input type="checkbox" name="is_example" checked value="1" />

I suspect there are probably other values that work as well. But I'd argue that "1" is just as much a str or bool as "on" is, and the official standard has to use "on" for checkboxes since the form element was added to HTML 2 in 1995, so it should probably be supported natively.

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

No branches or pull requests

2 participants
@bdoms and others