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

Validate json object #1264

Open
syco opened this issue Nov 13, 2023 · 6 comments
Open

Validate json object #1264

syco opened this issue Nov 13, 2023 · 6 comments

Comments

@syco
Copy link

syco commented Nov 13, 2023

Hi,
is it possible to use an array of chains [body('xx').isInt(), body('yy').isString()] or similar, against a json object instead of the request object?
I have complex validation arrays already built and I'm in the situation where I need to validate objects coming from a different source, not from an http request.
Would be great if I could use a logic like const result = validationResult(object, [body('xx').isInt(), body('yy').isString()]);

Thanks

@gustavohenke
Copy link
Member

Hey,
In theory you can, it's how most of the tests are written, but all of express-validator assumes a request-like structure.

it('can be used on custom chains', async () => {
const req = { query: { foo: 1 } };
const { check, matchedData } = createInstance();
await check('foo').isAllowedDomain().run(req);
const data = matchedData(req);

...but this might be weird when you're not near the HTTP layer of your application (e.g. "why does this object have a body/cookie/query/header/param property")

You might like using pure validator.js instead: https://github.com/validatorjs/validator.js/
You'd need to validate each property you care about. And at of the time of writing, it'll throw if the input is not a string, which might or might not be annoying for you.

Then there are plenty other libraries for object validation out there.

@fedeci
Copy link
Member

fedeci commented Nov 19, 2023

@gustavohenke do we want to make a shift allowing validations to run on express requests and on plain json objects too? It shouldn't be too tricky to implement and would provide a nice wrapper over validator.js.

@syco
Copy link
Author

syco commented Nov 19, 2023 via email

@gustavohenke
Copy link
Member

do we want to make a shift allowing validations to run on express requests and on plain json objects too?

Yeah, I think this can be done.

I'm marking this issue as an enhancement then, but please note it might take a few weeks/months before I'm able to get around to working on it @syco

@syco
Copy link
Author

syco commented Dec 13, 2023

That's great news, thanks

@fedeci
Copy link
Member

fedeci commented Dec 17, 2023

do we want to make a shift allowing validations to run on express requests and on plain json objects too?

Yeah, I think this can be done.

I'm marking this issue as an enhancement then, but please note it might take a few weeks/months before I'm able to get around to working on it @syco

I may do that this week. It shouldn't take me more than a couple hours to get it working.

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

No branches or pull requests

3 participants