1.1.1 • Published 2 years ago

@hippo-oss/nest-dto v1.1.1

Weekly downloads
1,858
License
MIT
Repository
github
Last release
2 years ago

nest-dto

NestJS DTO decorator composition.

What Problem Does This Project Solve?

Data transfer objects (DTOs) in the NestJS ecosystem typically rely on decorators from several libraries, including:

As a result, a single DTO property often needs to be decorated several times and needs each of these decorators to be defined consistently. Over the course of a large project with many DTOs, a large amount of code will be dedicated to these definitions and small inconsistencies often creep into the definitions.

nest-dto provides a simplified set of decorators that compose over the library decorators.

An Example

A DTO for an HTTP API that generates an OpenAPI spec might need all of the following decorators for a single property:

import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsDefined, IsInt } from 'class-validator';

export class ExampleDTO {

    @Type(() => Number)
    @IsDefined()
    @IsInt()
    @ApiProperty({
        description: 'An example value',
        type: integer,
    })
    public value!: number;
}

With nest-dto, this declaration becomes:

import { IsInteger } from '@hippo-oss/nest-dto/openapi';


export class ExampleDTO {

    @IsInteger({
        description: 'An example value',
    })
    public value!: number;
}

Flavors

Different combinations of the library decorators produce different behaviors. nest-dto defines collections of foundational decorators (e.g. IsString or IsNumber) in "flavors" that use specific combination of these libraries.

FlavorDescription
strictIntegrates class-transformer and class-validator and always uses @Expose()
openapiIntegrates class-transformer, class-validator, and @nestjs/swagger

Decorators

The following decorators are supported:

DecoratorDescription
IsArrayDeclares an array of a nested type.
IsBooleanDeclares a boolean value.
IsDateDeclares a Date value.
IsDateStringDeclares an ISO 8601 date string.
IsEnumDeclares an enumerated value.
IsIntegerDeclares an integer number.
IsNestedDeclares a nested object type.
IsNumberDeclares a floating point number.
IsStringDeclares a string.
IsUUIDDeclares a UUID string.

Decorator Options

Decorators may be passed various options, depending on their type.

All options are optional expect where indicated.

OptionDecoratorDescription
descriptionallDescription of the field; exposed in OpenAPI.
exposeallEnables alternate name to be set for the field.
isArrayallDesignates an array of values.
nameallAlternate name of the field; exposed in OpenAPI.
nullableallWhether the field can be set to null.
optionalallWhether the field be set to undefined or omitted.
deprecatedallWhether the field appears as deprecated
----------------------------------------------------------------------------------
type (required)IsArrayThe type of the array's items.
maxItemsIsArrayThe maximum number of array items.
minItemsIsArrayThe minimum number of array items.
----------------------------------------------------------------------------------
formatIsDateThe OpenaPI date format; defaults to date-time.
----------------------------------------------------------------------------------
formatIsDateStringThe OpenAPI date format; defaults to date.
----------------------------------------------------------------------------------
enum (required)IsEnumThe enum type.
enumNameIsEnumThe enum name; required to correctly export OpenAPI
----------------------------------------------------------------------------------
maxValueIsIntegerThe maximum value of the field.
minValueIsIntegerThe minimum value of the field.
----------------------------------------------------------------------------------
type (required)IsNestedThe nested type.
----------------------------------------------------------------------------------
maxValueIsNumberThe maximum value of the field.
minValueIsNumberThe minimum value of the field.
----------------------------------------------------------------------------------
maxLengthIsStringThe maximum length of the string.
minLengthIsStringThe minimum length of the string.
patternIsStringA regex pattern for validating the string.
----------------------------------------------------------------------------------
versionIsUUIDThe type of UUID.

Arrays

The easiest (and preferred) way to define arrays is to add the isArray argument to another decorator:

class Example {
   @IsString({
       isArray: true,
   })
   values!: string[];
}

The isArray option may be supplied as either the literal true or as ArraySizeOptions:

class Example {
   @IsString({
       isArray: {
           maxSize: 30,
           minSize: 0,
       },
   })
   values!: string[];
}

Note that while the IsArray decorator is also supported, it is less well-suited for defining arrays of value types using a consistent interface and may be deprecated in the future.

Enums

Enumerated types work pretty much as expected:

enum Color {
  Red = 'Red',
  Blue = 'Blue',
  Green = 'Green',
}

class Example {
   @IsEnum({
      enum: Color,
      enumName: 'Color',
   })
   color!: Color;
}

The enumName value is optional, but if omitted causes OpenAPI generation to treat the enum as a value type rather than as $ref to a shared type. This choice may matters when generating code from an OpenAPI spec because most code generators will produce different types for every enum value, even if they share the same enumerated values. Defining an enumName (and therefore a $ref declaration) ensures that generated code sees each use of the same enum as the same type.

Nested Types

Decorator values that use another object type should be decorated with IsNested:

class Child {
    @IsString()
    value!: string;
}

class Parent {
     @IsNested({
         type: Child,
     })
     child!: Child;
}

Every child type is expected to define at least one decorator field. Failure to do so may result in errors from class-transformer.

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.19.0

2 years ago

0.18.0

2 years ago

0.17.0

3 years ago

0.14.0

3 years ago

0.15.0

3 years ago

0.16.0

3 years ago

0.13.1

3 years ago

0.13.0

3 years ago

0.12.0

3 years ago

0.11.0

3 years ago

0.10.0

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago