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

Validation module #109

Merged
merged 5 commits into from
Dec 27, 2018
Merged

Validation module #109

merged 5 commits into from
Dec 27, 2018

Conversation

mukkachaitanya
Copy link
Collaborator

@mukkachaitanya mukkachaitanya commented Dec 17, 2018

Requirements

  • Need for proper error handling for invalid commands
  • Need for offloading the validation from the inputs.

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

  • The invalid commands exit now with proper error messages. Brings all the validation to a single module.
  • This module brings in ease of intergating new views.
  • Message passing decouples various modules allowing for better modularity.
  • The change also decreases the tests of cli/input which is a according to the SRP.

Applicable Issues

Addresses #100

@codecov
Copy link

codecov bot commented Dec 17, 2018

Codecov Report

Merging #109 into dev will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Flag Coverage Δ
#integration 94.91% <93.61%> (-0.11%) ⬇️
#unit 99.27% <100%> (+0.02%) ⬆️
Impacted Files Coverage Δ
lib/cli/input/prefs.js 100% <100%> (ø) ⬆️
lib/cli/input/eval.js 100% <100%> (ø) ⬆️
lib/controller/eval.js 100% <100%> (ø) ⬆️
lib/cli/output/eval.js 100% <100%> (ø) ⬆️
lib/controller/validate/eval.js 100% <100%> (ø)
lib/cli/input/prefsprompt.js 100% <100%> (ø) ⬆️
lib/controller/prefs.js 100% <100%> (ø) ⬆️
lib/model/eval.js 91.37% <100%> (+0.15%) ⬆️
lib/controller/validate/prefs.js 100% <100%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3969cab...a0ccfc5. Read the comment docs.

Copy link
Member

@prasadtalasila prasadtalasila left a 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.

@@ -0,0 +1,27 @@
const prefsOutput = require('../cli/output/prefs');
Copy link
Member

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');
Copy link
Member

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.

Copy link
Member

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

Copy link
Member

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.

Copy link
Member

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.

@@ -0,0 +1,27 @@
const prefsOutput = require('../cli/output/prefs');

const supportedServers = ['ms', 'gitlab'];
Copy link
Member

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure sir

const supportedServers = ['ms', 'gitlab'];
const supportedCommand = ['show', 'changelang', 'changeserver', 'logger'];

const prefs = (args, options) => {
Copy link
Member

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.

Copy link
Collaborator Author

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.

@mukkachaitanya
Copy link
Collaborator Author

Some considerations:

  1. The length validations in cli/input/init.js are left inside input to provide proper user feedback while input.
  2. eval module didn't implement message passing between input and model. That has been implemented. (and also here)

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.
I haven't put the test cases yet, wanted approval of the structure and idea.

@prasadtalasila
Copy link
Member

@mukkachaitanya The code looks good. Please proceed with the same structure for tests. The validation of username to rootshould be moved to controller/validator/

@mukkachaitanya
Copy link
Collaborator Author

mukkachaitanya commented Dec 26, 2018

Sir, the tests have been updated. I have updated the PR description also to include benefits and changes.

Copy link
Member

@prasadtalasila prasadtalasila left a 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';
Copy link
Member

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?

Copy link
Collaborator Author

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.

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);
Copy link
Member

Choose a reason for hiding this comment

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

spell check: different

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

Successfully merging this pull request may close these issues.

2 participants