Refactor option validation to resolve defaults and support empty option lists, and update test expectations
This commit is contained in:
parent
1cc1d7808c
commit
1e2e200bd3
@ -76,8 +76,12 @@ class Ingredient {
|
|||||||
if (this.disabled) return true;
|
if (this.disabled) return true;
|
||||||
|
|
||||||
let checkVal = val;
|
let checkVal = val;
|
||||||
if (this.type === "toggleString" && val && typeof val === "object" && "string" in val) {
|
if (checkVal === null || checkVal === undefined) {
|
||||||
checkVal = val.string;
|
checkVal = this.defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.type === "toggleString" && checkVal && typeof checkVal === "object" && "string" in checkVal) {
|
||||||
|
checkVal = checkVal.string;
|
||||||
}
|
}
|
||||||
if (this.type === "option" && Array.isArray(checkVal)) {
|
if (this.type === "option" && Array.isArray(checkVal)) {
|
||||||
checkVal = checkVal[this.defaultIndex ?? 0];
|
checkVal = checkVal[this.defaultIndex ?? 0];
|
||||||
@ -92,7 +96,11 @@ class Ingredient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isEmpty) {
|
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.`);
|
throw new OperationError(`${this.name} cannot be empty.`);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -113,16 +121,16 @@ class Ingredient {
|
|||||||
|
|
||||||
// 3. number checks
|
// 3. number checks
|
||||||
if (this.type === "number") {
|
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.`);
|
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.`);
|
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}.`);
|
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}.`);
|
throw new OperationError(`${this.name} must be less than or equal to ${this.max}.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,7 +71,7 @@ TestRegister.addTests([
|
|||||||
{
|
{
|
||||||
name: "Encode text: empty encoding",
|
name: "Encode text: empty encoding",
|
||||||
input: "hello",
|
input: "hello",
|
||||||
expectedOutput: "Invalid encoding",
|
expectedOutput: "Encoding cannot be empty.",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
"op": "Encode text",
|
"op": "Encode text",
|
||||||
@ -82,7 +82,7 @@ TestRegister.addTests([
|
|||||||
{
|
{
|
||||||
name: "Decode text: empty encoding",
|
name: "Decode text: empty encoding",
|
||||||
input: "68 65 6c 6c 6f",
|
input: "68 65 6c 6c 6f",
|
||||||
expectedOutput: "Invalid encoding",
|
expectedOutput: "Encoding cannot be empty.",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
"op": "From Hex",
|
"op": "From Hex",
|
||||||
|
|||||||
@ -48,7 +48,7 @@ TestRegister.addTests([
|
|||||||
{
|
{
|
||||||
name: "Generate Image: empty mode",
|
name: "Generate Image: empty mode",
|
||||||
input: "",
|
input: "",
|
||||||
expectedOutput: "Unsupported Mode: ()",
|
expectedOutput: "Mode cannot be empty.",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "Generate Image",
|
op: "Generate Image",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user