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.

oneOf not validating correctly?

See original GitHub issue

The oneOf middleware appears to be validating incorrectly.

The below validates successfully

// body
{
    "myField": true
}

// validation middleware
oneOf([check('myField').isString()])

This test case looks wrong, shouldn’t it be something other than isBoolean()?

https://github.com/express-validator/express-validator/blob/7ba714b26d802547369615c90e74389c1b9c83a0/src/middlewares/one-of.spec.ts#L176-L186

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fedecicommented, Feb 26, 2021

This works as intended:

const { oneOf, body, validationResult } = require('express-validator')
const express = require('express')

const app = express()
app.use(express.json())

app.post('/', oneOf([body('boolean').isString()]), (req, res, next) => {
  console.warn(validationResult(req))
})

app.listen(3000)
0reactions
7opfcommented, Feb 26, 2021

Right, I wasn’t using validationResult in my middleware 🤦

export function validateOneOf(schemas: Schema[]): express.RequestHandler {
    const validationChainsList = schemas.map(s => checkSchema(s));
    const middleware = oneOf(validationChainsList);
    return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
        await middleware.run(req);
        const errors = validationResult(req);

        if (errors.isEmpty()) {
            return next();
        }

        next(new HttpError({
            status: StatusCodes.BAD_REQUEST,
            message: 'Invalid request',
            extra: errors.mapped()
        }));
    };
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed validating 'oneOf' in schema · Issue #113 - GitHub
Hi, after updating to the latest release I had a strange error with schemas. Swagger example /couriers/{courierIdentifier}: get: ...
Read more >
Issue with json schema oneOf validation ... Not able to get the ...
I need help in validating json object with following constraint. My object has four sub objects. ... Only one out of obj2, obj3,...
Read more >
oneOf Validation Issue - Google Groups
I am working on creating a complex JSON schema and am having issues with validating a "oneOf" construction.
Read more >
JSON Schema validation not handling nested "oneOf" properly
json that appear valid to my schema. I have attached a simplified example: oneOfTest. json is the schema, and oneOf-test. json is the...
Read more >
oneOf, anyOf, allOf, not - Swagger
You can use it to validate the request body contains all the necessary information about the object to be updated, depending on the...
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