Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Array with allowable values #429

Merged
3 commits merged into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the Imperative package will be documented in this file.

## Recent Changes

- Add allowableValues support for array

## `4.7.4`

- Fix update profile API storing secure fields incorrectly when called without CLI args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports[`cmd-cli should display proper messages when missing positional paramete
"
Syntax Error:
You must specify one of the following options for this command:
[--option-to-specify-1, --option-to-specify-2, --option-to-specify-3]
[--option-to-specify-1, --option-to-specify-2, --option-to-specify-3, --option-to-specify-4]

Syntax Error:
If you do not specify the following option:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export const syntaxTestCommand: ICommandDefinition = {
type: "string",
allowableValues: {values: ["allowableA", "allowableB", "^allowableC\\$"], caseSensitive: false}
},
{
name: "option-to-specify-4",
description: "Part of must specify one group",
type: "array",
allowableValues: {values: ["allowableA", "allowableB"], caseSensitive: false}
},
{
name: "conflicts-with-1",
description: "Conflicts with option-to-specify-2",
Expand Down Expand Up @@ -161,5 +167,5 @@ export const syntaxTestCommand: ICommandDefinition = {
options: `"file.txt" "ibmuser.ps" --mr wait`
},
],
mustSpecifyOne: ["option-to-specify-1", "option-to-specify-2", "option-to-specify-3"]
mustSpecifyOne: ["option-to-specify-1", "option-to-specify-2", "option-to-specify-3", "option-to-specify-4"]
};
14 changes: 14 additions & 0 deletions __tests__/src/packages/cmd/SyntaxValidator.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ describe("Imperative should provide advanced syntax validation rules", function
alwaysRequired, true, [])()
]);
});
it("If we specify an option whose type is array and which has a set of allowable string values," +
" and specify multiple values each of which matches one of the allowable values," +
" the command should succeed ",
function() {
return tryOptions.bind(this, "--option-to-specify-4 allowableA --option-to-specify-4 allowableB " +
"--absence-implies " + alwaysRequired, true, [])();
});
it("If we specify an option whose type is array and which has a set of allowable string values," +
" and specify multiple values one of which doesn't match one of the allowable values," +
" the command should fail ",
function() {
return tryOptions.bind(this, "--option-to-specify-4 allowableA --option-to-specify-4 notAllowable " +
"--absence-implies " + alwaysRequired, false, ["must match"])();
});
it("If we don't specify an option, and the absence of that option implies " +
"the presence of another option, and we omit that option as well, the command should fail ",
function () {
Expand Down
8 changes: 7 additions & 1 deletion __tests__/src/packages/cmd/ValidationTestCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export const ValidationTestCommand: ICommandDefinition = {
type: "string",
allowableValues: {values: ["allowableA", "allowableB", "^allowableC\\$"], caseSensitive: false}
},
{
name: "option-to-specify-4",
description: "Part of must specify one group",
type: "array",
allowableValues: {values: ["allowableA", "allowableB"], caseSensitive: false}
},
{
name: "conflicts-with-1",
description: "Conflicts with option-to-specify-2",
Expand Down Expand Up @@ -132,5 +138,5 @@ export const ValidationTestCommand: ICommandDefinition = {
required: true
},
],
mustSpecifyOne: ["option-to-specify-1", "option-to-specify-2", "option-to-specify-3"]
mustSpecifyOne: ["option-to-specify-1", "option-to-specify-2", "option-to-specify-3", "option-to-specify-4"]
};
12 changes: 8 additions & 4 deletions packages/cmd/src/syntax/SyntaxValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,14 @@ export class SyntaxValidator {

return regex;
});
if (!this.checkIfAllowable(optionDefCopy.allowableValues, commandArguments[optionName])) {
this.invalidOptionError(optionDefCopy, responseObject, commandArguments[optionName]);
valid = false;
}

const optionValue = commandArguments[optionName];
const optionValueArray = Array.isArray(optionValue) ? optionValue : [optionValue];
optionValueArray.filter(value => !this.checkIfAllowable(optionDefCopy.allowableValues, value))
.forEach(value => {
this.invalidOptionError(optionDefCopy, responseObject, value);
valid = false;
});
}

if (!isNullOrUndefined(optionDef.conflictsWith) && optionDef.conflictsWith.length > 0) {
Expand Down
14 changes: 14 additions & 0 deletions packages/cmd/src/syntax/__tests__/SyntaxValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ describe("Imperative should provide advanced syntax validation rules", () => {
alwaysRequired, true, [])()
]);
});
it("If we specify an option whose type is array and which has a set of allowable string values," +
" and specify multiple values each of which matches one of the allowable values," +
" the command should succeed ",
function() {
return tryOptions.bind(this, "--option-to-specify-4 allowableA --option-to-specify-4 allowableB " +
"--absence-implies " + alwaysRequired, true, [])();
});
it("If we specify an option whose type is array and which has a set of allowable string values," +
" and specify multiple values one of which doesn't match one of the allowable values," +
" the command should fail ",
function() {
return tryOptions.bind(this, "--option-to-specify-4 allowableA --option-to-specify-4 notAllowable " +
"--absence-implies " + alwaysRequired, false, ["must match"])();
});
it("If we don't specify an option, and the absence of that option implies " +
"the presence of another option, and we omit that option as well, the command should fail ",
function () {
Expand Down