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.

How to use custom validator for nested validate

See original GitHub issue

Hi, 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:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
Rouzzcommented, Dec 4, 2018

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 requires req as a parameter. You could try using a custom function as a middleware :

return [
     check("hasLogin", "Invalid data").isBoolean(),
     check("isInteraction", "Invalid data").isBoolean(),
     function(req, res, next) {
        // Guessing you get hasLogin from body, please align with your way to get hasLogin in the app
        if (req.body.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();
        }
        const errors = validationResult(req);
        return errors.isEmpty();
      })
    ];

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.

0reactions
lock[bot]commented, May 31, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

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