Nest: How to properly do parameter validation?

Created on 3 Jan 2018  路  12Comments  路  Source: nestjs/nest

What's the best way to validate "body" DTO's? I'm just worried by simple stuff such as making sure if all mandatory fields are defined and types are correct, for instance, if a field I declared to be a string is actually a string, etc.

For instance:

import { Get, Controller, Query, Param, Post, Body } from '@nestjs/common';

class MyType {
    name: string;
}

@Controller('example')
export class ExampleController {
    @Post() 
    createOne(@Body() item: MyType) {

    }
}

Will accept a payload such as:

{
    "name": 123
}

...without complaining.

question 馃檶

Most helpful comment

@panuhorsmalahti - What did you end up doing here? I have an endpoint that I'd like to POST an array to (body is array). Trying to validate using class-validator as well :)

All 12 comments

check out "class-validator" from cats example:
https://github.com/nestjs/nest/blob/master/examples/01-cats-app/src/cats/dto/create-cat.dto.ts
you just use annotations for your body model, then use ValidationPipe from that example:
https://github.com/nestjs/nest/blob/master/examples/01-cats-app/src/common/pipes/validation.pipe.ts

Then just use Validation pipe in controller (you can also find it in that example). It's amazingly easy and declarative way to do validation.

When you want to check validation of parameter aka :id you can do it via ParseInt pipe from that example or create your own custom if you have id with string type.

Hi @HeavyStorm,
Great explanation @pumano 馃檪 Take a look at the docs to find more details: https://docs.nestjs.com/pipes. Pipes will fit your requirements.

How would one verify a body which is an array?

app.useGlobalPipes(new ValidationPipe());
export class CreateMeasurementDto {
    @IsString() readonly time: string;
}



md5-7f72b108b9f45e00ca7ec2f4675381b5



    @Post()
    async measurements(@Req() request: Request, @Body() newMeasurements: CreateMeasurementDto[]) {

This doesn't seem to work (but works when CreateMeasurementDto is not an array.

@panuhorsmalahti body should be an object.

Why? As far as I know JSON array is valid in HTTP POST and body content type application/json? Or are you saying it's a limitation of Nest.js validation?

@panuhorsmalahti AFAIK you can create use an object and supply the array inside. Nest.js framework itself doesn't comes with validation, nest uses the validation pipe which is using class-validator library.

Honestly, I've never sent a req.body as an array.

I don't want to change my endpoint from [] to {data: []; }. Nest.js does come with some validation code, namely the ValidationPipe. Well, since it doesn't support validating arrays, I think this is an open feature request.

I could alternatively call class-validator manually in the controller, but I'd like to avoid that solution. I could also implement my own ValidationPipe, but it's better to get it fixed upstream.

It isn't Nest limitation, but a TypeScript one. We cannot reflect generic types.

The links to these examples have been updated:

check out "class-validator" from cats example:
/examples/01-cats-app/src/cats/dto/create-cat.dto.ts@master

https://github.com/nestjs/nest/blob/master/sample/01-cats-app/src/cats/dto/create-cat.dto.ts

you just use annotations for your body model, then use ValidationPipe from that example:
/examples/01-cats-app/src/common/pipes/validation.pipe.ts@master

https://github.com/nestjs/nest/blob/master/sample/01-cats-app/src/common/pipes/validation.pipe.ts

@panuhorsmalahti - What did you end up doing here? I have an endpoint that I'd like to POST an array to (body is array). Trying to validate using class-validator as well :)

How to validate what all items is number in array

async getRates(@Body('ids') ids: number[]): Promise {

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

2233322 picture 2233322  路  3Comments

tronginc picture tronginc  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments

VRspace4 picture VRspace4  路  3Comments