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

Pattern validation not applied to individual values in case of comma separated query params #54

Open
KPranith opened this issue Oct 10, 2020 · 4 comments

Comments

@KPranith
Copy link

Steps to recreate the issue

  1. Change query.json /pets petId parameter like below
    {
    "name": "petId",
    "in": "query",
    "required": true,
    "description": "The id of the pet to retrieve",
    "schema": {
    "type": "array",
    "items": {
    "type": "string",
    "pattern": "^[A-Z0-9]+-[0-9]{4}"
    }
    }
    }
  2. change the test case should coerce query parameter to an array in query.spec.ts as below

it('should coerce query parameter to an array', () => {
const queryMeta = {
method: 'get',
query: {
petId:'ACD-2016,ACD-213'
}
};
expect(chowchow.validateRequest('/pets', queryMeta)).toEqual(expect.objectContaining({
query: {
petId: ['ACD-2016,ACD-213']
}
}));
expect(queryMeta.query.petId).toEqual('ACD-2016,ACD-213');
});

As the string ACD-213 is not matching the pattern it should fail but it doesn't
image

@KPranith
Copy link
Author

@TageNielsen FYI..

@supertong
Copy link
Collaborator

Hey @KPranith , the example you provided is taking the whole 'ACD-2016,ACD-213' as a single array item which matches the regex pattern you provided - "pattern": "^[A-Z0-9]+-[0-9]{4}".
At the moment, this library is expecting the query string to be parsed before pass in for validation. If you pass in 'ACD-2016,ACD-213', it will be taken as a string instead of doing further parsing to an array.

@KPranith
Copy link
Author

KPranith commented Oct 23, 2020

Thank You @supertong ,

You meant the query string has to be parsed
from 'ACD-2016,ACD-213'
to ['ACD-2016','ACD-213']
and then sent to the library

Are there any other such parsing (pre processing) that has to be done before its passed in for validation ?

Would submitting a PR to parse comma separated values to an array would help as a feature for other users ?

@supertong
Copy link
Collaborator

Hey @KPranith , we have library like https://github.com/atlassian/koa-oas3 that using this library under the hood and it's also using the https://github.com/sindresorhus/query-string for parsing that.
Depends on the style and explode you specified in the Parameter Object, there could be more combination than the comma separated one. https://swagger.io/specification . I have a thinking of parsing the query depends on the style and explode specified in the document, but haven't got time to implement it yet.

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

No branches or pull requests

2 participants