How to change the format of NestJS controller/class-validator error messages showing which field each message belongs to?

huangapple go评论87阅读模式
英文:

How to change the format of NestJS controller/class-validator error messages showing which field each message belongs to?

问题

The NestJS controller, along with the class-validator, currently returns an error message like this:

  1. {
  2. statusCode: 422,
  3. message: [ 'Name is required', 'Email is required' ],
  4. error: 'Unprocessable Entity'
  5. }

But I would like to bind each message to its related property like this:

  1. {
  2. statusCode: 422,
  3. message: { name: 'Name is required', email: 'Email is required' },
  4. error: 'Unprocessable Entity'
  5. }
  6. }
  7. My DTO:
  8. ```typescript
  9. import { IsNotEmpty } from 'class-validator';
  10. export class CreateUserRequestDTO {
  11. @IsNotEmpty({ message: 'Name is required' })
  12. name: string;
  13. @IsNotEmpty({ message: 'Email is required' })
  14. email: string;
  15. }

How could I change the NestJS controller/class-validator errors to return messages like this?

英文:

The NestJS controller, along with the class-validator, currently returns an error message like this:

  1. {
  2. statusCode: 422,
  3. message: [ 'Name is required', 'Email is required' ],
  4. error: 'Unprocessable Entity'
  5. }

But I would like to bind each message to its related property like this:

  1. {
  2. statusCode: 422,
  3. message: { name: 'Name is required', email: 'Email is required' },
  4. error: 'Unprocessable Entity'
  5. }

My DTO:

  1. import { IsNotEmpty } from 'class-validator';
  2. export class CreateUserRequestDTO {
  3. @IsNotEmpty({ message: 'Name is required' })
  4. name: string;
  5. @IsNotEmpty({ message: 'Email is required' })
  6. email: string;
  7. }

How could I change the NestJS controller/class-validator errors to return messages like this?

答案1

得分: 5

The response for validation errors can be modified by passing exceptionFactory to the ValidationPipe options.

查看示例实现:

  1. app.useGlobalPipes(
  2. new ValidationPipe({
  3. exceptionFactory: (errors) => {
  4. return new UnprocessableEntityException({
  5. statusCode: 422,
  6. error: 'Unprocessable Entity',
  7. message: errors.reduce(
  8. (acc, e) => ({
  9. ...acc,
  10. [e.property]: Object.values(e.constraints),
  11. }),
  12. {},
  13. ),
  14. });
  15. },
  16. }),
  17. );
英文:

The response for validation errors can be modified by passing exceptionFactory to the ValidationPipe options.

https://docs.nestjs.com/techniques/validation

See example implementation:

  1. app.useGlobalPipes(
  2. new ValidationPipe({
  3. exceptionFactory: (errors) => {
  4. return new UnprocessableEntityException({
  5. statusCode: 422,
  6. error: 'Unprocessable Entity',
  7. message: errors.reduce(
  8. (acc, e) => ({
  9. ...acc,
  10. [e.property]: Object.values(e.constraints),
  11. }),
  12. {},
  13. ),
  14. });
  15. },
  16. }),
  17. );

huangapple
  • 本文由 发表于 2023年4月7日 00:11:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定