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

Feature: On / Off per HTML spec as boolean castable string values #8019

Closed
DevBrent opened this issue Jul 25, 2019 · 1 comment
Closed

Feature: On / Off per HTML spec as boolean castable string values #8019

DevBrent opened this issue Jul 25, 2019 · 1 comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@DevBrent
Copy link

DevBrent commented Jul 25, 2019

Do you want to request a feature or report a bug? Feature

What is the current behavior? "on" is no longer cast as true for Boolean fields.

What is the expected behavior? "on" and "off" should be cast as true/false like "yes" and "no"

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. 5.6.6

It looks like in Mongoose 5.X only specific string values are permitted to be cast as Boolean. The default value for a checkbox in HTML spec is "on".

By default, unchecked checkboxes simply do not pass a value when forms are serialized but it might make sense to include an "off" counterpart for falsey conversion.

Thoughts? This can improve native compatibility with form submissions from HTML forms. I am not certain of potential negative impacts. This also restores Pre-5.X functionality for input checkbox form submissions while maintaining the improvements in boolean casting introduced.

While I agree that converting any non falsy value to true is too permissive, "on" and "off" are clearly boolean values.

Related:
#5344 (comment)

@vkarpov15
Copy link
Collaborator

We'll consider adding this in a minor release since it is fairly common. However, adding support for 'on/off' is a two-liner, see boolean schematype docs.

mongoose.Schema.Types.Boolean.convertToTrue.add('on');
mongoose.Schema.Types.Boolean.convertToFalse.add('off');

@vkarpov15 vkarpov15 added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Jul 29, 2019
@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Jul 29, 2019
@vkarpov15 vkarpov15 removed this from the 5.x Unprioritized milestone May 18, 2021
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature labels May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants