@@ -42,24 +42,30 @@ async function getAvailableSelectValues(component: SelectComponent, context: Val
42
42
if ( Array . isArray ( component . data . values ) ) {
43
43
return mapStaticValues ( component . data . values ) ;
44
44
}
45
- throw new Error (
45
+ throw new ProcessorError (
46
46
`Failed to validate available values in static values select component '${ component . key } ': the values are not an array` ,
47
+ context ,
48
+ 'validate:validateAvailableItems'
47
49
) ;
48
50
case 'json' : {
49
51
if ( typeof component . data . json === 'string' ) {
50
52
try {
51
53
return mapDynamicValues ( component , JSON . parse ( component . data . json ) ) ;
52
54
} 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'
55
59
) ;
56
60
}
57
61
} else if ( Array . isArray ( component . data . json ) ) {
58
62
// TODO: need to retype this
59
63
return mapDynamicValues ( component , component . data . json as Record < string , any > [ ] ) ;
60
64
} 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'
63
69
) ;
64
70
}
65
71
}
@@ -85,13 +91,17 @@ async function getAvailableSelectValues(component: SelectComponent, context: Val
85
91
if ( Array . isArray ( customItems ) ) {
86
92
return customItems ;
87
93
} 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'
90
98
) ;
91
99
}
92
100
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'
95
105
) ;
96
106
}
97
107
}
@@ -102,8 +112,10 @@ function getAvailableSelectValuesSync(component: SelectComponent, context: Valid
102
112
if ( Array . isArray ( component . data ?. values ) ) {
103
113
return mapStaticValues ( component . data . values ) ;
104
114
}
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'
107
119
) ;
108
120
case 'json' : {
109
121
if ( typeof component . data . json === 'string' ) {
@@ -138,18 +150,22 @@ function getAvailableSelectValuesSync(component: SelectComponent, context: Valid
138
150
if ( Array . isArray ( customItems ) ) {
139
151
return customItems ;
140
152
} 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'
143
157
) ;
144
158
}
145
159
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'
148
164
) ;
149
165
}
150
166
}
151
167
152
- function compareComplexValues ( valueA : unknown , valueB : unknown ) {
168
+ function compareComplexValues ( valueA : unknown , valueB : unknown , context : ValidationContext ) {
153
169
if ( ! isObject ( valueA ) || ! isObject ( valueB ) ) {
154
170
return false ;
155
171
}
@@ -159,7 +175,7 @@ function compareComplexValues(valueA: unknown, valueB: unknown) {
159
175
// this won't work
160
176
return JSON . stringify ( valueA ) === JSON . stringify ( valueB ) ;
161
177
} 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' ) ;
163
179
}
164
180
}
165
181
@@ -187,7 +203,7 @@ export const validateAvailableItems: RuleFn = async (context: ValidationContext)
187
203
const values = await getAvailableSelectValues ( component , context ) ;
188
204
if ( values ) {
189
205
if ( isObject ( value ) ) {
190
- return values . find ( ( optionValue ) => compareComplexValues ( optionValue , value ) ) !==
206
+ return values . find ( ( optionValue ) => compareComplexValues ( optionValue , value , context ) ) !==
191
207
undefined
192
208
? null
193
209
: error ;
@@ -236,7 +252,7 @@ export const validateAvailableItemsSync: RuleFnSync = (context: ValidationContex
236
252
const values = getAvailableSelectValuesSync ( component , context ) ;
237
253
if ( values ) {
238
254
if ( isObject ( value ) ) {
239
- return values . find ( ( optionValue ) => compareComplexValues ( optionValue , value ) ) !==
255
+ return values . find ( ( optionValue ) => compareComplexValues ( optionValue , value , context ) ) !==
240
256
undefined
241
257
? null
242
258
: error ;
0 commit comments