diff --git a/src/core/Ingredient.mjs b/src/core/Ingredient.mjs index d01c6d34..5902e2da 100644 --- a/src/core/Ingredient.mjs +++ b/src/core/Ingredient.mjs @@ -76,8 +76,12 @@ class Ingredient { if (this.disabled) return true; let checkVal = val; - if (this.type === "toggleString" && val && typeof val === "object" && "string" in val) { - checkVal = val.string; + if (checkVal === null || checkVal === undefined) { + checkVal = this.defaultValue; + } + + if (this.type === "toggleString" && checkVal && typeof checkVal === "object" && "string" in checkVal) { + checkVal = checkVal.string; } if (this.type === "option" && Array.isArray(checkVal)) { checkVal = checkVal[this.defaultIndex ?? 0]; @@ -92,7 +96,11 @@ class Ingredient { } if (isEmpty) { - if (this.allowEmpty === false) { + let isAllowedOptionEmpty = false; + if (this.type === "option" && Array.isArray(this.defaultValue)) { + isAllowedOptionEmpty = this.defaultValue.includes(""); + } + if (this.allowEmpty === false || (this.type === "option" && !isAllowedOptionEmpty)) { throw new OperationError(`${this.name} cannot be empty.`); } return true; @@ -113,16 +121,16 @@ class Ingredient { // 3. number checks if (this.type === "number") { - if (val === null || val === undefined || isNaN(val)) { + if (checkVal === null || checkVal === undefined || isNaN(checkVal)) { throw new OperationError(`${this.name} must be a number.`); } - if (this.integer && !Number.isInteger(val)) { + if (this.integer && !Number.isInteger(checkVal)) { throw new OperationError(`${this.name} must be an integer.`); } - if (typeof this.min === "number" && val < this.min) { + if (typeof this.min === "number" && checkVal < this.min) { throw new OperationError(`${this.name} must be greater than or equal to ${this.min}.`); } - if (typeof this.max === "number" && val > this.max) { + if (typeof this.max === "number" && checkVal > this.max) { throw new OperationError(`${this.name} must be less than or equal to ${this.max}.`); } } diff --git a/tests/operations/tests/CharEnc.mjs b/tests/operations/tests/CharEnc.mjs index 83f71ca9..88991761 100644 --- a/tests/operations/tests/CharEnc.mjs +++ b/tests/operations/tests/CharEnc.mjs @@ -71,7 +71,7 @@ TestRegister.addTests([ { name: "Encode text: empty encoding", input: "hello", - expectedOutput: "Invalid encoding", + expectedOutput: "Encoding cannot be empty.", recipeConfig: [ { "op": "Encode text", @@ -82,7 +82,7 @@ TestRegister.addTests([ { name: "Decode text: empty encoding", input: "68 65 6c 6c 6f", - expectedOutput: "Invalid encoding", + expectedOutput: "Encoding cannot be empty.", recipeConfig: [ { "op": "From Hex", diff --git a/tests/operations/tests/Image.mjs b/tests/operations/tests/Image.mjs index fe6cab10..a7b1167b 100644 --- a/tests/operations/tests/Image.mjs +++ b/tests/operations/tests/Image.mjs @@ -48,7 +48,7 @@ TestRegister.addTests([ { name: "Generate Image: empty mode", input: "", - expectedOutput: "Unsupported Mode: ()", + expectedOutput: "Mode cannot be empty.", recipeConfig: [ { op: "Generate Image",