How to use custom validator for nested validate
See original GitHub issueHi, in this code the value of hasLogin
is true
or false
and always it is a boolean value, but I get Invalid value
error for it. what is wrong in my code? Can I use express-validator like this?
return [
check("hasLogin", "Invalid data").isBoolean(),
check("isInteraction", "Invalid data").isBoolean(),
check("hasLogin").custom((hasLogin) => {
if (hasLogin) {
check("link").isURL().withMessage("Invalid website url");
check("loginPage").isURL().withMessage("Invalid login page url");
check("username", "Username must be string").isAlpha();
check("password", "Password must be string").isAlpha();
check("dataPage", "Invalid data page url").isURL();
} else {
check("pageUrl", "Invalid website url").isURL();
}
})
];
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
Built-In, Nested, Custom Validators with FluentValidation
That's where we can make use of custom validators. In fact, if you right-click on any built-in validator we've used so far, and...
Read more >How to make class-validator work with custom decorator and ...
So I have this in my DTO: @Validate(SomeCustomDecorator) @ValidateIf(o => o.someProp === false) @ValidateNested() @Type(() => SomeOtherDTO) ...
Read more >Validating nested objects with class-validator in NestJS
I'm using class-validator for request validation in NestJS really often. A few days ago I needed to validate a nested object.
Read more >Validation - 3.10 - CakePHP Cookbook
Validation ¶. The validation package in CakePHP provides features to build validators that can validate arbitrary arrays of data with ease.
Read more >How to build nested forms & validate input in Angular 4? - Turing
To validate the form fields, use basic HTML validations. However, if you wish to carry out custom validations, make use of the directives....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Okay so you want the custom validation to be valid if all sub-validations are valid and false otherwise ?
If that’s the case, I don’t know if there is a proper way to do it. Basically you need to access the
validationResult
and make the return depend on it.The issue is that as far as I can tell,
validationResult
requiresreq
as a parameter. You could try using a custom function as a middleware :This would need to be tested. And based on what you want to do, you may want to count the current number of errors before your if/else condition and return true if the count went up after you added the new checks.
Hope this helps.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.