question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: add option to stop validation after first validation error

See original GitHub issue

If i apply the following annotations:

  @IsNotEmpty()
  @IsInt()
  @Min(0)
  foobar: number;

The validation result will be:

{ 
  "value": null, 
  "property": "foobar", 
  "children": [], 
  "constraints": {
    "min": "foobar must be greater than 0",
   "isInt": "foobar must be an integer number", 
   "isNotEmpty": "foobar should not be empty" 
  } 
}

If no value is given (i.e.: foobar === null), I only want the validation result of @IsNotEmpty. All the others are quite useless… worse: since the validation errors (the constraints) are stored in an object, the ordering is lost so I cannot “just take the first error constraint” 😦

Expected result:

{ 
  "value": null, 
  "property": "foobar", 
  "children": [], 
  "constraints": {
   "isNotEmpty": "foobar should not be empty" 
  } 
}

or:

 { 
  "value": null, 
  "property": "foobar", 
  "children": [], 
  "constraints": [
    {"isNotEmpty": "foobar should not be empty" },
    {"isInt": "foobar must be an integer number"},
    { "min": "foobar must be greater than 0"}
  ]
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:25
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

12reactions
ph55commented, Feb 1, 2019

I agree. Would be much efficient when no validators would run after one failed.

My example:

  @IsInt()
  @IsInModel({ model: User, property: 'id' })
  userId: number;

IsInModel will still run even though userId sent as string.

And error will look doubled and not so smart: User id must be an integer number. Invalid user id

11reactions
kostanevcommented, Apr 17, 2018

It would be nice if there is a setting that makes the validator stop after the very first fail. For example, I have custom constraint that checks the database for email existence. I used the following decorators:

@IsNotEmpty()
@Length(3, 50)
@EmailNotExists()
email: string;

This makes database query even if email is empty or with less than 3 characters. Of course I could add additional checks in my constraint, but it makes no sense to do that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel how to stop validation after first error - Stack Overflow
1. Create a custom validator class extending the Laravel validator and overwrite the passes method. <?php namespace App ...
Read more >
Stop validation after the first validation error was found
Hi , Would it be possible to stop further validations when the first error was found? Regards, Elena.
Read more >
User Input Validation in Razor Pages
However, you should only ever view client-side validation as a courtesy to the user because it is easily circumnavigated. The MVC framework, on...
Read more >
show message validate error input once at a time ant design ...
Removed exclusive option of getFieldDecorator . Added createFormField , and you must use it to wrap field data in option.mapPropsToFields of Form.create :...
Read more >
Conventional Commits
Summary. The Conventional Commits specification is a lightweight convention on top of commit messages. It provides an easy set of rules for creating...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found