Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting "undefined" error when running validations imperatively with "oneOf" #995

Closed
armandAkop opened this issue Feb 26, 2021 · 4 comments · Fixed by #999
Closed

Getting "undefined" error when running validations imperatively with "oneOf" #995

armandAkop opened this issue Feb 26, 2021 · 4 comments · Fixed by #999

Comments

@armandAkop
Copy link

armandAkop commented Feb 26, 2021

Describe the bug

I am running a simple validation using oneOf and running the validations imperatively. My validation middleware calls validation.run(validation), the result is undefined.

To Reproduce

The validation middleware taken from the docs

const requestValidator = (validations = []) => {
  return async (req, res, next) => {
    for (const validation of validations) {
      const result = await validation.run(req);

      if (result.errors.length) break; // TypeError: Cannot read property 'errors' of undefined
    }

    const errors = validationResult(req);
    if (errors.isEmpty()) {
      return next();
    }
    
    throw new ValidationError(errors.errors[0].msg);
  }
};

Sample route that is failing

const validationsToRun = [
  oneOf([
    body('ipAddress').isIP()
  ])
];

router.post('/test', requestValidator(validationsToRun), (req, res, next) => {
  res.send('sall good');
});

Payload

{
    "ipAddress": 4.4
}

Expected behavior

I expect the imperative validation would work as is, given that function comes from the documentation. And it seems like this issue was similar, but was resolved with a PR.

Current behavior

Express-validator version:

  • Version: 6.10.0

Am I doing something wrong here?

@fedeci
Copy link
Member

fedeci commented Feb 26, 2021

Does it work correctly if you don't run it imperatively?


edit:
We are returning void here, so result is not expected to hold a value. I will fix this later!

const run = async (req: Request) => {
return new Promise<void>((resolve, reject) => {
middleware(req, {}, (e?: any) => {
e ? reject(e) : resolve();
});
});
};

@armandAkop
Copy link
Author

armandAkop commented Feb 26, 2021

Awesome, thanks!

Edit: Parallel processing worked fine, so I will switch to that for the time being.

@fedeci
Copy link
Member

fedeci commented Feb 26, 2021

We actually have to figure out what result we want to expose. Furthermore some of those properties are marked as private (which is just a tsc flag) and I think they should be marked as private, in v7.0.0 because this is breaking, also for the end user using #propname.
/cc @gustavohenke

@gustavohenke
Copy link
Member

Hey there! This is fixed on v6.11.0 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants