Repository navigation
feat: add validation of query params in channel name - #191
Conversation
|
@juergenbr looks good. 2 questions:
|
|
@derberg good points, yes. I thought that using a similar expression as parseUrlVariables would not be an issue, sorry. |
|
@juergenbr you can keep regex, I just suggested different approaches as then you do not have issues with a linter. You can also just modify your function to have a similar approach as in regarding case when 2 issues show up at the same time, don't worry about it, they do not have to be thrown at the same time. We have such a case here already https://github.com/asyncapi/parser-js/blob/master/lib/customValidators.js#L180-L196 |
|
@derberg oh okay, no problem I'll fix it. |
|
Test cases now work as expected, but due to the merge the new method got a little too complex so I split part of the logic. Please let me know if this is fine or if I should find another way. |
derberg
left a comment
There was a problem hiding this comment.
it is getting a very good shape. I added some comments, long story short -> you need to add one more test case there that I do not think is covered with current implementation
| if (typeof str !== 'string') return; | ||
| const channelName = str; | ||
| const url = new URL(`http://${channelName}`); | ||
| console.log(url.search); //logs ?foo=1; |
| if (invalidChannelName.size) { | ||
| throw new ParserError({ | ||
| type: validationError, | ||
| title: 'Channel names with parameters exist', |
There was a problem hiding this comment.
| title: 'Channel names with parameters exist', | |
| title: 'Channel names contain query parameters', |
| type: validationError, | ||
| title: 'Channel names with parameters exist', | ||
| parsedJSON, | ||
| validationErrors: groupValidationErrors('channels', 'channels contain invalid name with url parameters ', invalidChannelName, asyncapiYAMLorJSON, initialFormat) |
There was a problem hiding this comment.
| validationErrors: groupValidationErrors('channels', 'channels contain invalid name with url parameters ', invalidChannelName, asyncapiYAMLorJSON, initialFormat) | |
| validationErrors: groupValidationErrors('channels', 'channels contain invalid name with url query parameters', invalidChannelName, asyncapiYAMLorJSON, initialFormat) |
| const variables = parseUrlVariables(key); | ||
| const notProvidedChannelParams = notProvidedParams.get(tilde(key)); | ||
| const parameters = parseUrlParameters(key); | ||
| if (!variables && !parameters) |
There was a problem hiding this comment.
because of the following ifs, I don't think we need this one
| chnlsMap.forEach((val, key) => { | ||
| const variables = parseUrlVariables(key); | ||
| const notProvidedChannelParams = notProvidedParams.get(tilde(key)); | ||
| const parameters = parseUrlParameters(key); |
There was a problem hiding this comment.
maybe rename parameters to queryParameters and parseUrlParameters to parseUrlQueryParameters?
this will make code slightly clearer. Problem with current naming is that this function validates, query params and also if variables have params object. So you see "parameters" in 2 different contexts with totally different meaning. This is why I think we need to make super clear that it is query param here
| const inputString = `{ | ||
| "channels": { | ||
| "test/test01": { | ||
| "parameters": { |
There was a problem hiding this comment.
you are validating here a channel name, so instead of providing a parameter here, I think you could just pass empty object
| const inputString = `{ | ||
| "channels": { | ||
| "/user/signedup?foo=1": { | ||
| "subscribe": { |
There was a problem hiding this comment.
same as with the other test case, I think you can easily just pass an empty channel object
|
|
||
| return true; | ||
|
|
||
| function setNotProvidedParams(variables, val, key, notProvidedChannelParams) { |
There was a problem hiding this comment.
I think you can move this function outside this function, as a separate private helper function, because it has the potential to be used in other validations, just don't forget about jsdoc.
Why? because I'm pretty sure you will need to use it in query params validation too, like here
if (parameters) {
invalidChannelName.set(key,
parameters);
}
this logic is here for handling use case where more that once the error of the same title appear during validation. In other words, I'm pretty sure the current implementation is not able to handle a case where you have 2 different channels containing query parameters. Please add such a test case and you will see what I mean.
|
@derberg thanks again for your detailed feedback. I refactored the code to pull the additional method into the utils file. I also found a way to combine exceptions from both validations so that none "overrules" the other. Regarding the case with 2 or more channels that contain query parameters: since the code you mentioned is inside the loop this case is covered. I added a test case and it is working as expected, returning 2 validation errors inside the array and returning them with correct messages. Same works if one channel has a missing parameter and one contains a query parameter. |
derberg
left a comment
There was a problem hiding this comment.
I added some comments regarding naming of errors and jsdoc.
Great idea with displaying errors from different validations, I like it.
What is missing:
- test case for channel that has no parameter object and name of channel is
user/{userId}/signedup?foo=1. So 2 different errors for the same channel - in tests, I cannot see in error information about the channel (the one that failed validation) locatin in the file. Things like
endColumnand others. Notice that channel variables validaton usestildefunction to make it work
| if (notProvidedParams.size || invalidChannelName.size) { | ||
| throw new ParserError({ | ||
| type: validationError, | ||
| title: 'Channel validation failed wiht following errors', |
There was a problem hiding this comment.
tiny spelling mistake here, with not wiht
| title: 'Channel validation failed wiht following errors', | |
| title: 'Channel validation failed', |
|
|
||
| //combine validation errors of both checks and output them as one array | ||
| const parameterValidationErrors = groupValidationErrors('channels', 'channel does not have a corresponding parameter object for', notProvidedParams, asyncapiYAMLorJSON, initialFormat); | ||
| const nameValidationErrors = groupValidationErrors('channels', 'channels contain invalid name with url query parameters ', invalidChannelName, asyncapiYAMLorJSON, initialFormat); |
There was a problem hiding this comment.
| const nameValidationErrors = groupValidationErrors('channels', 'channels contain invalid name with url query parameters ', invalidChannelName, asyncapiYAMLorJSON, initialFormat); | |
| const nameValidationErrors = groupValidationErrors('channels', 'channel contains invalid name with query parameters ', invalidChannelName, asyncapiYAMLorJSON, initialFormat); |
| type: validationError, | ||
| title: 'Channel validation failed wiht following errors', | ||
| parsedJSON, | ||
| validationErrors: allValidationErrors //groupValidationErrors('channels', 'channel does not have a corresponding parameter object for', notProvidedParams, asyncapiYAMLorJSON, initialFormat) |
| * @function setNotProvidedParams | ||
| * @private | ||
| * @param {String} variables array of all identified URL variables in a channel name | ||
| * @param {Object} val The channel object for which to identify the missing parameters |
There was a problem hiding this comment.
| * @param {Object} val The channel object for which to identify the missing parameters | |
| * @param {Object} val the channel object for which to identify the missing parameters |
| * | ||
| * @function setNotProvidedParams | ||
| * @private | ||
| * @param {String} variables array of all identified URL variables in a channel name |
There was a problem hiding this comment.
| * @param {String} variables array of all identified URL variables in a channel name | |
| * @param {Array<String>} variables array of all identified URL variables in a channel name |
| }; | ||
|
|
||
| /** | ||
| * return all missing properties for a channel |
There was a problem hiding this comment.
| * return all missing properties for a channel | |
| * extend map with channel params missing corresponding param object |
| * @param {Object} val The channel object for which to identify the missing parameters | ||
| * @param {String} key the channel name. | ||
| * @param {Array<Object>} notProvidedChannelParams concatinated list of missing parameters for all channels | ||
| * @param {Map} notProvidedParams result map of all missing parameters |
There was a problem hiding this comment.
| * @param {Map} notProvidedParams result map of all missing parameters | |
| * @param {Map} notProvidedParams result map of all missing parameters extended by this function |
|
Sorry for the doc errors, I implemented your suggestions, added the location information and a test case to cover a channel violating both checks at the same time. |
Co-authored-by: Lukasz Gornicki <[email protected]>
|
Kudos, SonarCloud Quality Gate passed!
|
derberg
left a comment
There was a problem hiding this comment.
LGTM. Great work @juergenbr !
|
🎉 This PR is included in version 1.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
@all-contributors please add @juergenbr for code |
|
I've put up a pull request to add @juergenbr! 🎉 |
|
This is failing with error Because the channel name is just |
|
@WaleedAshraf fix provided #196 |
Description
Related issue(s)
Fixes #145