-
Notifications
You must be signed in to change notification settings - Fork 10
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
Validation module #109
Validation module #109
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #109 +/- ##
==========================================
+ Coverage 99.24% 99.27% +0.02%
==========================================
Files 22 24 +2
Lines 662 688 +26
==========================================
+ Hits 657 683 +26
Misses 5 5
Continue to review full report at Codecov.
|
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.
@mukkachaitanya please see the suggested changes.
lib/controller/validation.js
Outdated
@@ -0,0 +1,27 @@ | |||
const prefsOutput = require('../cli/output/prefs'); |
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.
We agreed on doing the validation at the controller. So, the location of this module is appropriate. However, we are going to need validation for all the commands, not just preferences. Hence, we may need to think about a better structure (perhaps validation/prefs.js
to include validators for all the controllers).
Another possibility is to put small validators in the respective controllers themselves and only push reasonably big / complex validators to a separate module.
@@ -2,14 +2,20 @@ const prefsInput = require('../cli/input/prefs'); | |||
const prefsOutput = require('../cli/output/prefs'); | |||
const prefsModel = require('../model/prefs'); |
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.
Is prefs
the only command to suffer from the validation problem? What about other commands. Please check again.
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.
Top of my head, I can forsee the following invalid inputs.
- Invalid hostname and or port
- Invalid login credentials - deduced without even contacting the gitlab. For example, usernames such as
123
are obviously not valid. We can look at the validity of usernames for GitLab and repeat the same logic here.
We need validations for the above scenarios too.
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.
Sir since we have a dual nature of input, these validations can only be done after the input it taken from the stdin when necessary. This happens in the input/prefs
module.
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.
Is
prefs
the only command to suffer from the validation problem? What about other commands. Please check again.
For now only prefs has the nested command model which is not taken care by caporal
. No other commands have this issue right now. I made the validation module to be extensible, so can be extended if need arises in any of the other modules.
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.
We discussed about the validation functions feature provided by caporal
. Is it better to leave the validation functions inside the controller modules or to have controllers/validators/<controller-name.js>?
Putting the non-trivial validators in controllers/validators/<controller-name.js>
may improve the code design.
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.
BTW, the nested commands are all lined up in the TODO feature list. So, the validators.js
is not just one single file; we might need a lot of them.
lib/controller/validation.js
Outdated
@@ -0,0 +1,27 @@ | |||
const prefsOutput = require('../cli/output/prefs'); | |||
|
|||
const supportedServers = ['ms', 'gitlab']; |
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.
These constants are better derived from config.
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.
Sure sir
lib/controller/validation.js
Outdated
const supportedServers = ['ms', 'gitlab']; | ||
const supportedCommand = ['show', 'changelang', 'changeserver', 'logger']; | ||
|
||
const prefs = (args, options) => { |
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.
We can ponder a bit on appropriate guidelines for the use of arrow functions in this project. If you can read a bit, and create a wiki page while also quoting good quality references, we can stick to the created guidelines everywhere in the project.
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.
Ok sir. I'll read up about it.
Some considerations:
Also Sir, how should we take care of the root user and the id parameter, should it be taken care at validation? Currently, it's been taken care like this. |
@mukkachaitanya The code looks good. Please proceed with the same structure for tests. The validation of username to |
Sir, the tests have been updated. I have updated the PR description also to include benefits and changes. |
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.
@mukkachaitanya Apart from two minor changes, the PR looks good. Thanks.
@@ -287,7 +287,7 @@ async function testChangeMSPrompt() { | |||
process.argv = ['/usr/local/nodejs/bin/node', | |||
'/usr/local/nodejs/bin/autolabjs', 'prefs', 'changeserver', '--type', 'ms']; | |||
const mockInquirer = sandbox.mock(inquirer); | |||
const testPort = 5687; | |||
const testPort = '5687'; |
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.
Is there any specific reason for this change?
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.
This description here explains the change https://github.com/chriso/validator.js#strings-only
This library validates and sanitizes strings only.
If you're not sure if your input is a string, coerce it using input + ''. Passing anything other than a string is an error.
Honestly, this is because of the #108. I think I have an updated version of validator.js
in my node_modules
.
test/unit/lib/cli/input/prefs.js
Outdated
it('should send the appropriate message when invalid host is given for gitlab server', testGitlabInvalidHost); | ||
it('should send the appropriate message when invalid port is given', testInvalidPort); | ||
it('should send the appropriate message when invalid server is given', testInvalidServer); | ||
it('should send the right event when differnt server is changed', testDefaultServer); |
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.
spell check: different
Requirements
Description of the Change
Adds a validation module which offloads the command validation and provides proper error messages. The changes also makes the applications closer to the MVC architecure. The
cli/input
module are simple view with no buisness logic.The changes also identify and decrease coupling between modules by the use of mesage passing.
Benefits
cli/input
which is a according to the SRP.Applicable Issues
Addresses #100