Skip to content

question: why does @Type(() => Boolean) always return true? #676

Closed as not planned
@Pacheco95

Description

@Pacheco95

I was trying to...
Parse an NestJs http request params to a filter class but the property I'm filtering is a boolean which is always being converted to true.

The problem:
Nest will parse the query params to an object whose properties keys are the same passed to the browser but the values are all converted to strings. So the object taken by class-transformer is something like this:

URL: http://example.com/?clt=something

{
  "clt": "something"
}

And I need to convert this stringified object to my filter class:

import { IsBoolean } from 'class-validator';
import { Expose, Type } from 'class-transformer';

export class EmployeeFilter {
  @Expose()
  @IsBoolean()
  @Type(() => Boolean)
  clt?: boolean
}

If I pass false to the url, the parsed object still with clt=true

Am I missing something?

Doc: https://github.com/typestack/class-transformer#working-with-nested-objects
Codesandbox https://codesandbox.io/s/nestjs-forked-4x3qd?file=/src/validation.pipe.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: duplicateIssue is being tracked already in another issue.type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions