@defra/forms-engine-plugin 4.0.2 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.server/index.js CHANGED
@@ -6,7 +6,7 @@ const logger = createLogger();
6
6
  process.on('unhandledRejection', error => {
7
7
  const err = getErrorMessage(error);
8
8
  logger.info('Unhandled rejection');
9
- logger.error(err, `[unhandledRejection] Unhandled promise rejection: ${err}`);
9
+ logger.error(error, `[unhandledRejection] Unhandled promise rejection: ${err}`);
10
10
  throw error;
11
11
  });
12
12
  const port = config.get('port');
@@ -27,7 +27,7 @@ async function startServer() {
27
27
  startServer().catch(error => {
28
28
  const err = getErrorMessage(error);
29
29
  logger.info('Server failed to start :(');
30
- logger.error(err, `[serverStartup] Server failed to start: ${err}`);
30
+ logger.error(error, `[serverStartup] Server failed to start: ${err}`);
31
31
  throw error;
32
32
  });
33
33
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["getErrorMessage","config","createLogger","createServer","logger","process","on","error","err","info","port","get","ordnanceSurveyApiKey","startServer","server","start","send","catch"],"sources":["../src/index.ts"],"sourcesContent":["import { getErrorMessage } from '@defra/forms-model'\n\nimport { config } from '~/src/config/index.js'\nimport { createLogger } from '~/src/server/common/helpers/logging/logger.js'\nimport { createServer } from '~/src/server/index.js'\n\nconst logger = createLogger()\n\nprocess.on('unhandledRejection', (error) => {\n const err = getErrorMessage(error)\n logger.info('Unhandled rejection')\n logger.error(err, `[unhandledRejection] Unhandled promise rejection: ${err}`)\n throw error\n})\n\nconst port = config.get('port')\nconst ordnanceSurveyApiKey = config.get('ordnanceSurveyApiKey')\n\n/**\n * Main entrypoint to the application.\n */\nasync function startServer() {\n const server = await createServer({ ordnanceSurveyApiKey })\n await server.start()\n\n process.send?.('online')\n\n server.logger.info('Server started successfully')\n server.logger.info(`Access your frontend on http://localhost:${port}`)\n}\n\nstartServer().catch((error: unknown) => {\n const err = getErrorMessage(error)\n logger.info('Server failed to start :(')\n logger.error(err, `[serverStartup] Server failed to start: ${err}`)\n throw error\n})\n"],"mappings":"AAAA,SAASA,eAAe,QAAQ,oBAAoB;AAEpD,SAASC,MAAM;AACf,SAASC,YAAY;AACrB,SAASC,YAAY;AAErB,MAAMC,MAAM,GAAGF,YAAY,CAAC,CAAC;AAE7BG,OAAO,CAACC,EAAE,CAAC,oBAAoB,EAAGC,KAAK,IAAK;EAC1C,MAAMC,GAAG,GAAGR,eAAe,CAACO,KAAK,CAAC;EAClCH,MAAM,CAACK,IAAI,CAAC,qBAAqB,CAAC;EAClCL,MAAM,CAACG,KAAK,CAACC,GAAG,EAAE,qDAAqDA,GAAG,EAAE,CAAC;EAC7E,MAAMD,KAAK;AACb,CAAC,CAAC;AAEF,MAAMG,IAAI,GAAGT,MAAM,CAACU,GAAG,CAAC,MAAM,CAAC;AAC/B,MAAMC,oBAAoB,GAAGX,MAAM,CAACU,GAAG,CAAC,sBAAsB,CAAC;;AAE/D;AACA;AACA;AACA,eAAeE,WAAWA,CAAA,EAAG;EAC3B,MAAMC,MAAM,GAAG,MAAMX,YAAY,CAAC;IAAES;EAAqB,CAAC,CAAC;EAC3D,MAAME,MAAM,CAACC,KAAK,CAAC,CAAC;EAEpBV,OAAO,CAACW,IAAI,GAAG,QAAQ,CAAC;EAExBF,MAAM,CAACV,MAAM,CAACK,IAAI,CAAC,6BAA6B,CAAC;EACjDK,MAAM,CAACV,MAAM,CAACK,IAAI,CAAC,4CAA4CC,IAAI,EAAE,CAAC;AACxE;AAEAG,WAAW,CAAC,CAAC,CAACI,KAAK,CAAEV,KAAc,IAAK;EACtC,MAAMC,GAAG,GAAGR,eAAe,CAACO,KAAK,CAAC;EAClCH,MAAM,CAACK,IAAI,CAAC,2BAA2B,CAAC;EACxCL,MAAM,CAACG,KAAK,CAACC,GAAG,EAAE,2CAA2CA,GAAG,EAAE,CAAC;EACnE,MAAMD,KAAK;AACb,CAAC,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["getErrorMessage","config","createLogger","createServer","logger","process","on","error","err","info","port","get","ordnanceSurveyApiKey","startServer","server","start","send","catch"],"sources":["../src/index.ts"],"sourcesContent":["import { getErrorMessage } from '@defra/forms-model'\n\nimport { config } from '~/src/config/index.js'\nimport { createLogger } from '~/src/server/common/helpers/logging/logger.js'\nimport { createServer } from '~/src/server/index.js'\n\nconst logger = createLogger()\n\nprocess.on('unhandledRejection', (error) => {\n const err = getErrorMessage(error)\n logger.info('Unhandled rejection')\n logger.error(\n error,\n `[unhandledRejection] Unhandled promise rejection: ${err}`\n )\n throw error\n})\n\nconst port = config.get('port')\nconst ordnanceSurveyApiKey = config.get('ordnanceSurveyApiKey')\n\n/**\n * Main entrypoint to the application.\n */\nasync function startServer() {\n const server = await createServer({ ordnanceSurveyApiKey })\n await server.start()\n\n process.send?.('online')\n\n server.logger.info('Server started successfully')\n server.logger.info(`Access your frontend on http://localhost:${port}`)\n}\n\nstartServer().catch((error: unknown) => {\n const err = getErrorMessage(error)\n logger.info('Server failed to start :(')\n logger.error(error, `[serverStartup] Server failed to start: ${err}`)\n throw error\n})\n"],"mappings":"AAAA,SAASA,eAAe,QAAQ,oBAAoB;AAEpD,SAASC,MAAM;AACf,SAASC,YAAY;AACrB,SAASC,YAAY;AAErB,MAAMC,MAAM,GAAGF,YAAY,CAAC,CAAC;AAE7BG,OAAO,CAACC,EAAE,CAAC,oBAAoB,EAAGC,KAAK,IAAK;EAC1C,MAAMC,GAAG,GAAGR,eAAe,CAACO,KAAK,CAAC;EAClCH,MAAM,CAACK,IAAI,CAAC,qBAAqB,CAAC;EAClCL,MAAM,CAACG,KAAK,CACVA,KAAK,EACL,qDAAqDC,GAAG,EAC1D,CAAC;EACD,MAAMD,KAAK;AACb,CAAC,CAAC;AAEF,MAAMG,IAAI,GAAGT,MAAM,CAACU,GAAG,CAAC,MAAM,CAAC;AAC/B,MAAMC,oBAAoB,GAAGX,MAAM,CAACU,GAAG,CAAC,sBAAsB,CAAC;;AAE/D;AACA;AACA;AACA,eAAeE,WAAWA,CAAA,EAAG;EAC3B,MAAMC,MAAM,GAAG,MAAMX,YAAY,CAAC;IAAES;EAAqB,CAAC,CAAC;EAC3D,MAAME,MAAM,CAACC,KAAK,CAAC,CAAC;EAEpBV,OAAO,CAACW,IAAI,GAAG,QAAQ,CAAC;EAExBF,MAAM,CAACV,MAAM,CAACK,IAAI,CAAC,6BAA6B,CAAC;EACjDK,MAAM,CAACV,MAAM,CAACK,IAAI,CAAC,4CAA4CC,IAAI,EAAE,CAAC;AACxE;AAEAG,WAAW,CAAC,CAAC,CAACI,KAAK,CAAEV,KAAc,IAAK;EACtC,MAAMC,GAAG,GAAGR,eAAe,CAACO,KAAK,CAAC;EAClCH,MAAM,CAACK,IAAI,CAAC,2BAA2B,CAAC;EACxCL,MAAM,CAACG,KAAK,CAACA,KAAK,EAAE,2CAA2CC,GAAG,EAAE,CAAC;EACrE,MAAMD,KAAK;AACb,CAAC,CAAC","ignoreList":[]}
@@ -120,15 +120,14 @@
120
120
  "content": "### This is a H3 in markdown\n\n[An internal link](http://localhost:3009/fictional-page)\n\n[An external link](https://defra.gov.uk/fictional-page)",
121
121
  "options": {},
122
122
  "schema": {}
123
- },
124
- {
125
- "title": "Summary",
126
- "path": "/summary",
127
- "controller": "SummaryPageController",
128
- "components": [],
129
- "next": []
130
123
  }
