diff --git a/packages/imperative/CHANGELOG.md b/packages/imperative/CHANGELOG.md index 490e36fcb8..6bf9028c70 100644 --- a/packages/imperative/CHANGELOG.md +++ b/packages/imperative/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to the Imperative package will be documented in this file. +## Recent Changes + +- BugFix: Fixed a typo in the syntax validation code for positional arguments which caused the validation to never fail. [#2375](https://github.com/zowe/zowe-cli/issues/2375) + ## `8.10.3` - BugFix: Resolved an issue where extraneous base profiles were created in project configurations when a nested profile property was updated. [#2400](https://github.com/zowe/zowe-cli/pull/2400) diff --git a/packages/imperative/src/cmd/src/syntax/SyntaxValidator.ts b/packages/imperative/src/cmd/src/syntax/SyntaxValidator.ts index ea0b0be56b..b95dad3974 100644 --- a/packages/imperative/src/cmd/src/syntax/SyntaxValidator.ts +++ b/packages/imperative/src/cmd/src/syntax/SyntaxValidator.ts @@ -226,7 +226,7 @@ export class SyntaxValidator { if (!(commandArguments[positional.name] == null)) { if (positional.regex) { if (commandArguments[positional.name] - .toString().match(new RegExp(positional.regex) == null)) { + .toString().match(new RegExp(positional.regex)) == null) { valid = false; this.positionalParameterInvalid(positional, commandArguments[positional.name], responseObject); diff --git a/packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts b/packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts index 58bf12a29c..db03e257c3 100644 --- a/packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts +++ b/packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts @@ -46,6 +46,9 @@ describe("Imperative should provide advanced syntax validation rules", () => { function tryOptions(optionString: string, shouldSucceed: boolean, expectedText?: string[]) { const options = yargsParser.detailed(optionString, configuration).argv; + ValidationTestCommand.positionals?.forEach((p) => { + options[p.name] = options._.shift(); // fake out named positionals + }); options._ = ["test", "validation-test"].concat(options._ || []); // fake out command structure options[Constants.JSON_OPTION] = true; delete options["--"]; // delete extra yargs parse field @@ -525,6 +528,28 @@ describe("Imperative should provide advanced syntax validation rules", () => { expect(svResponse.valid).toEqual(true); }); + it("should fail if a positional argument does not match the defined regex", async () => { + const invalidPositional = "invalid_value"; + const regexForPositional = "^\\w+$"; + ValidationTestCommand.positionals = [{ + name: invalidPositional, + type: "string", + description: "Invalid positional", + regex: regexForPositional, + }]; + + return tryOptions.bind(this)( + minValidOptions + "inv@lid", + false, + [ + "Invalid format specified for positional option:", + invalidPositional, + "Option must match the following regular expression:", + regexForPositional + ] + ); + }); + describe("We should be able to validate positional arguments of type 'number'", () => { const numberCommand: ICommandDefinition = { name: "gimme-number", aliases: [],