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

fix(checkSchema): check all locations if in is not specified. #1070

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nikli2009
Copy link
Contributor

@nikli2009 nikli2009 commented Jul 29, 2021

Description

When using checkSchema, I notice that the default option doesn't work as expected when the field is in req.query and in is not specified. (if in is empty, by default we should check all locations as official doc suggested here

Related issues:

To-do list

  • I have added tests for what I changed.
  • This pull request is ready to merge.

@fedeci fedeci self-requested a review July 29, 2021 10:25
@coveralls
Copy link

coveralls commented Jul 29, 2021

Coverage Status

Coverage remained the same at 100.0% when pulling 989f0bb on nikli2009:master into 690cd63 on express-validator:master.

src/context.ts Show resolved Hide resolved
@nikli2009 nikli2009 requested a review from fedeci July 29, 2021 16:37
@fedeci fedeci mentioned this pull request Jul 31, 2021
@fedeci fedeci changed the title fix: check all locations if in is not specified. fix(checkSchema): check all locations if in is not specified. Jul 31, 2021
@fedeci fedeci changed the title fix(checkSchema): check all locations if in is not specified. fix(checkSchema): check all locations if in is not specified. Jul 31, 2021
@fedeci
Copy link
Member

fedeci commented Aug 14, 2021

Hey @nikli2009 can you add a few more tests to check if #1010 is fixed by this?

@nikli2009
Copy link
Contributor Author

Hey @nikli2009 can you add a few more tests to check if #1010 is fixed by this?

hey man, gladly ! checking today

@nikli2009
Copy link
Contributor Author

Hey @nikli2009 can you add a few more tests to check if #1010 is fixed by this?

Test added, could you help review and let me know if need to add more

Copy link
Member

@fedeci fedeci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nitty comments but look fine. Thanks!
/cc @gustavohenke patch or minor? It actually corrects a bug(?) but may break something🤔

src/middlewares/validation-chain-builders.spec.ts Outdated Show resolved Hide resolved
src/middlewares/validation-chain-builders.spec.ts Outdated Show resolved Hide resolved
src/context.spec.ts Outdated Show resolved Hide resolved
src/context.ts Outdated Show resolved Hide resolved
src/middlewares/schema.spec.ts Outdated Show resolved Hide resolved
src/middlewares/schema.spec.ts Show resolved Hide resolved
@nikli2009
Copy link
Contributor Author

nikli2009 commented Aug 16, 2021

Tests updated + commits squashed 🥪🥪 .

@gustavohenke gustavohenke self-requested a review September 6, 2021 23:17
Copy link
Member

@gustavohenke gustavohenke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change.
For everybody using check() instead of the specialised functions, this can be a bit wild...

Test code:

const req = {};
check('foo').isNumeric().run(req).then(() => {
  console.log(validationResult(req).array());
});

Output:

[
  {
    value: undefined,
    msg: 'Invalid value',
    param: 'foo',
    location: 'body'
  },
  {
    value: undefined,
    msg: 'Invalid value',
    param: 'foo',
    location: 'cookies'
  },
  {
    value: undefined,
    msg: 'Invalid value',
    param: 'foo',
    location: 'headers'
  },
  {
    value: undefined,
    msg: 'Invalid value',
    param: 'foo',
    location: 'params'
  },
  {
    value: undefined,
    msg: 'Invalid value',
    param: 'foo',
    location: 'query'
  }
]

Looking at #1071, it's conceptually wrong that you don't pass in to the schema, and expect a certain request location to have the value you wanted.
You should probably use matchedData() instead.

But then, a few things I'm wondering:

  • would it makes sense to differentiate between "all/multiple locations" vs "any location", so to appease to those that want the convenience of check?
  • should we verify if the user actually tried to pass the validated field somewhere -- e.g. /some?query vs /some -- and prefer handling req.query instead of req.body?

@nikli2009
Copy link
Contributor Author

nikli2009 commented Sep 9, 2021

Thanks for taking time to review this PR and sharing all the detail of API design @gustavohenke

  • should we verify if the user actually tried to pass the validated field somewhere -- e.g. /some?query vs /some -- and prefer handling req.query instead of req.body?

Totally agree 👍 , we can check query if query isn't empty.
Also wondering for validator exists, scenarios like below ⬇️, should we check all locations if user doesn't specify in? 🤔

// when use check or checkSchema
check('anyKey').exists()
checkSchema({
   'anyKey': {
       exists: true
   }
})


// req is simply `/helloworld`
{
   body: {},
   query: {},
   params: {},
   headers: { ... stuff },
   cookies: { ...stuff }
}
// expected validation result 
// Option 1: [ notInBody ]
// Option 2: [ notInBody, notInQuery, notInParams ]
// Option 3: [ notInBody, notInQuery, notInParams, notInHeaders, notInCookies ]

@fedeci fedeci self-requested a review September 12, 2021 22:12
@gustavohenke gustavohenke force-pushed the master branch 2 times, most recently from 3177f64 to 6e160b1 Compare April 7, 2023 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants