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

Enhance boolean conversion logic for strings and numeric values #1776

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

GFoniX
Copy link

@GFoniX GFoniX commented Oct 24, 2024

Description

This update improves the boolean conversion logic when handling string values in the type mapping. The changes include:

  • Converting strings representing numbers (e.g., '1', '0') to booleans.
  • Handling specific string values like 'true' and 'false' to map them to their correct boolean counterparts.

This ensures more consistent and accurate conversions when dealing with strings that are either numeric or represent boolean values.

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

fixes #676, fixes #626

This fix ensures that query parameters sent as strings, which are common in NestJS, are correctly converted to booleans when using class-transformer. It prevents incorrect boolean conversions that previously resulted in all such values being interpreted as true.
For exemple have '1', '0', 'true', 'false' result to having true.

@GFoniX
Copy link
Author

GFoniX commented Nov 21, 2024

Hi @typestack,
I would like to know if there's anything missing for my PR to be approved.
Thank you!

@jeremycalvo
Copy link

Very interesting, might be very useful 👍

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

Successfully merging this pull request may close these issues.

question: why does @Type(() => Boolean) always return true? feat: allow casting strings to boolean
3 participants