|
1 | 1 | import { codedTypes } from '../../../constants'; |
2 | 2 | import { type FormContextProps } from '../../../provider/form-provider'; |
3 | | -import { type FormField } from '../../../types'; |
| 3 | +import { type FormFieldValidator, type SessionMode, type ValidationResult, type FormField } from '../../../types'; |
4 | 4 | import { isTrue } from '../../../utils/boolean-utils'; |
5 | 5 | import { hasRendering } from '../../../utils/common-utils'; |
6 | 6 | import { evaluateAsyncExpression, evaluateExpression } from '../../../utils/expression-runner'; |
@@ -65,6 +65,21 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon |
65 | 65 | }, |
66 | 66 | ).then((result) => { |
67 | 67 | setValue(dependent.id, result); |
| 68 | + // validate calculated value |
| 69 | + const { errors, warnings } = validateFieldValue(dependent, result, context.formFieldValidators, { |
| 70 | + formFields, |
| 71 | + values, |
| 72 | + expressionContext: { patient, mode: sessionMode }, |
| 73 | + }); |
| 74 | + if (!dependent.meta.submission) { |
| 75 | + dependent.meta.submission = {}; |
| 76 | + } |
| 77 | + dependent.meta.submission.errors = errors; |
| 78 | + dependent.meta.submission.warnings = warnings; |
| 79 | + if (!errors.length) { |
| 80 | + context.formFieldAdapters[dependent.type].transformFieldValue(dependent, result, context); |
| 81 | + } |
| 82 | + updateFormField(dependent); |
68 | 83 | }); |
69 | 84 | } |
70 | 85 | // evaluate hide |
@@ -212,3 +227,48 @@ function evaluateFieldDependents(field: FormField, values: any, context: FormCon |
212 | 227 | setForm(formJson); |
213 | 228 | } |
214 | 229 | } |
| 230 | + |
| 231 | +export interface ValidatorConfig { |
| 232 | + formFields: FormField[]; |
| 233 | + values: Record<string, any>; |
| 234 | + expressionContext: { |
| 235 | + patient: fhir.Patient; |
| 236 | + mode: SessionMode; |
| 237 | + }; |
| 238 | +} |
| 239 | + |
| 240 | +export function validateFieldValue( |
| 241 | + field: FormField, |
| 242 | + value: any, |
| 243 | + validators: Record<string, FormFieldValidator>, |
| 244 | + context: ValidatorConfig, |
| 245 | +): { errors: ValidationResult[]; warnings: ValidationResult[] } { |
| 246 | + const errors: ValidationResult[] = []; |
| 247 | + const warnings: ValidationResult[] = []; |
| 248 | + |
| 249 | + if (field.meta.submission?.unspecified) { |
| 250 | + return { errors: [], warnings: [] }; |
| 251 | + } |
| 252 | + |
| 253 | + try { |
| 254 | + field.validators.forEach((validatorConfig) => { |
| 255 | + const results = validators[validatorConfig.type]?.validate?.(field, value, { |
| 256 | + ...validatorConfig, |
| 257 | + ...context, |
| 258 | + }); |
| 259 | + if (results) { |
| 260 | + results.forEach((result) => { |
| 261 | + if (result.resultType === 'error') { |
| 262 | + errors.push(result); |
| 263 | + } else if (result.resultType === 'warning') { |
| 264 | + warnings.push(result); |
| 265 | + } |
| 266 | + }); |
| 267 | + } |
| 268 | + }); |
| 269 | + } catch (error) { |
| 270 | + console.error(error); |
| 271 | + } |
| 272 | + |
| 273 | + return { errors, warnings }; |
| 274 | +} |
0 commit comments