-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
lib/utils.js
Outdated
if (typeof str !== 'string') return; | ||
const channelName = str; | ||
const url = new URL(`http://${channelName}`); | ||
console.log(url.search); //logs ?foo=1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove this console.log
lib/customValidators.js
Outdated
if (invalidChannelName.size) { | ||
throw new ParserError({ | ||
type: validationError, | ||
title: 'Channel names with parameters exist', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
title: 'Channel names with parameters exist', | |
title: 'Channel names contain query parameters', |
lib/customValidators.js
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
lib/customValidators.js
Outdated
const variables = parseUrlVariables(key); | ||
const notProvidedChannelParams = notProvidedParams.get(tilde(key)); | ||
const parameters = parseUrlParameters(key); | ||
if (!variables && !parameters) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because of the following ifs, I don't think we need this one
lib/customValidators.js
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
test/customValidators_test.js
Outdated
const inputString = `{ | ||
"channels": { | ||
"test/test01": { | ||
"parameters": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are validating here a channel name, so instead of providing a parameter here, I think you could just pass empty object
test/customValidators_test.js
Outdated
const inputString = `{ | ||
"channels": { | ||
"/user/signedup?foo=1": { | ||
"subscribe": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as with the other test case, I think you can easily just pass an empty channel object
lib/customValidators.js
Outdated
|
||
return true; | ||
|
||
function setNotProvidedParams(variables, val, key, notProvidedChannelParams) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
endColumn
and others. Notice that channel variables validaton usestilde
function to make it work
lib/customValidators.js
Outdated
if (notProvidedParams.size || invalidChannelName.size) { | ||
throw new ParserError({ | ||
type: validationError, | ||
title: 'Channel validation failed wiht following errors', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tiny spelling mistake here, with
not wiht
title: 'Channel validation failed wiht following errors', | |
title: 'Channel validation failed', |
lib/customValidators.js
Outdated
|
||
//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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
lib/customValidators.js
Outdated
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove this comment
lib/utils.js
Outdated
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @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 |
lib/utils.js
Outdated
* | ||
* @function setNotProvidedParams | ||
* @private | ||
* @param {String} variables array of all identified URL variables in a channel name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @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 |
lib/utils.js
Outdated
@@ -304,3 +317,25 @@ utils.groupValidationErrors = (root, errorMessage, errorElements, asyncapiYAMLor | |||
|
|||
return errors; | |||
}; | |||
|
|||
/** | |||
* return all missing properties for a channel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* return all missing properties for a channel | |
* extend map with channel params missing corresponding param object |
lib/utils.js
Outdated
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @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 <lpgornicki@gmail.com>
Kudos, SonarCloud Quality Gate passed! 0 Bugs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Schema: https://raw.githubusercontent.com/asyncapi/asyncapi/master/examples/2.0.0/slack-rtm.yml Because the channel name is just |
@WaleedAshraf fix provided #196 |
Description
Related issue(s)
Fixes #145