diff --git a/packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.test.ts b/packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.test.ts index f9c70c9b0..322fe2a9d 100644 --- a/packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.test.ts +++ b/packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.test.ts @@ -1,6 +1,7 @@ import { getConfig } from '@openmrs/esm-framework'; import { type RegistrationConfig } from '../../config-schema'; import { getValidationSchema } from './patient-registration-validation'; +import dayjs from 'dayjs'; const mockGetConfig = jest.mocked(getConfig); @@ -144,7 +145,7 @@ describe('Patient registration validation', () => { expect(validationError).toBeFalsy(); }); - it('should throw error when date of birth is a future date', async () => { + it('should throw an error when date of birth is a future date', async () => { const invalidFormValues = { ...validFormValues, birthdate: new Date('2100-01-01'), @@ -153,6 +154,15 @@ describe('Patient registration validation', () => { expect(validationError.errors).toContain('birthdayNotInTheFuture'); }); + it('should throw an error when date of birth is more than 140 years ago', async () => { + const invalidFormValues = { + ...validFormValues, + birthdate: dayjs().subtract(141, 'years').toDate(), + }; + const validationError = await validateFormValues(invalidFormValues); + expect(validationError.errors).toContain('birthdayNotOver140YearsAgo'); + }); + it('should require yearsEstimated when birthdateEstimated is true', async () => { const invalidFormValues = { ...validFormValues, @@ -162,7 +172,7 @@ describe('Patient registration validation', () => { expect(validationError.errors).toContain('yearsEstimateRequired'); }); - it('should throw error when monthEstimated is negative', async () => { + it('should throw an error when monthEstimated is negative', async () => { const invalidFormValues = { ...validFormValues, birthdateEstimated: true, @@ -173,7 +183,17 @@ describe('Patient registration validation', () => { expect(validationError.errors).toContain('negativeMonths'); }); - it('should throw error when deathDate is in future', async () => { + it('should throw an error when yearsEstimated is more than 140', async () => { + const invalidFormValues = { + ...validFormValues, + birthdateEstimated: true, + yearsEstimated: 141, + }; + const validationError = await validateFormValues(invalidFormValues); + expect(validationError.errors).toContain('nonsensicalYears'); + }); + + it('should throw an error when deathDate is in future', async () => { const invalidFormValues = { ...validFormValues, deathDate: new Date('2100-01-01'), diff --git a/packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.ts b/packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.ts index ebb5fc835..c4a1b1c5e 100644 --- a/packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.ts +++ b/packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.ts @@ -33,6 +33,10 @@ export function getValidationSchema(config: RegistrationConfig) { then: Yup.date() .required(t('birthdayRequired', 'Birthday is required')) .max(Date(), t('birthdayNotInTheFuture', 'Birthday cannot be in future')) + .min( + dayjs().subtract(140, 'years').toDate(), + t('birthdayNotOver140YearsAgo', 'Birthday cannot be more than 140 years ago'), + ) .nullable(), otherwise: Yup.date().nullable(), }), @@ -40,7 +44,8 @@ export function getValidationSchema(config: RegistrationConfig) { is: true, then: Yup.number() .required(t('yearsEstimateRequired', 'Estimated years required')) - .min(0, t('negativeYears', 'Estimated years cannot be negative')), + .min(0, t('negativeYears', 'Estimated years cannot be negative')) + .max(140, t('nonsensicalYears', 'Estimated years cannot be more than 140')), otherwise: Yup.number().nullable(), }), monthsEstimated: Yup.number().min(0, t('negativeMonths', 'Estimated months cannot be negative')), diff --git a/packages/esm-patient-registration-app/translations/en.json b/packages/esm-patient-registration-app/translations/en.json index 34e551538..ff2890d24 100644 --- a/packages/esm-patient-registration-app/translations/en.json +++ b/packages/esm-patient-registration-app/translations/en.json @@ -4,6 +4,7 @@ "allFieldsRequiredText": "All fields are required unless marked optional", "autoGeneratedPlaceholderText": "Auto-generated", "birthdayNotInTheFuture": "Birthday cannot be in future", + "birthdayNotOver140YearsAgo": "Birthday cannot be more than 140 years ago", "birthdayRequired": "Birthday is required", "birthFieldLabelText": "Birth", "cancel": "Cancel", @@ -72,6 +73,7 @@ "no": "No", "nonCodedCauseOfDeath": "Non-coded cause of death", "nonCodedCauseOfDeathRequired": "Cause of death is required", + "nonsensicalYears": "Estimated years cannot be more than 140", "numberInNameDubious": "Number in name is dubious", "obsFieldUnknownDatatype": "Concept for obs field '{{fieldDefinitionId}}' has unknown datatype '{{datatypeName}}'", "optional": "optional",