#Pipe Schema, Is this overkill?

1 messages · Page 1 of 1 (latest)

zealous terrace
#

I'm reading over the pipe page and came across this section.

According to this, we need to create our DTOs to make sure our data coming from the body/params is formatted correctly. The validation happens when the method is executed. At the same time, we also need to create a validation pipe schema (tool like Joi) to validate the data before the method executes.

Yet in the example, they are using both. Why? Seems like we are building in two levels of validation to validate the same thing. The DTO seems excessive at times already. Why add more excess?

Ref URL: https://docs.nestjs.com/pipes#binding-validation-pipes

boreal lichen
#

having DTO doesn't means you have validations
it all depends on if that DTO has annotations from class-validator and you're using the builtin ValidationPipe

#

as the docs didn't show how that CreateCatDto looks like, we can't tell much

#

but at the Class validator section, it was defined with class-validator in mind. In that case you shouldn't have that @UsePipes(new JoiValidationPipe(createCatSchema)) otherwise you'll end up validating twice, like you said

zealous terrace
#

LoL! So, I'm not going crazy. #cool

zealous terrace
boreal lichen
#

so there's no need to use Joi in your case

#

that sample is to show that you're not tied with class-validator

boreal lichen
#

you can assume that the CreateCatDto before the Class validator section looks like this:

export class CreateCatDto {
  name: string;
  age: number;
  breed: string;
}

and the createCatSchema could be like this:

const createCatSchema = Joi.object({
  name: Joi.string().required(),
  age: Joi.number().required(),
  breed: Joi.string().required(),
})

Feel free to add that to the docs 🙂

zealous terrace
boreal lichen
#

please don't use the @Moderator unless you need some support on the discord server

#

there's a pen in that page, click on it

#

you just need a github account to update the docs site

zealous terrace
#

Does this mean I'm done? Or do I need to complete a pull request, then push it back?