131
124
  ]
125
+ },
126
+ {
127
+ "title": "Summary",
128
+ "path": "/summary",
129
+ "controller": "SummaryPageController",
130
+ "next": []
132
131
  }
133
132
  ],
134
133
  "sections": [
@@ -229,12 +229,12 @@ export function getValidatorDate(component) {
229
229
  }
230
230
 
231
231
  // Minimum date from today
232
- const dateMin = options.maxDaysInPast ? sub(startOfToday(), {
232
+ const dateMin = options.maxDaysInPast || options.maxDaysInPast === 0 ? sub(startOfToday(), {
233
233
  days: options.maxDaysInPast
234
234
  }) : undefined;
235
235
 
236
236
  // Maximum date from today
237
- const dateMax = options.maxDaysInFuture ? add(startOfToday(), {
237
+ const dateMax = options.maxDaysInFuture || options.maxDaysInFuture === 0 ? add(startOfToday(), {
238
238
  days: options.maxDaysInFuture
239
239
  }) : undefined;
240
240
  if (dateMin && date < dateMin) {
@@ -1 +1 @@
1
- {"version":3,"file":"DatePartsField.js","names":["ComponentType","add","format","isValid","parse","startOfToday","sub","ComponentCollection","FormComponent","isFormState","isFormValue","NumberField","parseStrictDate","messageTemplate","convertToLanguageMessages","DatePartsField","constructor","def","props","name","options","isRequired","required","customValidationMessages","objectMissing","dateFormat","collection","type","title","schema","min","max","precision","optionalText","classes","parent","custom","getValidatorDate","peers","formSchema","stateSchema","getFormValueFromState","state","value","isState","undefined","getDisplayStringFromFormValue","formValue","year","month","day","getDisplayStringFromState","getContextValueFromFormValue","Date","getContextValueFromState","getViewModel","payload","errors","viewModel","fieldset","label","hasError","some","error","items","map","model","errorMessage","toString","text","trim","id","legend","isDateParts","getAllPossibleErrors","baseErrors","template","advancedSettingsErrors","dateMin","dateMax","isNumber","component","validator","helpers","values","getStateFromValidForm","context","missing","keys","key","date","maxDaysInPast","days","maxDaysInFuture","limit"],"sources":["../../../../../src/server/plugins/engine/components/DatePartsField.ts"],"sourcesContent":["import { ComponentType, type DatePartsFieldComponent } from '@defra/forms-model'\nimport { add, format, isValid, parse, startOfToday, sub } from 'date-fns'\nimport { type Context, type CustomValidator, type ObjectSchema } from 'joi'\n\nimport { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'\nimport {\n FormComponent,\n isFormState,\n isFormValue\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { NumberField } from '~/src/server/plugins/engine/components/NumberField.js'\nimport {\n type DateInputItem,\n type DatePartsState\n} from '~/src/server/plugins/engine/components/types.js'\nimport { parseStrictDate } from '~/src/server/plugins/engine/date-helper.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type ErrorMessageTemplateList,\n type FormPayload,\n type FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState\n} from '~/src/server/plugins/engine/types.js'\nimport { convertToLanguageMessages } from '~/src/server/utils/type-utils.js'\n\nexport class DatePartsField extends FormComponent {\n declare options: DatePartsFieldComponent['options']\n declare formSchema: ObjectSchema<FormPayload>\n declare stateSchema: ObjectSchema<FormState>\n declare collection: ComponentCollection\n\n constructor(\n def: DatePartsFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { name, options } = def\n\n const isRequired = options.required !== false\n\n const customValidationMessages = convertToLanguageMessages({\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n 'any.required': messageTemplate.objectMissing,\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n 'number.base': messageTemplate.objectMissing,\n 'number.precision': messageTemplate.dateFormat,\n 'number.integer': messageTemplate.dateFormat,\n 'number.unsafe': messageTemplate.dateFormat,\n 'number.min': messageTemplate.dateFormat,\n 'number.max': messageTemplate.dateFormat\n })\n\n this.collection = new ComponentCollection(\n [\n {\n type: ComponentType.NumberField,\n name: `${name}__day`,\n title: 'Day',\n schema: { min: 1, max: 31, precision: 0 },\n options: {\n required: isRequired,\n optionalText: true,\n classes: 'govuk-input--width-2',\n customValidationMessages\n }\n },\n {\n type: ComponentType.NumberField,\n name: `${name}__month`,\n title: 'Month',\n schema: { min: 1, max: 12, precision: 0 },\n options: {\n required: isRequired,\n optionalText: true,\n classes: 'govuk-input--width-2',\n customValidationMessages\n }\n },\n {\n type: ComponentType.NumberField,\n name: `${name}__year`,\n title: 'Year',\n schema: { min: 1000, max: 3000, precision: 0 },\n options: {\n required: isRequired,\n optionalText: true,\n classes: 'govuk-input--width-4',\n customValidationMessages\n }\n }\n ],\n { ...props, parent: this },\n {\n custom: getValidatorDate(this),\n peers: [`${name}__day`, `${name}__month`, `${name}__year`]\n }\n )\n\n this.options = options\n this.formSchema = this.collection.formSchema\n this.stateSchema = this.collection.stateSchema\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const value = super.getFormValueFromState(state)\n return this.isState(value) ? value : undefined\n }\n\n getDisplayStringFromFormValue(formValue: DatePartsState | undefined) {\n if (!formValue) {\n return ''\n }\n\n return format(\n `${formValue.year}-${formValue.month}-${formValue.day}`,\n 'd MMMM yyyy'\n )\n }\n\n getDisplayStringFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n return this.getDisplayStringFromFormValue(value)\n }\n\n getContextValueFromFormValue(value: DatePartsState | undefined) {\n if (\n !value ||\n !isValid(\n parseStrictDate(\n value,\n `${value.year}-${value.month}-${value.day}`,\n 'yyyy-MM-dd',\n new Date()\n )\n )\n ) {\n return null\n }\n\n return format(`${value.year}-${value.month}-${value.day}`, 'yyyy-MM-dd')\n }\n\n getContextValueFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n return this.getContextValueFromFormValue(value)\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { collection, name } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { fieldset, label } = viewModel\n\n // Check for component errors only\n const hasError = errors?.some((error) => error.name === name)\n\n // Use the component collection to generate the subitems\n const items: DateInputItem[] = collection\n .getViewModel(payload, errors)\n .map(({ model }) => {\n let { label, type, value, classes, errorMessage } = model\n\n if (label) {\n label.toString = () => label.text // Date component uses string labels\n }\n\n if (hasError || errorMessage) {\n classes = `${classes} govuk-input--error`.trim()\n }\n\n // Allow any `toString()`-able value so non-numeric\n // values are shown alongside their error messages\n if (!isFormValue(value)) {\n value = undefined\n }\n\n return {\n label,\n id: model.id,\n name: model.name,\n type,\n value,\n classes\n }\n })\n\n fieldset ??= {\n legend: {\n text: label.text,\n classes: 'govuk-fieldset__legend--m'\n }\n }\n\n return {\n ...viewModel,\n fieldset,\n items\n }\n }\n\n isState(value?: FormStateValue | FormState) {\n return DatePartsField.isDateParts(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return DatePartsField.getAllPossibleErrors()\n }\n\n /**\n * Static version of getAllPossibleErrors that doesn't require a component instance.\n */\n static getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [\n { type: 'required', template: messageTemplate.required },\n { type: 'dateFormat', template: messageTemplate.dateFormat },\n { type: 'dateFormatDay', template: '{{#label}} must include a day' },\n {\n type: 'dateFormatMonth',\n template: '{{#label}} must include a month'\n },\n { type: 'dateFormatYear', template: '{{#label}} must include a year' }\n ],\n advancedSettingsErrors: [\n { type: 'dateMin', template: messageTemplate.dateMin },\n { type: 'dateMax', template: messageTemplate.dateMax }\n ]\n }\n }\n\n static isDateParts(\n value?: FormStateValue | FormState\n ): value is DatePartsState {\n return (\n isFormState(value) &&\n NumberField.isNumber(value.day) &&\n NumberField.isNumber(value.month) &&\n NumberField.isNumber(value.year)\n )\n }\n}\n\nexport function getValidatorDate(component: DatePartsField) {\n const validator: CustomValidator = (payload: FormPayload, helpers) => {\n const { collection, name, options } = component\n\n const values = component.getFormValueFromState(\n component.getStateFromValidForm(payload)\n )\n\n const context: Context = {\n missing: collection.keys,\n key: name\n }\n\n if (!component.isState(values)) {\n return options.required !== false\n ? helpers.error('object.required', context)\n : payload\n }\n\n const date = parse(\n `${values.year}-${values.month}-${values.day}`,\n 'yyyy-MM-dd',\n new Date()\n )\n\n if (!isValid(date)) {\n return helpers.error('date.format', context)\n }\n\n // Minimum date from today\n const dateMin = options.maxDaysInPast\n ? sub(startOfToday(), { days: options.maxDaysInPast })\n : undefined\n\n // Maximum date from today\n const dateMax = options.maxDaysInFuture\n ? add(startOfToday(), { days: options.maxDaysInFuture })\n : undefined\n\n if (dateMin && date < dateMin) {\n return helpers.error('date.min', { ...context, limit: dateMin })\n }\n\n if (dateMax && date > dateMax) {\n return helpers.error('date.max', { ...context, limit: dateMax })\n }\n\n return payload\n }\n\n return validator\n}\n"],"mappings":"AAAA,SAASA,aAAa,QAAsC,oBAAoB;AAChF,SAASC,GAAG,EAAEC,MAAM,EAAEC,OAAO,EAAEC,KAA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Type","add","format","isValid","parse","startOfToday","sub","ComponentCollection","FormComponent","isFormState","isFormValue","NumberField","parseStrictDate","messageTemplate","convertToLanguageMessages","DatePartsField","constructor","def","props","name","options","isRequired","required","customValidationMessages","objectMissing","dateFormat","collection","type","title","schema","min","max","precision","optionalText","classes","parent","custom","getValidatorDate","peers","formSchema","stateSchema","getFormValueFromState","state","value","isState","undefined","getDisplayStringFromFormValue","formValue","year","month","day","getDisplayStringFromState","getContextValueFromFormValue","Date","getContextValueFromState","getViewModel","payload","errors","viewModel","fieldset","label","hasError","some","error","items","map","model","errorMessage","toString","text","trim","id","legend","isDateParts","getAllPossibleErrors","baseErrors","template","advancedSettingsErrors","dateMin","dateMax","isNumber","component","validator","helpers","values","getStateFromValidForm","context","missing","keys","key","date","maxDaysInPast","days","maxDaysInFuture","limit"],"sources":["../../../../../src/server/plugins/engine/components/DatePartsField.ts"],"sourcesContent":["import { ComponentType, type DatePartsFieldComponent } from '@defra/forms-model'\nimport { add, format, isValid, parse, startOfToday, sub } from 'date-fns'\nimport { type Context, type CustomValidator, type ObjectSchema } from 'joi'\n\nimport { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'\nimport {\n FormComponent,\n isFormState,\n isFormValue\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { NumberField } from '~/src/server/plugins/engine/components/NumberField.js'\nimport {\n type DateInputItem,\n type DatePartsState\n} from '~/src/server/plugins/engine/components/types.js'\nimport { parseStrictDate } from '~/src/server/plugins/engine/date-helper.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type ErrorMessageTemplateList,\n type FormPayload,\n type FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState\n} from '~/src/server/plugins/engine/types.js'\nimport { convertToLanguageMessages } from '~/src/server/utils/type-utils.js'\n\nexport class DatePartsField extends FormComponent {\n declare options: DatePartsFieldComponent['options']\n declare formSchema: ObjectSchema<FormPayload>\n declare stateSchema: ObjectSchema<FormState>\n declare collection: ComponentCollection\n\n constructor(\n def: DatePartsFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { name, options } = def\n\n const isRequired = options.required !== false\n\n const customValidationMessages = convertToLanguageMessages({\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n 'any.required': messageTemplate.objectMissing,\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n 'number.base': messageTemplate.objectMissing,\n 'number.precision': messageTemplate.dateFormat,\n 'number.integer': messageTemplate.dateFormat,\n 'number.unsafe': messageTemplate.dateFormat,\n 'number.min': messageTemplate.dateFormat,\n 'number.max': messageTemplate.dateFormat\n })\n\n this.collection = new ComponentCollection(\n [\n {\n type: ComponentType.NumberField,\n name: `${name}__day`,\n title: 'Day',\n schema: { min: 1, max: 31, precision: 0 },\n options: {\n required: isRequired,\n optionalText: true,\n classes: 'govuk-input--width-2',\n customValidationMessages\n }\n },\n {\n type: ComponentType.NumberField,\n name: `${name}__month`,\n title: 'Month',\n schema: { min: 1, max: 12, precision: 0 },\n options: {\n required: isRequired,\n optionalText: true,\n classes: 'govuk-input--width-2',\n customValidationMessages\n }\n },\n {\n type: ComponentType.NumberField,\n name: `${name}__year`,\n title: 'Year',\n schema: { min: 1000, max: 3000, precision: 0 },\n options: {\n required: isRequired,\n optionalText: true,\n classes: 'govuk-input--width-4',\n customValidationMessages\n }\n }\n ],\n { ...props, parent: this },\n {\n custom: getValidatorDate(this),\n peers: [`${name}__day`, `${name}__month`, `${name}__year`]\n }\n )\n\n this.options = options\n this.formSchema = this.collection.formSchema\n this.stateSchema = this.collection.stateSchema\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const value = super.getFormValueFromState(state)\n return this.isState(value) ? value : undefined\n }\n\n getDisplayStringFromFormValue(formValue: DatePartsState | undefined) {\n if (!formValue) {\n return ''\n }\n\n return format(\n `${formValue.year}-${formValue.month}-${formValue.day}`,\n 'd MMMM yyyy'\n )\n }\n\n getDisplayStringFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n return this.getDisplayStringFromFormValue(value)\n }\n\n getContextValueFromFormValue(value: DatePartsState | undefined) {\n if (\n !value ||\n !isValid(\n parseStrictDate(\n value,\n `${value.year}-${value.month}-${value.day}`,\n 'yyyy-MM-dd',\n new Date()\n )\n )\n ) {\n return null\n }\n\n return format(`${value.year}-${value.month}-${value.day}`, 'yyyy-MM-dd')\n }\n\n getContextValueFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n return this.getContextValueFromFormValue(value)\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { collection, name } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { fieldset, label } = viewModel\n\n // Check for component errors only\n const hasError = errors?.some((error) => error.name === name)\n\n // Use the component collection to generate the subitems\n const items: DateInputItem[] = collection\n .getViewModel(payload, errors)\n .map(({ model }) => {\n let { label, type, value, classes, errorMessage } = model\n\n if (label) {\n label.toString = () => label.text // Date component uses string labels\n }\n\n if (hasError || errorMessage) {\n classes = `${classes} govuk-input--error`.trim()\n }\n\n // Allow any `toString()`-able value so non-numeric\n // values are shown alongside their error messages\n if (!isFormValue(value)) {\n value = undefined\n }\n\n return {\n label,\n id: model.id,\n name: model.name,\n type,\n value,\n classes\n }\n })\n\n fieldset ??= {\n legend: {\n text: label.text,\n classes: 'govuk-fieldset__legend--m'\n }\n }\n\n return {\n ...viewModel,\n fieldset,\n items\n }\n }\n\n isState(value?: FormStateValue | FormState) {\n return DatePartsField.isDateParts(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return DatePartsField.getAllPossibleErrors()\n }\n\n /**\n * Static version of getAllPossibleErrors that doesn't require a component instance.\n */\n static getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [\n { type: 'required', template: messageTemplate.required },\n { type: 'dateFormat', template: messageTemplate.dateFormat },\n { type: 'dateFormatDay', template: '{{#label}} must include a day' },\n {\n type: 'dateFormatMonth',\n template: '{{#label}} must include a month'\n },\n { type: 'dateFormatYear', template: '{{#label}} must include a year' }\n ],\n advancedSettingsErrors: [\n { type: 'dateMin', template: messageTemplate.dateMin },\n { type: 'dateMax', template: messageTemplate.dateMax }\n ]\n }\n }\n\n static isDateParts(\n value?: FormStateValue | FormState\n ): value is DatePartsState {\n return (\n isFormState(value) &&\n NumberField.isNumber(value.day) &&\n NumberField.isNumber(value.month) &&\n NumberField.isNumber(value.year)\n )\n }\n}\n\nexport function getValidatorDate(component: DatePartsField) {\n const validator: CustomValidator = (payload: FormPayload, helpers) => {\n const { collection, name, options } = component\n\n const values = component.getFormValueFromState(\n component.getStateFromValidForm(payload)\n )\n\n const context: Context = {\n missing: collection.keys,\n key: name\n }\n\n if (!component.isState(values)) {\n return options.required !== false\n ? helpers.error('object.required', context)\n : payload\n }\n\n const date = parse(\n `${values.year}-${values.month}-${values.day}`,\n 'yyyy-MM-dd',\n new Date()\n )\n\n if (!isValid(date)) {\n return helpers.error('date.format', context)\n }\n\n // Minimum date from today\n const dateMin =\n options.maxDaysInPast || options.maxDaysInPast === 0\n ? sub(startOfToday(), { days: options.maxDaysInPast })\n : undefined\n\n // Maximum date from today\n const dateMax =\n options.maxDaysInFuture || options.maxDaysInFuture === 0\n ? add(startOfToday(), { days: options.maxDaysInFuture })\n : undefined\n\n if (dateMin && date < dateMin) {\n return helpers.error('date.min', { ...context, limit: dateMin })\n }\n\n if (dateMax && date > dateMax) {\n return helpers.error('date.max', { ...context, limit: dateMax })\n }\n\n return payload\n }\n\n return validator\n}\n"],"mappings":"AAAA,SAASA,aAAa,QAAsC,oBAAoB;AAChF,SAASC,GAAG,EAAEC,MAAM,EAAEC,OAAO,EAAEC,KAAK,EAAEC,YAAY,EAAEC,GAAG,QAAQ,UAAU;AAGzE,SAASC,mBAAmB;AAC5B,SACEC,aAAa,EACbC,WAAW,EACXC,WAAW;AAEb,SAASC,WAAW;AAKpB,SA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": "4.0.3",
4
4
  "description": "Defra forms engine",
5
5
  "type": "module",
6
6
  "files": [
package/src/index.ts CHANGED
@@ -9,7 +9,10 @@ const logger = createLogger()
9
9
  process.on('unhandledRejection', (error) => {
10
10
  const err = getErrorMessage(error)
11
11
  logger.info('Unhandled rejection')
12
- logger.error(err, `[unhandledRejection] Unhandled promise rejection: ${err}`)
12
+ logger.error(
13
+ error,
14
+ `[unhandledRejection] Unhandled promise rejection: ${err}`
15
+ )
13
16
  throw error
14
17
  })
15
18
 
@@ -32,6 +35,6 @@ async function startServer() {
32
35
  startServer().catch((error: unknown) => {
33
36
  const err = getErrorMessage(error)
34
37
  logger.info('Server failed to start :(')
35
- logger.error(err, `[serverStartup] Server failed to start: ${err}`)
38
+ logger.error(error, `[serverStartup] Server failed to start: ${err}`)
36
39
  throw error
37
40
  })
@@ -120,15 +120,14 @@
120
120
  "content": "### This is a H3 in markdown\n\n[An internal link](http://localhost:3009/fictional-page)\n\n[An external link](https://defra.gov.uk/fictional-page)",
121
121
  "options": {},
122
122
  "schema": {}
123
- },
124
- {
125
- "title": "Summary",
126
- "path": "/summary",
127
- "controller": "SummaryPageController",
128
- "components": [],
129
- "next": []
130
123
  }
131
124
  ]
125
+ },
126
+ {
127
+ "title": "Summary",
128
+ "path": "/summary",
129
+ "controller": "SummaryPageController",
130
+ "next": []
132
131
  }
133
132
  ],
134
133
  "sections": [
@@ -766,6 +766,62 @@ describe('DatePartsField', () => {
766
766
  }
767
767
  ]
768
768
  },
769
+ {
770
+ description: 'Max days in the future of zero',
771
+ component: {
772
+ title: 'Example date parts field',
773
+ name: 'myComponent',
774
+ type: ComponentType.DatePartsField,
775
+ options: {
776
+ maxDaysInFuture: 0
777
+ }
778
+ } satisfies DatePartsFieldComponent,
779
+ assertions: [
780
+ {
781
+ input: getFormData(OneDayInFuture),
782
+ output: {
783
+ value: getFormData(OneDayInFuture),
784
+ errors: [
785
+ expect.objectContaining({
786
+ text: `Example date parts field must be the same as or before ${format(today, 'd MMMM yyyy')}`
787
+ })
788
+ ]
789
+ }
790
+ },
791
+ {
792
+ input: getFormData(today),
793
+ output: { value: getFormData(today) }
794
+ }
795
+ ]
796
+ },
797
+ {
798
+ description: 'Max days in the past of zero',
799
+ component: {
800
+ title: 'Example date parts field',
801
+ name: 'myComponent',
802
+ type: ComponentType.DatePartsField,
803
+ options: {
804
+ maxDaysInPast: 0
805
+ }
806
+ } satisfies DatePartsFieldComponent,
807
+ assertions: [
808
+ {
809
+ input: getFormData(OneDayInPast),
810
+ output: {
811
+ value: getFormData(OneDayInPast),
812
+ errors: [
813
+ expect.objectContaining({
814
+ text: `Example date parts field must be the same as or after ${format(today, 'd MMMM yyyy')}`
815
+ })
816
+ ]
817
+ }
818
+ },
819
+ {
820
+ input: getFormData(today),
821
+ output: { value: getFormData(today) }
822
+ }
823
+ ]
824
+ },
769
825
  {
770
826
  description: 'Optional fields',
771
827
  component: {
@@ -278,14 +278,16 @@ export function getValidatorDate(component: DatePartsField) {
278
278
  }
279
279
 
280
280
  // Minimum date from today
281
- const dateMin = options.maxDaysInPast
282
- ? sub(startOfToday(), { days: options.maxDaysInPast })
283
- : undefined
281
+ const dateMin =
282
+ options.maxDaysInPast || options.maxDaysInPast === 0
283
+ ? sub(startOfToday(), { days: options.maxDaysInPast })
284
+ : undefined
284
285
 
285
286
  // Maximum date from today
286
- const dateMax = options.maxDaysInFuture
287
- ? add(startOfToday(), { days: options.maxDaysInFuture })
288
- : undefined
287
+ const dateMax =
288
+ options.maxDaysInFuture || options.maxDaysInFuture === 0
289
+ ? add(startOfToday(), { days: options.maxDaysInFuture })
290
+ : undefined
289
291
 
290
292
  if (dateMin && date < dateMin) {
291
293
  return helpers.error('date.min', { ...context, limit: dateMin })