1
1
import isEmpty from 'lodash/isEmpty' ;
2
- import { FieldError , ValidatorError } from 'error' ;
2
+ import { FieldError , ProcessorError } from 'error' ;
3
3
import { Evaluator } from 'utils' ;
4
4
import { RadioComponent , SelectComponent , RuleFn , RuleFnSync , ValidationContext } from 'types' ;
5
5
import { isObject , isPromise } from '../util' ;
@@ -36,30 +36,36 @@ function mapStaticValues(values: { label: string; value: string }[]) {
36
36
return values . map ( ( obj ) => obj . value ) ;
37
37
}
38
38
39
- async function getAvailableSelectValues ( component : SelectComponent ) {
39
+ async function getAvailableSelectValues ( component : SelectComponent , context : ValidationContext ) {
40
40
switch ( component . dataSrc ) {
41
41
case 'values' :
42
42
if ( Array . isArray ( component . data . values ) ) {
43
43
return mapStaticValues ( component . data . values ) ;
44
44
}
45
- throw new ValidatorError (
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 ValidatorError (
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 ValidatorError (
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
}
@@ -76,48 +82,60 @@ async function getAvailableSelectValues(component: SelectComponent) {
76
82
if ( Array . isArray ( resolvedCustomItems ) ) {
77
83
return resolvedCustomItems ;
78
84
}
79
- throw new ValidatorError (
80
- `Failed to validate available values in JSON select component '${ component . key } ': the values are not an array`
85
+ throw new ProcessorError (
86
+ `Failed to validate available values in JSON select component '${ component . key } ': the values are not an array` ,
87
+ context ,
88
+ 'validate:validateAvailableItems'
81
89
) ;
82
90
}
83
91
if ( Array . isArray ( customItems ) ) {
84
92
return customItems ;
85
93
} else {
86
- throw new ValidatorError (
87
- `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'
88
98
) ;
89
99
}
90
100
default :
91
- throw new ValidatorError (
92
- `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'
93
105
) ;
94
106
}
95
107
}
96
108
97
- function getAvailableSelectValuesSync ( component : SelectComponent ) {
109
+ function getAvailableSelectValuesSync ( component : SelectComponent , context : ValidationContext ) {
98
110
switch ( component . dataSrc ) {
99
111
case 'values' :
100
112
if ( Array . isArray ( component . data ?. values ) ) {
101
113
return mapStaticValues ( component . data . values ) ;
102
114
}
103
- throw new ValidatorError (
104
- `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'
105
119
) ;
106
120
case 'json' : {
107
121
if ( typeof component . data . json === 'string' ) {
108
122
try {
109
123
return mapDynamicValues ( component , JSON . parse ( component . data . json ) ) ;
110
124
} catch ( err ) {
111
- throw new ValidatorError (
112
- `Failed to validate available values in JSON select component '${ component . key } ': ${ err } `
125
+ throw new ProcessorError (
126
+ `Failed to validate available values in JSON select component '${ component . key } ': ${ err } ` ,
127
+ context ,
128
+ 'validate:validateAvailableItems'
113
129
) ;
114
130
}
115
131
} else if ( Array . isArray ( component . data . json ) ) {
116
132
// TODO: need to retype this
117
133
return mapDynamicValues ( component , component . data . json as Record < string , any > [ ] ) ;
118
134
} else {
119
- throw new ValidatorError (
120
- `Failed to validate available values in JSON select component '${ component . key } ': the values are not an array`
135
+ throw new ProcessorError (
136
+ `Failed to validate available values in JSON select component '${ component . key } ': the values are not an array` ,
137
+ context ,
138
+ 'validate:validateAvailableItems'
121
139
) ;
122
140
}
123
141
}
@@ -132,18 +150,22 @@ function getAvailableSelectValuesSync(component: SelectComponent) {
132
150
if ( Array . isArray ( customItems ) ) {
133
151
return customItems ;
134
152
} else {
135
- throw new ValidatorError (
136
- `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'
137
157
) ;
138
158
}
139
159
default :
140
- throw new ValidatorError (
141
- `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'
142
164
) ;
143
165
}
144
166
}
145
167
146
- function compareComplexValues ( valueA : unknown , valueB : unknown ) {
168
+ function compareComplexValues ( valueA : unknown , valueB : unknown , context : ValidationContext ) {
147
169
if ( ! isObject ( valueA ) || ! isObject ( valueB ) ) {
148
170
return false ;
149
171
}
@@ -153,41 +175,45 @@ function compareComplexValues(valueA: unknown, valueB: unknown) {
153
175
// this won't work
154
176
return JSON . stringify ( valueA ) === JSON . stringify ( valueB ) ;
155
177
} catch ( err ) {
156
- throw new ValidatorError ( `Error while comparing available values: ${ err } ` ) ;
178
+ throw new ProcessorError ( `Error while comparing available values: ${ err } ` , context , 'validate:validateAvailableItems' ) ;
157
179
}
158
180
}
159
181
160
182
export const validateAvailableItems : RuleFn = async ( context : ValidationContext ) => {
161
183
const { component, value } = context ;
162
184
const error = new FieldError ( 'invalidOption' , context , 'onlyAvailableItems' ) ;
163
- if ( isValidatableRadioComponent ( component ) ) {
164
- if ( value == null || isEmpty ( value ) ) {
165
- return null ;
166
- }
167
-
168
- const values = component . values ;
169
- if ( values ) {
170
- return values . findIndex ( ( { value : optionValue } ) => optionValue === value ) !== - 1
171
- ? null
172
- : error ;
173
- }
185
+ try {
186
+ if ( isValidatableRadioComponent ( component ) ) {
187
+ if ( value == null || isEmpty ( value ) ) {
188
+ return null ;
189
+ }
174
190
175
- return null ;
176
- } else if ( isValidateableSelectComponent ( component ) ) {
177
- if ( value == null || isEmpty ( value ) ) {
178
- return null ;
179
- }
180
- const values = await getAvailableSelectValues ( component ) ;
181
- if ( values ) {
182
- if ( isObject ( value ) ) {
183
- return values . find ( ( optionValue ) => compareComplexValues ( optionValue , value ) ) !==
184
- undefined
191
+ const values = component . values ;
192
+ if ( values ) {
193
+ return values . findIndex ( ( { value : optionValue } ) => optionValue === value ) !== - 1
185
194
? null
186
195
: error ;
187
196
}
188
197
189
- return values . find ( ( optionValue ) => optionValue === value ) !== undefined ? null : error ;
198
+ return null ;
199
+ } else if ( isValidateableSelectComponent ( component ) ) {
200
+ if ( value == null || isEmpty ( value ) ) {
201
+ return null ;
202
+ }
203
+ const values = await getAvailableSelectValues ( component , context ) ;
204
+ if ( values ) {
205
+ if ( isObject ( value ) ) {
206
+ return values . find ( ( optionValue ) => compareComplexValues ( optionValue , value , context ) ) !==
207
+ undefined
208
+ ? null
209
+ : error ;
210
+ }
211
+
212
+ return values . find ( ( optionValue ) => optionValue === value ) !== undefined ? null : error ;
213
+ }
190
214
}
215
+ } catch ( err : any ) {
216
+ throw new ProcessorError ( err . message || err , context , 'validate:validateAvailableItems' ) ;
191
217
}
192
218
return null ;
193
219
} ;
@@ -209,28 +235,32 @@ export const shouldValidate = (context: any) => {
209
235
export const validateAvailableItemsSync : RuleFnSync = ( context : ValidationContext ) => {
210
236
const { component, value } = context ;
211
237
const error = new FieldError ( 'invalidOption' , context , 'onlyAvailableItems' ) ;
212
- if ( ! shouldValidate ( context ) ) {
213
- return null ;
214
- }
215
- if ( isValidatableRadioComponent ( component ) ) {
216
- const values = component . values ;
217
- if ( values ) {
218
- return values . findIndex ( ( { value : optionValue } ) => optionValue === value ) !== - 1
219
- ? null
220
- : error ;
238
+ try {
239
+ if ( ! shouldValidate ( context ) ) {
240
+ return null ;
221
241
}
222
- return null ;
223
- } else if ( isValidateableSelectComponent ( component ) ) {
224
- const values = getAvailableSelectValuesSync ( component ) ;
225
- if ( values ) {
226
- if ( isObject ( value ) ) {
227
- return values . find ( ( optionValue ) => compareComplexValues ( optionValue , value ) ) !==
228
- undefined
242
+ if ( isValidatableRadioComponent ( component ) ) {
243
+ const values = component . values ;
244
+ if ( values ) {
245
+ return values . findIndex ( ( { value : optionValue } ) => optionValue === value ) !== - 1
229
246
? null
230
247
: error ;
231
248
}
232
- return values . find ( ( optionValue ) => optionValue === value ) !== undefined ? null : error ;
249
+ return null ;
250
+ } else if ( isValidateableSelectComponent ( component ) ) {
251
+ const values = getAvailableSelectValuesSync ( component , context ) ;
252
+ if ( values ) {
253
+ if ( isObject ( value ) ) {
254
+ return values . find ( ( optionValue ) => compareComplexValues ( optionValue , value , context ) ) !==
255
+ undefined
256
+ ? null
257
+ : error ;
258
+ }
259
+ return values . find ( ( optionValue ) => optionValue === value ) !== undefined ? null : error ;
260
+ }
233
261
}
262
+ } catch ( err : any ) {
263
+ throw new ProcessorError ( err . message || err , context , 'validate:validateAvailableItems' ) ;
234
264
}
235
265
return null ;
236
266
} ;
0 commit comments