Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date formats #894

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion server/utils/fieldValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ function mapJoiErrors(joiErrors, fieldsConfig) {

module.exports = {
validate(formResponse, pageConfig) {
const localFormResponse = { ...formResponse }
const formSchema = createSchemaFromConfig(pageConfig)
const joiErrors = formSchema.validate(formResponse, { stripUnknown: false, abortEarly: false })

// we want to accept dates with or without leading 0s e.g. 01/01/2024 and 1/1/2024, so this removes any leading zeros before running through the validator
pageConfig.fields.forEach(field => {
const fieldName = getFieldName(field)
const fieldConfigResponseType = getFieldDetail(['responseType'], field)
const [responseType] = fieldConfigResponseType.split('_')
if (['futureDate', 'pastDate', 'indeterminateCheck', 'todayOrPastDate'].includes(responseType)) {
localFormResponse[fieldName] = localFormResponse[fieldName]?.replace(/^0+/, '')?.replace(/\/0/g, '/')
}
})

const joiErrors = formSchema.validate(localFormResponse, { stripUnknown: false, abortEarly: false })
const fieldsConfig = getIn(['fields'], pageConfig)

return mapJoiErrors(joiErrors, fieldsConfig)
Expand Down
26 changes: 26 additions & 0 deletions test/utils/fieldValidation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const overThreeYearsDate = moment().add(3, 'years').add(1, 'days').format('D/M/Y
const validFutureDate = moment().add(12, 'months').format('D/M/YYYY')
const overOneYearsDate = moment().add(12, 'months').add(1, 'days').format('D/M/YYYY')
const todaysDate = moment().format('D/M/YYYY')
const todaysDateAlternativeFormat = moment().format('DD/MM/YYYY')
const pastDate = moment().subtract(1, 'days').format('D/M/YYYY')
const pastDateAlternativeFormat = moment().subtract(1, 'month').startOf('month').format('DD/MM/YYYY')
const invalidDate = '78/13/3043'
const tomorrow = moment().add(1, 'days').format('D/M/YYYY')

Expand All @@ -29,12 +31,22 @@ describe('Validating next review date for indeterminate', () => {
const formResponse = { indeterminate: 'true', date: todaysDate }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([])
})
it('Validation should pass for indeterminate with today using alternative format', () => {
const formResponse = { indeterminate: 'true', date: todaysDateAlternativeFormat }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([])
})
it('Validation should return the correct error message for indeterminate past day', () => {
const formResponse = { indeterminate: 'true', date: pastDate }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([
{ href: '#date', text: 'The review date must be today or in the future' },
])
})
it('Validation should return the correct error message for indeterminate past day when the date has leading 0s', () => {
const formResponse = { indeterminate: 'true', date: pastDateAlternativeFormat }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([
{ href: '#date', text: 'The review date must be today or in the future' },
])
})
it('Validation should return the correct error message for indeterminate with invalid date', () => {
const formResponse = { indeterminate: 'true', date: invalidDate }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([
Expand Down Expand Up @@ -64,12 +76,22 @@ describe('Validating next review date for determinate', () => {
const formResponse = { indeterminate: 'false', date: todaysDate }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([])
})
it('Validation should pass for determinate with today using alternative format', () => {
const formResponse = { indeterminate: 'false', date: todaysDateAlternativeFormat }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([])
})
it('Validation should return the correct error message for determinate past day', () => {
const formResponse = { indeterminate: 'false', date: pastDate }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([
{ href: '#date', text: 'The review date must be today or in the future' },
])
})
it('Validation should return the correct error message for determinate past day', () => {
const formResponse = { indeterminate: 'false', date: pastDateAlternativeFormat }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([
{ href: '#date', text: 'The review date must be today or in the future' },
])
})
it('Validation should return the correct error message for determinate with invalid date', () => {
const formResponse = { indeterminate: 'false', date: invalidDate }
expect(fieldValidation.validate(formResponse, pageConfig.nextReviewDate)).toEqual([
Expand Down Expand Up @@ -104,6 +126,10 @@ describe('Validating oasys review date for today and past date', () => {
const formResponse = { date: todaysDate }
expect(fieldValidation.validate(formResponse, dateConfig)).toEqual([])
})
it('Validation should pass for valid todays date using alternative format', () => {
const formResponse = { date: todaysDateAlternativeFormat }
expect(fieldValidation.validate(formResponse, dateConfig)).toEqual([])
})
it('Validation should pass for valid past date', () => {
const formResponse = { date: pastDate }
expect(fieldValidation.validate(formResponse, dateConfig)).toEqual([])
Expand Down