diff --git a/src/core/Ingredient.mjs b/src/core/Ingredient.mjs index 4f81f83b..d01c6d34 100644 --- a/src/core/Ingredient.mjs +++ b/src/core/Ingredient.mjs @@ -79,6 +79,9 @@ class Ingredient { if (this.type === "toggleString" && val && typeof val === "object" && "string" in val) { checkVal = val.string; } + if (this.type === "option" && Array.isArray(checkVal)) { + checkVal = checkVal[this.defaultIndex ?? 0]; + } // 1. check if empty let isEmpty = false; @@ -124,6 +127,21 @@ class Ingredient { } } + // 4. option checks + if (this.type === "option") { + if (Array.isArray(this.defaultValue)) { + const permittedOptions = this.defaultValue.filter(opt => { + if (typeof opt !== "string") return false; + return !opt.match(/^\[\/?[a-z0-9 -()^]+\]$/i); + }); + const valStr = (checkVal !== null && checkVal !== undefined) ? String(checkVal).toLowerCase() : ""; + const matchedOption = permittedOptions.find(opt => opt.toLowerCase() === valStr); + if (!matchedOption) { + throw new OperationError(`${this.name} must be one of the following: ${permittedOptions.join(", ")}`); + } + } + } + return true; } diff --git a/src/core/operations/AutomatedValidationTestOp.mjs b/src/core/operations/AutomatedValidationTestOp.mjs index 315eb417..92f803ae 100644 --- a/src/core/operations/AutomatedValidationTestOp.mjs +++ b/src/core/operations/AutomatedValidationTestOp.mjs @@ -60,6 +60,12 @@ class AutomatedValidationTestOp extends Operation { }, "toggleValues": ["Option A", "Option B"], "allowEmpty": false + }, + { + "name": "Option Ingredient", + "type": "option", + "value": ["[Group 1]", "Option 1", "Option 2", "[/Group 1]", "[Group 2]", "Option 3", "[/Group 2]"], + "allowEmpty": false } ]; } diff --git a/src/core/operations/SM4Encrypt.mjs b/src/core/operations/SM4Encrypt.mjs index 0a58dfb9..69c414eb 100644 --- a/src/core/operations/SM4Encrypt.mjs +++ b/src/core/operations/SM4Encrypt.mjs @@ -43,7 +43,7 @@ class SM4Encrypt extends Operation { { "name": "Mode", "type": "option", - "value": ["CBC", "CFB", "OFB", "CTR", "ECB"] + "value": ["CBC", "CFB", "OFB", "CTR", "ECB", "CBC/NoPadding", "ECB/NoPadding"] }, { "name": "Input", diff --git a/src/node/api.mjs b/src/node/api.mjs index f41feb23..83163d37 100644 --- a/src/node/api.mjs +++ b/src/node/api.mjs @@ -74,7 +74,7 @@ function transformArgs(opArgsList, newArgs) { return opArgs.map((arg) => { if (arg.type === "option") { // pick default option if not already chosen - return typeof arg.value === "string" ? arg.value : arg.value[arg.defaultIndex ?? 0]; + return !Array.isArray(arg.value) ? arg.value : arg.value[arg.defaultIndex ?? 0]; } if (arg.type === "editableOption") { diff --git a/tests/operations/tests/AutomatedValidation.mjs b/tests/operations/tests/AutomatedValidation.mjs index da84de11..e9d16029 100644 --- a/tests/operations/tests/AutomatedValidation.mjs +++ b/tests/operations/tests/AutomatedValidation.mjs @@ -15,7 +15,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }] + args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -26,7 +26,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [4, 1.5, "hello", "", { "option": "Option A", "string": "test" }] + args: [4, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -37,7 +37,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [11, 1.5, "hello", "", { "option": "Option A", "string": "test" }] + args: [11, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -48,7 +48,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [5.5, 1.5, "hello", "", { "option": "Option A", "string": "test" }] + args: [5.5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -59,7 +59,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [5, 1.4, "hello", "", { "option": "Option A", "string": "test" }] + args: [5, 1.4, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -70,7 +70,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [5, 5.6, "hello", "", { "option": "Option A", "string": "test" }] + args: [5, 5.6, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -81,7 +81,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [5, 1.5, "helloooo", "", { "option": "Option A", "string": "test" }] + args: [5, 1.5, "helloooo", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -92,7 +92,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [5, 1.5, "", "", { "option": "Option A", "string": "test" }] + args: [5, 1.5, "", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -103,7 +103,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }] + args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"] } ] }, @@ -114,7 +114,40 @@ TestRegister.addTests([ recipeConfig: [ { op: "Automated Validation Test Op", - args: [5, 1.5, "hello", "", { "option": "Option A", "string": "" }] + args: [5, 1.5, "hello", "", { "option": "Option A", "string": "" }, "Option 1"] + } + ] + }, + { + name: "Automated Validation: Invalid Option value", + input: "test", + expectedOutput: "Option Ingredient must be one of the following: Option 1, Option 2, Option 3", + recipeConfig: [ + { + op: "Automated Validation Test Op", + args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 4"] + } + ] + }, + { + name: "Automated Validation: Option value as optgroup heading (invalid)", + input: "test", + expectedOutput: "Option Ingredient must be one of the following: Option 1, Option 2, Option 3", + recipeConfig: [ + { + op: "Automated Validation Test Op", + args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "[Group 1]"] + } + ] + }, + { + name: "Automated Validation: Option value empty (invalid)", + input: "test", + expectedOutput: "Option Ingredient cannot be empty.", + recipeConfig: [ + { + op: "Automated Validation Test Op", + args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, ""] } ] } diff --git a/tests/operations/tests/GenerateLoremIpsum.mjs b/tests/operations/tests/GenerateLoremIpsum.mjs index c42bf8da..145f2884 100644 --- a/tests/operations/tests/GenerateLoremIpsum.mjs +++ b/tests/operations/tests/GenerateLoremIpsum.mjs @@ -67,12 +67,12 @@ TestRegister.addTests([ { name: "Generate Lorem Ipsum: Incorrect lengthType", input: "", - expectedOutput: "Invalid length type", + expectedOutput: "Length in must be one of the following: Paragraphs, Sentences, Words, Bytes", recipeConfig: [ { "op": "Generate Lorem Ipsum", "args": [999_999, "Novels"] - }, + } ], }, diff --git a/tests/operations/tests/SM2.mjs b/tests/operations/tests/SM2.mjs index a3d6fd2c..53ee22ca 100644 --- a/tests/operations/tests/SM2.mjs +++ b/tests/operations/tests/SM2.mjs @@ -113,7 +113,7 @@ TestRegister.addTests([ }, { "op": "SM2 Decrypt", - "args": [PRIVATE_K, "C1C2C2", CURVE] + "args": [PRIVATE_K, "C1C2C3", CURVE] } ] },