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

fix: Boolean Type Transformation goes wrong #711

Closed
somehowchris opened this issue May 2, 2021 · 2 comments
Closed

fix: Boolean Type Transformation goes wrong #711

somehowchris opened this issue May 2, 2021 · 2 comments
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@somehowchris
Copy link

Description

Using @Type(() => Boolean does not work as advertised here.

Minimal code-snippet showcasing the problem
I have a minimal app to show the whole scenario https://github.com/somehowchris/nestjs-query-validation-failing

Doing:

curl --location --request POST 'http://localhost:3001/?name=Chris&external=false&age=12' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name":"Chris",
    "external": "false",
    "age": "12"
}'

Should transform "false" to the type Boolean with false

import { Type } from 'class-transformer';
import { IsBoolean, IsInt, IsPositive, IsString } from 'class-validator';

export class AppSearchQuery {
  @IsString()
  public name: string;
  @IsBoolean()
  @Type(() => Boolean)
  public external: boolean;
  @IsInt()
  @IsPositive()
  @Type(() => Number)
  public age: number;
}

Which again should result in the return body

{
    "name": "Chris",
    "external": false,
    "age": 12
}

but results in this

{
    "name": "Chris",
    "external": true,
    "age": 12
}

Expected behavior

The boolean should be transformed with its actual value.

Actual behavior

As shown the boolean value is true which should be false.

@somehowchris somehowchris added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels May 2, 2021
@diffy0712
Copy link

Hello @somehowchris,

This works as expected. Any non-empty string value is transformed to boolean true by Javascript.
You can force the 'correct' behavior, that you expect with a custom transformer.
Example:

@Transform(({ value }) => {
  if (value === 'true') return true;
  if (value === 'false') return false;
  return value;
})

There is a feature issue on allow casting of primitive values during transformation #550.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

2 participants