2.6.1 • Published 3 months ago

nestjs-typebox v2.6.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

nestjs-typebox

This library provides helper utilities for writing and validating NestJS APIs using TypeBox as an alternative to class-validator/class-transformer. It also includes a patch for @nestjs/swagger allowing OpenAPI generation to continue working.

Warning

As of 2.x, this library is undergoing active development and will stabilize with the 3.x release. It was decided to drop support for the class-based DTO approach in favor of a pure decorator approach, since the class-based approach made it impossible to validate complex union types.

Installation

npm i nestjs-typebox @sinclair/typebox

Usage

1. Create TypeBox schema

The example below demonstrates a discriminated union type, which was previously incompatible with the class-based DTO approach used in v1. JSON schema fields like "description" will be parsed by the Swagger generator.

import { Type } from '@sinclair/typebox';

export const PetSchemaBase = Type.Object({
    id: Type.Number(),
    name: Type.String({
        description: "The pet's name",
        examples: ['Figaro'],
    }),
    microchip: Type.String(){
        description: 'Secret microchip number. Not sent to client'
    },
});

export const CatSchema = Type.Composite([
    PetSchemaBase,
    Type.Object({
        type: Type.Literal('cat'),
        breed: Type.Union([Type.Literal('shorthair'), Type.Literal('persian'), Type.Literal('siamese')]),
    }),
]);

export const DogSchema = Type.Composite([
    PetSchemaBase,
    Type.Object({
        type: Type.Literal('dog'),
        breed: Type.Union([Type.Literal('shiba-inu'), Type.Literal('poodle'), Type.Literal('dachshund')]),
    }),
]);

export const PetSchema = Type.Union([CatSchema, DogSchema]);
export type Pet = Static<typeof PetSchema>;

2. Decorate controller methods

The example below shows two different decorators and their usage, calling out default configuration. Schemas have all been defined inline for brevity, but could just as easily be defined elsewhere and reused. The primary benefit of using @HttpEndpoint over @Validator is the additional validation enforcing path parameters to be properly defined as request "param" validators.

import { Type } from '@sinclair/typebox';
import { Validate, HttpEndpoint } from 'nestjs-typebox';

@Controller('pets')
export class PetController {
    constructor(private readonly petService: PetService) {}

    @Get()
    @Validate({
        response: { schema: Type.Array(Type.Omit(PetSchema, ['microchip'])), stripUnknownProps: true },
    })
    async getPets() {
        return this.petService.getPets();
    }

    @Get(':id')
    @Validate({
        // stripUnknownProps is true by default for response validators
        // so this shorthand is equivalent
        response: Type.Omit(PetSchema, ['microchip']),
        request: [
            // coerceTypes is true by default for "param" and "query" request validators
            { name: 'id', type: 'param', schema: Type.Number(), coerceTypes: true },
        ],
    })
    // no need to use @Param() decorator here since the @Validate() decorator will
    // automatically attach a pipe to populate and convert the paramater value
    async getPet(id: number) {
        return this.petService.getPet(id);
    }

    @Post()
    @Validate({
        response: Type.Omit(PetSchema, ['microchip']),
        request: [
            // if "name" not provided, method name will be used
            { type: 'body', schema: Type.Omit(PetSchema, 'id') },
        ],
    })
    async createPet(data: Omit<Pet, 'id'>) {
        return this.petService.createPet(data);
    }

    @HttpEndpoint({
        method: 'PATCH',
        path: ':id',
        response: Type.Omit(PetSchema, ['microchip']),
        request: [
            { name: 'id', type: 'param', schema: Type.Number() },
            { type: 'body', schema: Type.Partial(Type.Omit(PetSchema, ['id'])) },
        ],
    })
    // the order of the controller method parameters must correspond to the order/types of
    // "request" validators, including "required" configuration. Additionally nestjs-typebox will
    // throw at bootup if parameters defined in the "request" validator config don't correspond
    // with the parameters defined in the "path" configuration
    async updatePet(id: number, data: Partial<Omit<Pet, 'id'>>) {
        return this.petService.updatePet(id, data);
    }

    @HttpEndpoint({
        method: 'DELETE',
        path: ':id',
        response: Type.Omit(PetSchema, ['microchip']),
        request: [{ name: 'id', type: 'param', schema: Type.Number() }],
    })
    async deletePet(id: number) {
        return this.petService.deletePet(id);
    }
}

3. Apply patch for OpenAPI/Swagger Support

As of 2.x, it is no longer necessary to register any interceptors/pipes, global or otherwise.

// main.ts

import { Reflector } from '@nestjs/core';
import { patchNestJsSwagger, applyFormats, TypeboxValidationPipe, TypeboxTransformInterceptor } from 'nestjs-typebox';

// provide swagger OpenAPI generator support
patchNestJsSwagger();

// provide custom JSON schema string format support
// currently only "email".
applyFormats();

async function bootstrap() {
    const app = await NestFactory.create(AppModule);

    await app.listen(3000);
    console.log(`Application is running on: ${await app.getUrl()}`);
}

bootstrap();

Credits

Swagger patch derived from https://github.com/risenforces/nestjs-zod

Todo

  • Validate observable support
  • utility to create typebox schemas with CRUD defaults (i.e. SchemaName'response', SchemaName'update')
  • include method name in decorator errors
  • support validating entire query object? (instead of individual values)
  • check controller metadata so resolved path can include params specified at the controller level
3.0.0-next.6

3 months ago

3.0.0-next.7

3 months ago

3.0.0-next.5

4 months ago

3.0.0-next.4

5 months ago

3.0.0-next.2

5 months ago

3.0.0-next.1

5 months ago

3.0.0-next.3

5 months ago

3.0.0-next.0

5 months ago

2.6.1

8 months ago

2.6.0

8 months ago

2.3.0

11 months ago

2.0.3

12 months ago

2.2.0

12 months ago

2.0.2

12 months ago

2.5.0

11 months ago

2.4.1

11 months ago

2.4.0

11 months ago

2.0.4

12 months ago

2.5.2

11 months ago

2.5.1

11 months ago

2.5.4

11 months ago

2.5.3

11 months ago

2.1.0

12 months ago

2.0.1

12 months ago

2.0.0

12 months ago

2.5.5

10 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

0.4.1

1 year ago

0.3.0

1 year ago

0.2.1

2 years ago

0.2.0

2 years ago

0.4.0

1 year ago

0.2.2

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago