Skip to content

question: How to make custom validator work with stopAtFirstError? #1811

Description

@gtamas

I was trying to...
I'm trying to validate an object type using NestJS / class-validator (v0.13.2) like this.

  @ValidateIf(o => o.someProp === false)
  @IsOptional()
  @Validate(SomeCustomValidator)
  @ValidateNested()
  @Type(() => SomeDto)
  prop?: SomeDto 

My validator config:

{
      validateCustomDecorators: true,
      stopAtFirstError: true,
      always: false
 }

The problem:
The custom validator gets executed every time, even if input is invalid, so @ValidateNested should catch it and prevent further validation for this property. As far as I know validators are executed in a bottom-to-top order.
If remove the custom validator, I get a proper validation error, but if it's there, I get 500 because it tries to run using the invalid input.

How can I fix this? I want the validation to stop if input data is invalid. The custom validator should only run with valid input.

Btw: is there a way to halt validation globally (not only for the given prop) as soon as some validator fails?

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions