From 15a45a9e7d92422a60fb097b18edf101bf389ae2 Mon Sep 17 00:00:00 2001 From: jabahum Date: Thu, 9 May 2024 19:44:45 +0300 Subject: [PATCH] fix comments --- src/validators/form-validator.test.ts | 24 +++++++++++++ src/validators/form-validator.ts | 52 +++++++++++++++------------ translations/en.json | 8 ++++- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/validators/form-validator.test.ts b/src/validators/form-validator.test.ts index 8323708c0..1755a3af4 100644 --- a/src/validators/form-validator.test.ts +++ b/src/validators/form-validator.test.ts @@ -14,6 +14,19 @@ describe('FieldValidator - validate', () => { id: 'sampleNumberQuestion', }; + const numberWithDecimalsInputField: FormField = { + label: 'A Question of type obs that renders a Number input', + type: 'obs', + questionOptions: { + rendering: 'number', + concept: 'a-system-defined-concept-uuid', + disallowDecimals: true, + min: '5.0', + max: '10.0', + }, + id: 'sampleNumberWithDecimalsQuestion', + }; + const textInputField: FormField = { label: 'A Question of type obs that renders a Text input', type: 'obs', @@ -154,4 +167,15 @@ describe('FieldValidator - validate', () => { }, ]); }); + + it('should fail for numbers with decimals', () => { + const validationErrors = FieldValidator.validate(numberWithDecimalsInputField, 100.5); + expect(validationErrors).toEqual([ + { + errCode: 'field.outOfBound', + message: `Decimal values are not allowed`, + resultType: 'error', + }, + ]); + }); }); diff --git a/src/validators/form-validator.ts b/src/validators/form-validator.ts index b962a5ea5..b18bfd56c 100644 --- a/src/validators/form-validator.ts +++ b/src/validators/form-validator.ts @@ -1,5 +1,7 @@ import { type FormFieldValidator, type FormField } from '../types'; +import { moduleName } from '../globals'; import { isTrue } from '../utils/boolean-utils'; +import { translateFrom } from '@openmrs/esm-framework'; export const fieldRequiredErrCode = 'field.required'; export const fieldOutOfBoundErrCode = 'field.outOfBound'; @@ -24,19 +26,21 @@ export const FieldValidator: FormFieldValidator = { const min = Number(field.questionOptions.min); const max = Number(field.questionOptions.max); if (isEmpty(value)) return []; - return !Number.isNaN(min) || !Number.isNaN(max) ? numberInputRangeValidator(min, max, value) : []; + return !Number.isNaN(min) || !Number.isNaN(max) ? numberInputRangeValidator(min, max, value, field) : []; } return []; }, }; -export function numberInputRangeValidator(min: number, max: number, inputValue: number) { +export function numberInputRangeValidator(min: number, max: number, inputValue: number, field?: FormField) { if (!Number.isNaN(min) && inputValue < min) { return [ { - resultType: 'error', + resultType: translateFrom('@openmrs/esm-form-engine-app', 'error', 'error'), errCode: fieldOutOfBoundErrCode, - message: `Value must be greater than ${min}`, + message: translateFrom('@openmrs/esm-form-engine-app', 'minValue', 'Value must be greater than {{min}}', { + min, + }), }, ]; } @@ -44,28 +48,32 @@ export function numberInputRangeValidator(min: number, max: number, inputValue: if (!Number.isNaN(max) && inputValue > max) { return [ { - resultType: 'error', + resultType: translateFrom('@openmrs/esm-form-engine-app', 'error', 'error'), errCode: fieldOutOfBoundErrCode, - message: `Value must be lower than ${max}`, + message: translateFrom('@openmrs/esm-form-engine-app', 'maxValue', 'Value must be lower than {{max}}', { + max, + }), }, ]; } - return []; -} - -export function disallowDecimalsValidator(inputValue: string) { - if (!isEmpty(inputValue) && inputValue.toString().includes('.')) { - return [ - { - resultType: 'error', - errCode: fieldOutOfBoundErrCode, - message: `Decimal values are not allowed`, - }, - ]; - } else { - return []; + if (field.questionOptions.disallowDecimals || field.meta.concept?.allowDecimal) { + if (typeof inputValue === 'number' && !Number.isInteger(inputValue)) { + return [ + { + resultType: translateFrom('@openmrs/esm-form-engine-app', 'error', 'error'), + errCode: fieldOutOfBoundErrCode, + message: translateFrom( + '@openmrs/esm-form-engine-app', + 'decimalValue', + 'Decimal values are not allowed for this field', + ), + }, + ]; + } } + + return []; } export function isEmpty(value: any): boolean { @@ -107,9 +115,9 @@ export function textInputLengthValidator(minLength: string, maxLength: string, i export function addError(errorCode: string, message: string): [{}] { return [ { - resultType: 'error', + resultType: translateFrom('@openmrs/esm-form-engine-app', 'error', 'error'), errCode: errorCode, - message: message, + message: translateFrom('@openmrs/esm-form-engine-app', 'message', message), }, ]; } diff --git a/translations/en.json b/translations/en.json index 13ecb44bc..9a8089fbc 100644 --- a/translations/en.json +++ b/translations/en.json @@ -12,6 +12,8 @@ "closesNotification": "Closes notification", "createdRecord": "Record created", "createdRecordDescription": "A new encounter was created", + "decimalValue": "Decimal values are not allowed ${inputValue}", + "error": "error", "errorDescription": "", "errorDescriptionTitle": "Error on saving form", "errorRenderingField": "Error rendering field", @@ -20,16 +22,20 @@ "errorSavingPatientIndentifiers": "Error saving patient identifiers", "fileUploadDescription": "", "fileUploadDescriptionAny": "Upload any file type", + "identifierCreated": "Identifier Created", + "identifierCreatedDescription": "", "invalidWorkspaceName": "Invalid workspace name.", "invalidWorkspaceNameSubtitle": "Please provide a valid workspace name.", "launchWorkspace": "", "loading": "Loading", + "maxValue": "Value must be greater than ${max}", + "minValue": "Value must be lower than {{min}}", "notification": "Notification", "ordersSaved": "Order(s) saved sucessfully", "patientIdentifiersSaved": "Patient identifier(s) saved sucessfully", "preview": "Preview", "previousValue": "Previous value:", - "remove": "Remove", + "removeGroup": "Remove group", "required": "Required", "revert": "Revert", "save": "Save",