# Id validation

Currently, if we access the <mark style="color:blue;">`findOne()`</mark> route, we can pass any kind of <mark style="color:blue;">`id`</mark> and it is simply converted to <mark style="color:blue;">`number`</mark>.  When we have a database set up and running, it will search for it, whatever it is. However, we could validate it before this happens. And that's what we'll do now, using a **DTO**.

Let's create a DTO to represent an <mark style="color:blue;">`id`</mark> in <mark style="color:purple;">common</mark>/<mark style="color:purple;">dto</mark>/<mark style="color:purple;">id.dto</mark>. Notice its validation, as a **serial id** is a **positive integer**.

```typescript
export class IdDto {
  @IsInt()
  @IsPositive()
  readonly id: number;
}
```

Now, in the <mark style="color:blue;">`UsersController`</mark>, we can alter the routes that receive an <mark style="color:blue;">`id`</mark>, changing these occurrences:

```typescript
@Param('id') id: string
```

For this:

```typescript
@Param() { id }: IdDto
```

Now, the <mark style="color:blue;">`id`</mark> is properly validated and typed, and also already extracted from the DTO. Still, all path/query params come as a <mark style="color:blue;">`string`</mark>. To automatically convert the incoming <mark style="color:blue;">`id`</mark> to its corresponding type in the DTO, let's add this last option in the <mark style="color:blue;">`ValidationPipe`</mark>.

```typescript
transformOptions: {
  enableImplicitConversion: true,
}
```

This will enable automatic conversion of path/query params to their corresponding primitive types. We can then remove the **number conversion operator** (<mark style="color:blue;">`+`</mark>) from the routes, as it became unnecessary.

<mark style="color:green;">**Commit**</mark> - Applying validation
