From 6e4b3890f35cf89dc8f7ef82656b74e66a3ca77a Mon Sep 17 00:00:00 2001 From: kajambiya Date: Mon, 17 Apr 2023 16:22:16 +0300 Subject: [PATCH 1/4] add error message for min and max on number field --- src/components/encounter/ohri-encounter-form.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/components/encounter/ohri-encounter-form.tsx b/src/components/encounter/ohri-encounter-form.tsx index 891e48fa7..a2e501427 100644 --- a/src/components/encounter/ohri-encounter-form.tsx +++ b/src/components/encounter/ohri-encounter-form.tsx @@ -425,6 +425,22 @@ export const OHRIEncounterForm: React.FC = ({ errors.push(...errorsAndWarinings.filter(error => error.resultType == 'error')); warnings.push(...errorsAndWarinings.filter(error => error.resultType == 'warning')); } + if (field.questionOptions.rendering == 'number') { + let min = field.questionOptions.min; + let max = field.questionOptions.max; + if (min && Number(value) < Number(min)) { + errors.push({ + resultType: 'error', + message: `Field value can't be less than ${min}`, + }); + if (max && Number(value) > Number(max)) { + errors.push({ + resultType: 'error', + message: `Field value can't be greater than ${max}`, + }); + } + } + } setErrors?.(errors); setWarnings?.(warnings); if (errors.length) { From 17edde1df3552e39b90acee436798a794df34b3e Mon Sep 17 00:00:00 2001 From: kajambiya Date: Wed, 19 Apr 2023 08:44:23 +0300 Subject: [PATCH 2/4] refactor code --- .../encounter/ohri-encounter-form.tsx | 23 +++++++------------ src/validators/default-value-validator.ts | 19 +++++++++++++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/components/encounter/ohri-encounter-form.tsx b/src/components/encounter/ohri-encounter-form.tsx index a2e501427..da0761f8a 100644 --- a/src/components/encounter/ohri-encounter-form.tsx +++ b/src/components/encounter/ohri-encounter-form.tsx @@ -28,6 +28,7 @@ import { scrollIntoView } from '../../utils/ohri-sidebar'; import { useEncounter } from '../../hooks/useEncounter'; import { useInitialValues } from '../../hooks/useInitialValues'; import { useEncounterRole } from '../../hooks/useEncounterRole'; +import { OHRIDefaultFieldValueValidator } from '../../validators/default-value-validator'; interface OHRIEncounterFormProps { formJson: OHRIFormSchema; @@ -394,6 +395,7 @@ export const OHRIEncounterForm: React.FC = ({ obs: obsForSubmission, }; } + if (encounterForSubmission.obs?.length || encounterForSubmission.orders?.length) { const ac = new AbortController(); return saveEncounter(ac, encounterForSubmission, encounter?.uuid); @@ -423,23 +425,14 @@ export const OHRIEncounterForm: React.FC = ({ getValidator(validatorConfig.type)?.validate(field, value, { ...basevalidatorConfig, ...validatorConfig }) || []; errors.push(...errorsAndWarinings.filter(error => error.resultType == 'error')); - warnings.push(...errorsAndWarinings.filter(error => error.resultType == 'warning')); - } - if (field.questionOptions.rendering == 'number') { - let min = field.questionOptions.min; - let max = field.questionOptions.max; - if (min && Number(value) < Number(min)) { - errors.push({ - resultType: 'error', - message: `Field value can't be less than ${min}`, + + const defaultValueValidation = OHRIDefaultFieldValueValidator.validate(field, value); + if (defaultValueValidation.length) { + defaultValueValidation.map(error => { + errors.push(error); }); - if (max && Number(value) > Number(max)) { - errors.push({ - resultType: 'error', - message: `Field value can't be greater than ${max}`, - }); - } } + warnings.push(...errorsAndWarinings.filter(error => error.resultType == 'warning')); } setErrors?.(errors); setWarnings?.(warnings); diff --git a/src/validators/default-value-validator.ts b/src/validators/default-value-validator.ts index 86a8d1259..2c6e17b04 100644 --- a/src/validators/default-value-validator.ts +++ b/src/validators/default-value-validator.ts @@ -23,6 +23,25 @@ export const OHRIDefaultFieldValueValidator: FieldValidator = { return [ { resultType: 'error', errCode: 'invalid.defaultValue', message: `Invalid numerical value: '${value}'` }, ]; + } else { + let min = field.questionOptions.min; + let max = field.questionOptions.max; + if (min && Number(value) < Number(min)) { + return [ + { + resultType: 'error', + message: `Field value can't be less than ${min}`, + }, + ]; + } + if (max && Number(value) > Number(max)) { + return [ + { + resultType: 'error', + message: `Field value can't be greater than ${max}`, + }, + ]; + } } } return []; From ab6e841a605d8efef8dac6af751a165e436a4e5d Mon Sep 17 00:00:00 2001 From: kajambiya Date: Wed, 19 Apr 2023 09:43:04 +0300 Subject: [PATCH 3/4] more code refactor --- .../encounter/ohri-encounter-form.tsx | 8 ------ src/validators/default-value-validator.ts | 19 -------------- src/validators/ohri-form-validator.ts | 25 +++++++++++++++++++ 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/components/encounter/ohri-encounter-form.tsx b/src/components/encounter/ohri-encounter-form.tsx index da0761f8a..c7b00a35b 100644 --- a/src/components/encounter/ohri-encounter-form.tsx +++ b/src/components/encounter/ohri-encounter-form.tsx @@ -28,7 +28,6 @@ import { scrollIntoView } from '../../utils/ohri-sidebar'; import { useEncounter } from '../../hooks/useEncounter'; import { useInitialValues } from '../../hooks/useInitialValues'; import { useEncounterRole } from '../../hooks/useEncounterRole'; -import { OHRIDefaultFieldValueValidator } from '../../validators/default-value-validator'; interface OHRIEncounterFormProps { formJson: OHRIFormSchema; @@ -425,13 +424,6 @@ export const OHRIEncounterForm: React.FC = ({ getValidator(validatorConfig.type)?.validate(field, value, { ...basevalidatorConfig, ...validatorConfig }) || []; errors.push(...errorsAndWarinings.filter(error => error.resultType == 'error')); - - const defaultValueValidation = OHRIDefaultFieldValueValidator.validate(field, value); - if (defaultValueValidation.length) { - defaultValueValidation.map(error => { - errors.push(error); - }); - } warnings.push(...errorsAndWarinings.filter(error => error.resultType == 'warning')); } setErrors?.(errors); diff --git a/src/validators/default-value-validator.ts b/src/validators/default-value-validator.ts index 2c6e17b04..86a8d1259 100644 --- a/src/validators/default-value-validator.ts +++ b/src/validators/default-value-validator.ts @@ -23,25 +23,6 @@ export const OHRIDefaultFieldValueValidator: FieldValidator = { return [ { resultType: 'error', errCode: 'invalid.defaultValue', message: `Invalid numerical value: '${value}'` }, ]; - } else { - let min = field.questionOptions.min; - let max = field.questionOptions.max; - if (min && Number(value) < Number(min)) { - return [ - { - resultType: 'error', - message: `Field value can't be less than ${min}`, - }, - ]; - } - if (max && Number(value) > Number(max)) { - return [ - { - resultType: 'error', - message: `Field value can't be greater than ${max}`, - }, - ]; - } } } return []; diff --git a/src/validators/ohri-form-validator.ts b/src/validators/ohri-form-validator.ts index a14da9db1..e82e5d5e6 100644 --- a/src/validators/ohri-form-validator.ts +++ b/src/validators/ohri-form-validator.ts @@ -2,6 +2,7 @@ import { FieldValidator, OHRIFormField } from '../api/types'; import { isTrue } from '../utils/boolean-utils'; export const fieldRequiredErrCode = 'field.required'; +export const fieldOutOfBoundErrCode = 'field.outOfBound'; export const OHRIFieldValidator: FieldValidator = { validate: (field: OHRIFormField, value: any) => { @@ -13,6 +14,30 @@ export const OHRIFieldValidator: FieldValidator = { return [{ resultType: 'error', errCode: fieldRequiredErrCode, message: 'Field is mandatory' }]; } } + if (field.questionOptions.rendering == 'number') { + let min = field.questionOptions.min; + let max = field.questionOptions.max; + + if (min && Number(value) < Number(min)) { + return [ + { + resultType: 'error', + errCode: fieldOutOfBoundErrCode, + message: `Field value can't be less than ${min}`, + }, + ]; + } + + if (max && Number(value) > Number(max)) { + return [ + { + resultType: 'error', + errCode: fieldOutOfBoundErrCode, + message: `Field value can't be greater than ${max}`, + }, + ]; + } + } return []; }, }; From bbd8197b62ffc1aa924a14aefb0233d46a39fdab Mon Sep 17 00:00:00 2001 From: kajambiya Date: Wed, 19 Apr 2023 11:17:32 +0300 Subject: [PATCH 4/4] remove number conversion on value --- src/validators/ohri-form-validator.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/validators/ohri-form-validator.ts b/src/validators/ohri-form-validator.ts index e82e5d5e6..22687abd4 100644 --- a/src/validators/ohri-form-validator.ts +++ b/src/validators/ohri-form-validator.ts @@ -15,24 +15,25 @@ export const OHRIFieldValidator: FieldValidator = { } } if (field.questionOptions.rendering == 'number') { - let min = field.questionOptions.min; - let max = field.questionOptions.max; - - if (min && Number(value) < Number(min)) { + const min = field.questionOptions.min; + const max = field.questionOptions.max; + if (min && value < Number(min)) { return [ { resultType: 'error', errCode: fieldOutOfBoundErrCode, + // TODO: handle i18n message: `Field value can't be less than ${min}`, }, ]; } - if (max && Number(value) > Number(max)) { + if (max && value > Number(max)) { return [ { resultType: 'error', errCode: fieldOutOfBoundErrCode, + // TODO: handle i18n message: `Field value can't be greater than ${max}`, }, ];