# Pipe - JoiValidation

### Import

```typescript
import { JoiValidationPipe } from 'niro-health';
```

### Create DTO & Schema

{% code title="dto/create.ts" %}

```typescript
export class CreateUserDto {
  email: string;
  username: string;
  password: string;
}
```

{% endcode %}

{% code title="dto/schemas/create.ts" %}

```typescript
import * as Joi from 'joi';

import { CreateUserDto } from './dto/create';

export const CreateUserSchema = Joi.object<CreateUserDto>({
  email: Joi.string().email().required(),
  username: Joi.string().required(),
  password: Joi.string().required(),
});
```

{% endcode %}

### Create Parser

{% code title="parsers/index.ts" %}

```typescript
import { Injectable } from '@nestjs/common';
import type { RecursivePartial } from 'niro-health';
import type { User } from './entities';

@Injectable()
export class UsersParser {
  toJSON(user: User): RecursivePartial<User> {
    return {
      id: user.id,
      email: user.tag,
      username: user.value,
      createdAt: user.createdAt,
      updatedAt: user.updatedAt,
    };
  }
}
```

{% endcode %}

### Method of Use

{% code title="users.controller.ts" %}

```typescript
import {
  HttpException,
  HttpStatus,
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Query,
  Param,
  Delete,
  UsePipes,
  Inject,
} from '@nestjs/common';

import { JoiValidationPipe } from 'niro-health';

import { UsersService } from 'users.service';

import type { CreateUserDto } from './dto/create';

import { CreateUserSchema } from './dto/schemas/create.joi';

import { UsersParser } from './parsers';

@Controller('api/users')
export class UsersController {
  constructor(
    @Inject('IUsersService')
    private readonly usersService: UsersService,
    @Inject('IUsersParser')
    private readonly usersParser: UsersParser,
  ) {}

  @Post()
  @UsePipes(new JoiValidationPipe(CreateUserSchema))
  async create(@Body() createUserDto: CreateUserDto) {
    const key = await this.usersService.create(
      createUserDto,
    );

    if (key instanceof Error)
      throw new HttpException(key.message, HttpStatus.FORBIDDEN);

    return this.usersParser.toJSON(key);
  }
}
```

{% endcode %}


---

# Agent Instructions: 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://guilhermesantos.gitbook.io/niro-health/modules/core/pipe-joivalidation.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.
