Skip to content
Open
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
24 changes: 23 additions & 1 deletion src/core/Ingredient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class Ingredient {
if (this.type === "option" && Array.isArray(checkVal)) {
checkVal = checkVal[this.defaultIndex ?? 0];
}
if (this.type === "argSelector" && Array.isArray(checkVal)) {
checkVal = checkVal[this.defaultIndex ?? 0]?.name || "";
}

// 1. check if empty
let isEmpty = false;
Expand All @@ -99,8 +102,10 @@ class Ingredient {
let isAllowedOptionEmpty = false;
if (this.type === "option" && Array.isArray(this.defaultValue)) {
isAllowedOptionEmpty = this.defaultValue.includes("");
} else if (this.type === "argSelector" && Array.isArray(this.defaultValue)) {
isAllowedOptionEmpty = this.defaultValue.some(opt => opt.name === "");
}
if (this.allowEmpty === false || (this.type === "option" && !isAllowedOptionEmpty)) {
if (this.allowEmpty === false || ((this.type === "option" || this.type === "argSelector") && !isAllowedOptionEmpty)) {
throw new OperationError(`${this.name} cannot be empty.`);
}
return true;
Expand Down Expand Up @@ -150,6 +155,23 @@ class Ingredient {
}
}

// 5. argSelector checks
if (this.type === "argSelector") {
if (Array.isArray(this.defaultValue)) {
const permittedOptions = this.defaultValue
.map(opt => opt.name)
.filter(optName => {
if (typeof optName !== "string") return false;
return !optName.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;
}

Expand Down
17 changes: 17 additions & 0 deletions src/core/operations/AutomatedValidationTestOp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ class AutomatedValidationTestOp extends Operation {
"type": "option",
"value": ["[Group 1]", "Option 1", "Option 2", "[/Group 1]", "[Group 2]", "Option 3", "[/Group 2]"],
"allowEmpty": false
},
{
"name": "Arg Selector Ingredient",
"type": "argSelector",
"value": [
{
name: "Option 1",
on: [0],
off: [1]
},
{
name: "Option 2",
on: [1],
off: [0]
}
],
"allowEmpty": false
}
];
}
Expand Down
4 changes: 4 additions & 0 deletions src/node/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ function transformArgs(opArgsList, newArgs) {
return !Array.isArray(arg.value) ? arg.value : arg.value[arg.defaultIndex ?? 0];
}

if (arg.type === "argSelector") {
return !Array.isArray(arg.value) ? arg.value : (arg.value[arg.defaultIndex ?? 0]?.name || "");
}

if (arg.type === "editableOption") {
return typeof arg.value === "string" ? arg.value : arg.value[arg.defaultIndex ?? 0].value;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/operations/tests/AutomatedValidation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,38 @@ TestRegister.addTests([
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, ""]
}
]
},
{
name: "Automated Validation: Valid Arg Selector value",
input: "test",
expectedOutput: "Success",
recipeConfig: [
{
op: "Automated Validation Test Op",
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1", "Option 2"]
}
]
},
{
name: "Automated Validation: Invalid Arg Selector value",
input: "test",
expectedOutput: "Arg Selector Ingredient must be one of the following: Option 1, Option 2.",
recipeConfig: [
{
op: "Automated Validation Test Op",
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1", "Option 3"]
}
]
},
{
name: "Automated Validation: Arg Selector value empty (invalid)",
input: "test",
expectedOutput: "Arg Selector Ingredient cannot be empty.",
recipeConfig: [
{
op: "Automated Validation Test Op",
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1", ""]
}
]
}
]);