Skip to content

Add validation for command parameters #142

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

Merged
merged 1 commit into from
Nov 7, 2014

Conversation

rosen-vladimirov
Copy link
Contributor

Add validation for the parameters that each command accepts. Add logic to check if command has mandatory parameters and all of them are provided - if any of them is missing, command is not executed. Add logic to validate all arguments passed to command - if the command does not accept arguments, but there are some passed to it, it is not executed. Add ICommandParameter interface to describe command parameter. Add allowedParameters property to ICommand interface. Add canExecute method to ICommand interface. It is not mandatory to implement it and it is used when the command will validate the parameters on its own. The common validation is implemented in CommandsService.

ICommandParameter describes one parameter that the command accepts. It can be mandatory for the command or not. That's why I've added mandatory boolean parameter to the interface. Each parameter have its own logic for valid values, that's why ICommandParameter has validate method. It is called from the CommandsService when the arguments are validated. CommandsService itself tries to validate all command arguments. One special case is when the command is hierarchical. In this situation the root command is validated first, but it doesn't have own definition, so it doesn't have allowed parameters. That's why CommandsService checks if the command is valid hierarchical command. If command has canExecute method implemented, CommandsService will use it to validate the arguments. If not, it will try to validate all passed command line arguments with the allowed parameters of the command, by checking their count, calling validate method, etc.

Each command has different number of allowed parameters. In most of the cases the command parameter is string and the only requirement for it is to be non-empty. That's why new StringCommandParameter class is added. It is used to define such parameters. In case the parameter has more complicated logic, new class, that implements ICommandParameter is added for it.

Remove some tests as the validation logic is moved outside of some methods - in the command parameters. Add new unit tests.

https://huboard.com/NativeScript/nativescript-cli#/issues/48065867

@justcodebuilduser
Copy link

Can one of the admins verify this patch?

2 similar comments
@ns-bot
Copy link

ns-bot commented Nov 5, 2014

Can one of the admins verify this patch?

@ns-bot
Copy link

ns-bot commented Nov 5, 2014

Can one of the admins verify this patch?

@@ -1,12 +1,29 @@
///<reference path="../.d.ts"/>

export class AddPlatformCommand implements ICommand {
Copy link
Contributor

Choose a reason for hiding this comment

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

because the file is changed in this PR, we can add the missing "use strict"

@teobugslayer
Copy link
Contributor

👍 after addressing my minor stylistic issues

return true;
}

return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

return this.$projectNameValidator.validate(value) is shorter

@ErjanGavalji
Copy link
Contributor

ok to test

@ns-bot
Copy link

ns-bot commented Nov 5, 2014

Test PASSed.

@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/commands-and-parameters branch from 0ced6c2 to 1af9927 Compare November 5, 2014 16:26
@ns-bot
Copy link

ns-bot commented Nov 5, 2014

Test PASSed.

Add validation for the parameters that each command accepts. Add logic to check if command has mandatory parameters and all of them are provided - if any of them is missing, command is not executed. Add logic to validate all arguments passed to command - if the command does not accept arguments, but there are some passed to it, it is not executed. Add ICommandParameter interface to describe command parameter. Add allowedParameters property to ICommand interface. Add canExecute method to ICommand interface. It is not mandatory to implement it and it is used when the command will validate the parameters on its own. The common validation is implemented in CommandsService.

ICommandParameter describes one parameter that the command accepts. It can be mandatory for the command or not. That's why I've added mandatory boolean parameter to the interface. Each parameter have its own logic for valid values, that's why ICommandParameter has validate method. It is called from the CommandsService when the arguments are validated. CommandsService itself tries to validate all command arguments. One special case is when the command is hierarchical. In this situation the root command is validated first, but it doesn't have own definition, so it doesn't have allowed parameters. That's why CommandsService checks if the command is valid hierarchical command. If command has canExecute method implemented, CommandsService will use it to validate the arguments. If not, it will try to validate all passed command line arguments with the allowed parameters of the command, by checking their count, calling validate method, etc.

Each command has different number of allowed parameters. In most of the cases the command parameter is string and the only requirement for it is to be non-empty. That's why new StringCommandParameter class is added. It is used to define such parameters. In case the parameter has more complicated logic, new class, that implements ICommandParameter is added for it.

Remove some tests as the validation logic is moved outside of some methods - in the command parameters. Add new unit tests.

https://huboard.com/NativeScript/nativescript-cli#/issues/48065867
@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/commands-and-parameters branch from 1af9927 to 4fdafde Compare November 7, 2014 10:07
@ns-bot
Copy link

ns-bot commented Nov 7, 2014

Test PASSed.

rosen-vladimirov added a commit that referenced this pull request Nov 7, 2014
…ameters

Add validation for command parameters
@rosen-vladimirov rosen-vladimirov merged commit 74f1be7 into master Nov 7, 2014
@rosen-vladimirov rosen-vladimirov deleted the vladimirov/commands-and-parameters branch November 7, 2014 10:10
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.

6 participants