最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to validate that one of 2 parameters are present using class-validator? - Stack Overflow

programmeradmin1浏览0评论

Using class-validator along with NestJS I want to validate that a user provides either propertyA or a propertyB but they don't need to provide both.

Currently, I'm doing something like:

export class TestDto {
  @ValidateIf(obj => !obj.propertyB)
  @IsString()
  @IsNotEmpty()
  propertyA

  @ValidateIf(obj => !obj.propertyA)
  @IsString()
  @IsNotEmpty()
  propertyB
}

If they provide none of the parameters there will be multiple errors saying that propertyA and propertyB are required and should be strings etc.

In the case that they provide neither property, I would want only a single error saying something like: "You must provide propertyA or propertyB."

Can this be accomplished using NestJS/class-validator?

Using class-validator along with NestJS I want to validate that a user provides either propertyA or a propertyB but they don't need to provide both.

Currently, I'm doing something like:

export class TestDto {
  @ValidateIf(obj => !obj.propertyB)
  @IsString()
  @IsNotEmpty()
  propertyA

  @ValidateIf(obj => !obj.propertyA)
  @IsString()
  @IsNotEmpty()
  propertyB
}

If they provide none of the parameters there will be multiple errors saying that propertyA and propertyB are required and should be strings etc.

In the case that they provide neither property, I would want only a single error saying something like: "You must provide propertyA or propertyB."

Can this be accomplished using NestJS/class-validator?

Share Improve this question asked Feb 5, 2022 at 0:38 mranonymous101mranonymous101 1431 gold badge2 silver badges7 bronze badges 1
  • Hello. Your solution has another bottleneck too. Actually I opened a feat request in the class-validator and I provide there a proposal for our problem too. You can vote me up there too: github.com/typestack/class-validator/issues/1581 – Mohammad Jawad Barati Commented Mar 14, 2022 at 8:14
Add a comment  | 

3 Answers 3

Reset to default 8

If you don't want to create custom Property Decorators, this is the solution I came up with for one of my projects.

Simply set the fields to optional and then add an additional 'undefined' field that will do the check

export class TestDto {
    @IsString()
    @IsOptional()
    a?: string;

    @IsString()
    @IsOptional()
    b?: string;

    // if nothing has been provided
    @ValidateIf(o => !o.a && !o.b)
    @IsDefined({message: 'At least one of a or b must be provided'})
    protected readonly atLeastOne: undefined;

    // if both have been provided
    @ValidateIf(o => o.a && o.b)
    @IsDefined({message: 'Only one of a or b may be provided'})
    protected readonly atMostOne: undefined;

    // the 2 checks above combined
    @ValidateIf(o => (!o.a && !o.b) || (o.a && o.b))
    @IsDefined({message: 'Provide either a or b, and only one of them'})
    protected readonly exactlyOne: undefined;
}

ps. you wont need to use all these checks at the same time :)

Don't know whether this functionality exists in class-validator or not but you can simply achieve this scenario using Pipes

Just create a custom pipe and then define the logic over there

import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';

@Injectable()
export class CustomValidationPipe implements PipeTransform {
  transform(value: TestDto , metadata: ArgumentMetadata) {
    if(value.porpertyA && value.porpertyB) throw new BadRequestException("You must provide propertyA or propertyB")
    else return value;
  }
}

After That, you can use that Pipe over the controller

  @Post()
  create(@Body( new CustomValidationPipe()) testDto: TestDto) {
    return "create Resource"
  }

You can also use it in combination with a builtIn Validation Pipe just like this. In this case, your class validator logic will be executed first and then your custom pipe will execute

  @Post()
  create(@Body(ValidationPipe ,  new CustomValidationPipe()) testDto: TestDto) {
    return "create Resource"
  }


Hope This Helps

You can do that by adding a new custom decorator to your class validator.

First, you must add a custom validation class (read document) and then define a custom decorator for that (read document).

You can see how to do that in this article.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论