Skip to content
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
},
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@defra/forms-model": "^3.0.676",
"@defra/forms-model": "^3.0.678",
"@defra/hapi-tracing": "^1.29.0",
"@defra/interactive-map": "^0.0.22-alpha",
"@elastic/ecs-pino-format": "^1.5.0",
Expand Down
65 changes: 64 additions & 1 deletion src/server/plugins/engine/components/MonthYearField.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentType, type MonthYearFieldComponent } from '@defra/forms-model'
import { startOfDay } from 'date-fns'
import { addMonths, format, startOfDay, startOfMonth } from 'date-fns'

import { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'
import {
Expand Down Expand Up @@ -401,6 +401,13 @@ describe('MonthYearField', () => {

describe('Validation', () => {
const date = new Date('2001-01-01')
const today = startOfDay(new Date())
const thisMonth = startOfMonth(today)

const OneMonthInPast = addMonths(thisMonth, -1)
const TwoMonthsInPast = addMonths(thisMonth, -2)
const OneMonthInFuture = addMonths(thisMonth, 1)
const TwoMonthsInFuture = addMonths(thisMonth, 2)

describe.each([
{
Expand Down Expand Up @@ -517,6 +524,62 @@ describe('MonthYearField', () => {
}
]
},
{
description: 'Earliest month/year option',
component: {
title: 'Example month/year field',
name: 'myComponent',
type: ComponentType.MonthYearField,
options: {
earliestMonthYear: format(OneMonthInPast, 'yyyy-MM')
}
} satisfies MonthYearFieldComponent,
assertions: [
{
input: getFormData(TwoMonthsInPast),
output: {
value: getFormData(TwoMonthsInPast),
errors: [
expect.objectContaining({
text: `Example month/year field must be the same as or after ${format(OneMonthInPast, 'd MMMM yyyy')}`
})
]
}
},
{
input: getFormData(today),
output: { value: getFormData(today) }
}
]
},
{
description: 'Latest month/year option',
component: {
title: 'Example month/year field',
name: 'myComponent',
type: ComponentType.MonthYearField,
options: {
latestMonthYear: format(OneMonthInFuture, 'yyyy-MM')
}
} satisfies MonthYearFieldComponent,
assertions: [
{
input: getFormData(TwoMonthsInFuture),
output: {
value: getFormData(TwoMonthsInFuture),
errors: [
expect.objectContaining({
text: `Example month/year field must be the same as or before ${format(OneMonthInFuture, 'd MMMM yyyy')}`
})
]
}
},
{
input: getFormData(today),
output: { value: getFormData(today) }
}
]
},
{
description: 'Optional fields',
component: {
Expand Down
30 changes: 29 additions & 1 deletion src/server/plugins/engine/components/MonthYearField.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentType, type MonthYearFieldComponent } from '@defra/forms-model'
import { format, isValid } from 'date-fns'
import { format, isValid, parse } from 'date-fns'
import {
type Context,
type CustomValidator,
Expand Down Expand Up @@ -259,6 +259,34 @@ export function getValidatorMonthYear(component: MonthYearField) {
: payload
}

const date = parse(
`${values.year}-${values.month}-01`,
'yyyy-MM-dd',
new Date()
)

if (!isValid(date)) {
return helpers.error('date.format', context)
}

// Minimum date from today
const earliestDate = options.earliestMonthYear
? new Date(`${options.earliestMonthYear}-01`)
: undefined

if (earliestDate && date < earliestDate) {
return helpers.error('date.min', { ...context, limit: earliestDate })
}

// Maximum date from today
const latestDate = options.latestMonthYear
? new Date(`${options.latestMonthYear}-01`)
: undefined

if (latestDate && date > latestDate) {
return helpers.error('date.max', { ...context, limit: latestDate })
}

return payload
}

Expand Down
Loading