Skip to content

Commit

Permalink
Fix DOB validation (cannot be today)
Browse files Browse the repository at this point in the history
Prior to this change, while registering a birth event, applicant could input the date of application creation as the birth date of mother and/or father.

This change will enforce the applicant to use a date from the past for the DOB of mother and father.
Additionally, this validation is also added to applicant's DOB while registering a death event.

Fixes OCRVS-1453
  • Loading branch information
maacpiash committed May 22, 2019
1 parent 99c6e9f commit 2fdfc1e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
dateNotInFuture,
dateFormatIsCorrect,
maxLength,
numeric
numeric,
dateInPast
} from 'src/utils/validate'

export interface IFatherSectionFormData {
Expand Down Expand Up @@ -403,7 +404,7 @@ export const fatherSection: IFormSection = {
dependencies: []
},
{
validator: dateNotInFuture,
validator: dateInPast,
dependencies: []
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
dateNotInFuture,
dateFormatIsCorrect,
numeric,
maxLength
maxLength,
dateInPast
} from 'src/utils/validate'

export interface IMotherSectionFormData {
Expand Down Expand Up @@ -360,7 +361,7 @@ export const motherSection: IFormSection = {
dependencies: []
},
{
validator: dateNotInFuture,
validator: dateInPast,
dependencies: []
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
isValidBirthDate,
validIDNumber,
numeric,
maxLength
maxLength,
isDateInPast
} from 'src/utils/validate'
import { countries } from 'src/forms/countries'
import {
Expand Down Expand Up @@ -446,7 +447,7 @@ export const applicantsSection: IFormSection = {
label: messages.applicantsDateOfBirth,
required: false,
initialValue: '',
validate: [isValidBirthDate],
validate: [isValidBirthDate, isDateInPast],
mapping: {
mutation: fieldValueNestingTransformer(
NESTED_SECTION,
Expand Down
29 changes: 28 additions & 1 deletion packages/register/src/utils/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
dateGreaterThan,
dateLessThan,
dateNotInFuture,
dateFormatIsCorrect
dateFormatIsCorrect,
dateInPast
} from './validate'

describe('validate', () => {
Expand Down Expand Up @@ -427,6 +428,32 @@ describe('validate', () => {
})
})

describe('dateInPast. Checks if a given birth date is in the past', () => {
it('should not give an error message if the birth date is in the past', () => {
const todaysDate = new Date('1999-12-31')
todaysDate.setHours(0, 0, 0)
const today = todaysDate.toDateString()
expect(dateInPast()(today)).toEqual(undefined)
})
it("should give an error message if the date is today's date", () => {
const todaysDate = new Date()
todaysDate.setHours(0, 0, 0)
const today = todaysDate.toDateString()
expect(dateInPast()(today)).toEqual({
message: messages.isValidBirthDate
})
})

it('should give an error message if the date is in the future', () => {
const todaysDate = new Date(2040, 12, 12)
todaysDate.setHours(0, 0, 0)
const today = todaysDate.toDateString()
expect(dateInPast()(today)).toEqual({
message: messages.isValidBirthDate
})
})
})

describe('bengaliOnlyNameFormat. Checks a value is a valid Bengali name', () => {
it('should error when a Bengali punctuation is given', () => {
const badValue = 'মাসুম।'
Expand Down
18 changes: 18 additions & 0 deletions packages/register/src/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,24 @@ export const dateNotInFuture: ValidationInitializer = (): Validation => (
}
}

export const dateInPast: ValidationInitializer = (): Validation => (
value: string
) => isDateInPast(value)

export const isDateInPast: Validation = (value: string) => {
if (isDateNotInFuture(value) && dateNotToday(value)) {
return undefined
} else {
return { message: messages.isValidBirthDate } // specific to DOB of parent/applicant
}
}

export const dateNotToday = (date: string): boolean => {
const today = new Date().setHours(0, 0, 0, 0)
const day = new Date(date).setHours(0, 0, 0, 0)
return day !== today
}

export const dateFormatIsCorrect: ValidationInitializer = (): Validation => (
value: string
) => dateFormat(value)
Expand Down

0 comments on commit 2fdfc1e

Please sign in to comment.