Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jabahum committed May 10, 2024
1 parent ac30996 commit 15a45a9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
24 changes: 24 additions & 0 deletions src/validators/form-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
},
]);
});
});
52 changes: 30 additions & 22 deletions src/validators/form-validator.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,48 +26,54 @@ 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,
}),
},
];
}

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 {
Expand Down Expand Up @@ -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),
},
];
}
8 changes: 7 additions & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 15a45a9

Please sign in to comment.