> For the complete documentation index, see [llms.txt](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/improvements-tips-module/improvements/soft-delete/fix-boolean-casting.md).

# Fix - Boolean casting

We'll use a boolean as query parameter to indicate whether we want a hard ou soft deletion. But before doing this, we need to fix something first.

Remember that option we have set in the validation pipe options, <mark style="color:blue;">`enableImplicitConversion`</mark>? It was responsible for automatically converting path/query parameters to their corresponding primitive types. The <mark style="color:blue;">`id`</mark>, for example, would arrive from the network as a string (<mark style="color:blue;">`"1"`</mark>) instead of a number (<mark style="color:blue;">`1`</mark>). So, this option automatically took care of this for us by transforming the variable.

However, with booleans, it does not work as one may expect. The reason is that it converts any non-empty string into <mark style="color:blue;">`true`</mark>, and an empty string into <mark style="color:blue;">`false`</mark>. If we decorate a variable in a DTO with the <mark style="color:blue;">`@IsBoolean()`</mark> decorator, it will always be valid, due to this reason. Obviously, this should not be the case. The string <mark style="color:blue;">`"true"`</mark>/<mark style="color:blue;">`"false"`</mark> should be converted to the boolean <mark style="color:blue;">`true`</mark>/<mark style="color:blue;">`false`</mark>, and anything else should be rejected.

What we'll do to fix this is to create the decorator <mark style="color:blue;">`@ToBoolean()`</mark>, which will make the correct boolean casting. But we'll not use it directly. We'll also create a new <mark style="color:blue;">`@IsBoolean()`</mark> decorator, which will be a combination of the default one with this new transformation decorator. As transformations always occur before validations, it is guaranteed that the boolean will be correctly transformed, and then correctly validated.

Let's then begin by creating the file <mark style="color:purple;">common</mark>/<mark style="color:purple;">decorators</mark>/<mark style="color:purple;">to-boolean.decorator</mark>. Here, first we'll create a function to make the correct boolean casting.

```typescript
const toBoolean = (value: unknown) => {
  switch (value) {
    case null:
      return 'Failure';

    case 'true':
      return true;
    case 'false':
      return false;

    default:
      return value;
  }
};
```

{% hint style="info" %}
Some things to consider:

* Here, the <mark style="color:blue;">null</mark> value is forbidden because there is not much sense in a null boolean.
* You can create two new folders inside <mark style="color:purple;">common</mark>/<mark style="color:purple;">decorators</mark> if you want: <mark style="color:purple;">validators</mark> and <mark style="color:purple;">transformers</mark>. This may ensure a better organization.
  {% endhint %}

The next step is to create a decorator that will transform a value using that function. We still haven't seen the <mark style="color:blue;">`@Transform()`</mark> decorator yet, but it allows to transform a value. It is always executed before validation decorators. A very basic example would be to transform the value into a <mark style="color:blue;">`number`</mark>, like this:

```typescript
@Transform(({ value }) => +value)
```

But in our case, we need to access the object before it is automatically transformed. To achieve this, we can use the <mark style="color:blue;">`obj`</mark> and <mark style="color:blue;">`key`</mark> fields. <mark style="color:blue;">`obj`</mark> refers to the object before this transformation, and <mark style="color:blue;">`key`</mark> is the name of the field receiving the decorator. So we can define our decorator as follows:

```typescript
export const ToBoolean = () => Transform(({ obj, key }) => toBoolean(obj[key]));
```

> The solution to this situation was inspired by [this answer](https://stackoverflow.com/questions/59046629/boolean-parameter-in-request-body-is-always-true-in-nestjs-api/66697810#66697810) from **David Kerr** (Stack Overflow).

Lastly, create the file <mark style="color:purple;">is-boolean.decorator</mark>. As it will have the same name of the original decorator, we'll need to give it an alias to avoid a conflict. So, put at the top of the file:

```typescript
import { IsBoolean as DefaultIsBoolean } from 'class-validator';
```

And now, we can create our own <mark style="color:blue;">`@IsBoolean()`</mark> decorator with correct transformation.

```typescript
/** Checks if the value is a boolean. Works with query params. */
export const IsBoolean = (
  validationOptions?: ValidationOptions,
): PropertyDecorator =>
  applyDecorators(DefaultIsBoolean(validationOptions), ToBoolean());
```

<mark style="color:green;">**Commit**</mark> - Correctly transforming booleans


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/improvements-tips-module/improvements/soft-delete/fix-boolean-casting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
