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.

Custom validators ignore return value

See original GitHub issue

I just updated from v5.3.1 to the latest v6.1.1. And I realized my custom validators are not working anymore. I used to just return false; to make the validator fail. But now I need to throw an exception.

To me this would be a “breaking change”, but I couldn’t find anything in the changelog. And another question would be if there is a particular benefit doing it that way? I find it cleaner to just return a boolean, cause that is exactly what one asks the validator: “Can you check if this check is true or false?”

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gustavohenkecommented, Nov 28, 2019

@sheghun people used to complain that a custom validator needed a return value to tag their field valid.

E.g.

check('email').custom(email => findUserByEmail(email).then(user => {
	if (user) {
		throw new Error('email exists');
	}

	return true; // 😐
})

…whereas the docs have always stated (although not clearly) that a resolved promise means a valid field. So the return value shouldn’t be important.

I have expanded the docs with clearer details on this.

1reaction
gustavohenkecommented, Aug 21, 2019

Ah yup, when the custom validator is async a resolved promise means a successful result. I guess it needs more extensive docs then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to return data to templates from custom validator in Angular
When creating a custom validator in Angular we have to return a javascript object (with true value) when an error is found (at...
Read more >
The best way to implement custom validators - Angular inDepth
The validator itself is just a function that accepts an AbstractControl and returns an Object containing the validation error or null if everything...
Read more >
How To Use Custom Form Validation in Angular - DigitalOcean
This validator will return an object - { 'phoneNumberInvalid': true } - if the value fails the condition of not being equal to...
Read more >
Building Custom Validators in Angular - Pluralsight
Every field in a form can act as a FormControl that returns complete ... Custom validators take the value from the FormControl ,...
Read more >
Validation - Laravel - The PHP Framework For Web Artisans
Validating Files; Validating Passwords; Custom Validation Rules ... even providing the ability to validate if values are unique in a given database table....
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