Skip to content

Commit ac961eb

Browse files
committed
add more processorerrors
1 parent b18b7be commit ac961eb

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/process/validation/rules/validateAvailableItems.ts

+35-19
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,30 @@ async function getAvailableSelectValues(component: SelectComponent, context: Val
4242
if (Array.isArray(component.data.values)) {
4343
return mapStaticValues(component.data.values);
4444
}
45-
throw new Error(
45+
throw new ProcessorError(
4646
`Failed to validate available values in static values select component '${component.key}': the values are not an array`,
47+
context,
48+
'validate:validateAvailableItems'
4749
);
4850
case 'json': {
4951
if (typeof component.data.json === 'string') {
5052
try {
5153
return mapDynamicValues(component, JSON.parse(component.data.json));
5254
} catch (err) {
53-
throw new Error(
54-
`Failed to validate available values in JSON select component '${component.key}': ${err}`
55+
throw new ProcessorError(
56+
`Failed to validate available values in JSON select component '${component.key}': ${err}`,
57+
context,
58+
'validate:validateAvailableItems'
5559
);
5660
}
5761
} else if (Array.isArray(component.data.json)) {
5862
// TODO: need to retype this
5963
return mapDynamicValues(component, component.data.json as Record<string, any>[]);
6064
} else {
61-
throw new Error(
62-
`Failed to validate available values in JSON select component '${component.key}': the values are not an array`
65+
throw new ProcessorError(
66+
`Failed to validate available values in JSON select component '${component.key}': the values are not an array`,
67+
context,
68+
'validate:validateAvailableItems'
6369
);
6470
}
6571
}
@@ -85,13 +91,17 @@ async function getAvailableSelectValues(component: SelectComponent, context: Val
8591
if (Array.isArray(customItems)) {
8692
return customItems;
8793
} else {
88-
throw new Error(
89-
`Failed to validate available values in JSON select component '${component.key}': the values are not an array`
94+
throw new ProcessorError(
95+
`Failed to validate available values in JSON select component '${component.key}': the values are not an array`,
96+
context,
97+
'validate:validateAvailableItems'
9098
);
9199
}
92100
default:
93-
throw new Error(
94-
`Failed to validate available values in select component '${component.key}': data source ${component.dataSrc} is not valid}`
101+
throw new ProcessorError(
102+
`Failed to validate available values in select component '${component.key}': data source ${component.dataSrc} is not valid}`,
103+
context,
104+
'validate:validateAvailableItems'
95105
);
96106
}
97107
}
@@ -102,8 +112,10 @@ function getAvailableSelectValuesSync(component: SelectComponent, context: Valid
102112
if (Array.isArray(component.data?.values)) {
103113
return mapStaticValues(component.data.values);
104114
}
105-
throw new Error(
106-
`Failed to validate available values in static values select component '${component.key}': the values are not an array`
115+
throw new ProcessorError(
116+
`Failed to validate available values in static values select component '${component.key}': the values are not an array`,
117+
context,
118+
'validate:validateAvailableItems'
107119
);
108120
case 'json': {
109121
if (typeof component.data.json === 'string') {
@@ -138,18 +150,22 @@ function getAvailableSelectValuesSync(component: SelectComponent, context: Valid
138150
if (Array.isArray(customItems)) {
139151
return customItems;
140152
} else {
141-
throw new Error(
142-
`Failed to validate available values in JSON select component '${component.key}': the values are not an array`
153+
throw new ProcessorError(
154+
`Failed to validate available values in JSON select component '${component.key}': the values are not an array`,
155+
context,
156+
'validate:validateAvailableItems'
143157
);
144158
}
145159
default:
146-
throw new Error(
147-
`Failed to validate available values in select component '${component.key}': data source ${component.dataSrc} is not valid}`
160+
throw new ProcessorError(
161+
`Failed to validate available values in select component '${component.key}': data source ${component.dataSrc} is not valid}`,
162+
context,
163+
'validate:validateAvailableItems'
148164
);
149165
}
150166
}
151167

152-
function compareComplexValues(valueA: unknown, valueB: unknown) {
168+
function compareComplexValues(valueA: unknown, valueB: unknown, context: ValidationContext) {
153169
if (!isObject(valueA) || !isObject(valueB)) {
154170
return false;
155171
}
@@ -159,7 +175,7 @@ function compareComplexValues(valueA: unknown, valueB: unknown) {
159175
// this won't work
160176
return JSON.stringify(valueA) === JSON.stringify(valueB);
161177
} catch (err) {
162-
throw new Error(`Error while comparing available values: ${err}`);
178+
throw new ProcessorError(`Error while comparing available values: ${err}`, context, 'validate:validateAvailableItems');
163179
}
164180
}
165181

@@ -187,7 +203,7 @@ export const validateAvailableItems: RuleFn = async (context: ValidationContext)
187203
const values = await getAvailableSelectValues(component, context);
188204
if (values) {
189205
if (isObject(value)) {
190-
return values.find((optionValue) => compareComplexValues(optionValue, value)) !==
206+
return values.find((optionValue) => compareComplexValues(optionValue, value, context)) !==
191207
undefined
192208
? null
193209
: error;
@@ -236,7 +252,7 @@ export const validateAvailableItemsSync: RuleFnSync = (context: ValidationContex
236252
const values = getAvailableSelectValuesSync(component, context);
237253
if (values) {
238254
if (isObject(value)) {
239-
return values.find((optionValue) => compareComplexValues(optionValue, value)) !==
255+
return values.find((optionValue) => compareComplexValues(optionValue, value, context)) !==
240256
undefined
241257
? null
242258
: error;

0 commit comments

Comments
 (0)