@defra/forms-engine-plugin 4.5.6 → 4.6.0

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.
@@ -32,11 +32,9 @@ export declare class EmailAddressField extends FormComponent {
32
32
  multiple?: string;
33
33
  accept
34
34
  /**
35
- * Static version of getAllPossibleErrors that doesn't require a component instance.
35
+ * For error preview page that shows all possible errors on a component
36
36
  */
37
- ? /**
38
- * Static version of getAllPossibleErrors that doesn't require a component instance.
39
- */: string;
37
+ ?: string;
40
38
  inputmode?: string;
41
39
  };
42
40
  content?: import("./types.js").Content | import("./types.js").Content[] | string;
@@ -1,3 +1,4 @@
1
+ import { preventUnicodeInEmail } from '@defra/forms-model';
1
2
  import joi from 'joi';
2
3
  import { FormComponent } from "./FormComponent.js";
3
4
  import { messageTemplate } from "../pageControllers/validationOptions.js";
@@ -7,7 +8,7 @@ export class EmailAddressField extends FormComponent {
7
8
  const {
8
9
  options
9
10
  } = def;
10
- let formSchema = joi.string().email().trim().label(this.label).required();
11
+ let formSchema = joi.string().trim().email().custom((value, helpers) => preventUnicodeInEmail(value, helpers)).label(this.label).required();
11
12
  if (options.required === false) {
12
13
  formSchema = formSchema.allow('');
13
14
  }
@@ -55,6 +56,9 @@ export class EmailAddressField extends FormComponent {
55
56
  }, {
56
57
  type: 'format',
57
58
  template: messageTemplate.format
59
+ }, {
60
+ type: 'unicode',
61
+ template: messageTemplate.unicode
58
62
  }],
59
63
  advancedSettingsErrors: []
60
64
  };
@@ -1 +1 @@
1
- {"version":3,"file":"EmailAddressField.js","names":["joi","FormComponent","messageTemplate","EmailAddressField","constructor","def","props","options","formSchema","string","email","trim","label","required","allow","customValidationMessage","message","messages","customValidationMessages","default","stateSchema","getViewModel","payload","errors","viewModel","attributes","autocomplete","type","getAllPossibleErrors","baseErrors","template","format","advancedSettingsErrors"],"sources":["../../../../../src/server/plugins/engine/components/EmailAddressField.ts"],"sourcesContent":["import { type EmailAddressFieldComponent } from '@defra/forms-model'\nimport joi from 'joi'\n\nimport { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type ErrorMessageTemplateList,\n type FormPayload,\n type FormSubmissionError\n} from '~/src/server/plugins/engine/types.js'\n\nexport class EmailAddressField extends FormComponent {\n declare options: EmailAddressFieldComponent['options']\n\n constructor(\n def: EmailAddressFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { options } = def\n\n let formSchema = joi.string().email().trim().label(this.label).required()\n\n if (options.required === false) {\n formSchema = formSchema.allow('')\n }\n\n if (options.customValidationMessage) {\n const message = options.customValidationMessage\n\n formSchema = formSchema.messages({\n 'any.required': message,\n 'string.empty': message,\n 'string.email': message\n })\n } else if (options.customValidationMessages) {\n formSchema = formSchema.messages(options.customValidationMessages)\n }\n\n this.formSchema = formSchema.default('')\n this.stateSchema = formSchema.default(null).allow(null)\n this.options = options\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const viewModel = super.getViewModel(payload, errors)\n const { attributes } = viewModel\n\n attributes.autocomplete = 'email'\n\n return {\n ...viewModel,\n type: 'email'\n }\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return EmailAddressField.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: 'format', template: messageTemplate.format }\n ],\n advancedSettingsErrors: []\n }\n }\n}\n"],"mappings":"AACA,OAAOA,GAAG,MAAM,KAAK;AAErB,SAASC,aAAa;AACtB,SAASC,eAAe;AAOxB,OAAO,MAAMC,iBAAiB,SAASF,aAAa,CAAC;EAGnDG,WAAWA,CACTC,GAA+B,EAC/BC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,E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B;IAC/C,OAAOzB,iBAAiB,CAACyB,oBAAoB,CAAC,CAAC;EACjD;;EAEA;AACF;AACA;EACE,OAAOA,oBAAoBA,CAAA,EAA6B;IACtD,OAAO;MACLC,UAAU,EAAE,CACV;QAAEF,IAAI,EAAE,UAAU;QAAEG,QAAQ,EAAE5B,eAAe,CAACW;MAAS,CAAC,EACxD;QAAEc,IAAI,EAAE,QAAQ;QAAEG,QAAQ,EAAE5B,eAAe,CAAC6B;MAAO,CAAC,CACrD;MACDC,sBAAsB,EAAE;IAC1B,CAAC;EACH;AACF","ignoreList":[]}
1
+ {"version":3,"file":"EmailAddressField.js","names":["preventUnicodeInEmail","joi","FormComponent","messageTemplate","EmailAddressField","constructor","def","props","options","formSchema","string","trim","email","custom","value","helpers","label","required","allow","customValidationMessage","message","messages","customValidationMessages","default","stateSchema","getViewModel","payload","errors","viewModel","attributes","autocomplete","type","getAllPossibleErrors","baseErrors","template","format","unicode","advancedSettingsErrors"],"sources":["../../../../../src/server/plugins/engine/components/EmailAddressField.ts"],"sourcesContent":["import {\n preventUnicodeInEmail,\n type EmailAddressFieldComponent\n} from '@defra/forms-model'\nimport joi, { type CustomHelpers } from 'joi'\n\nimport { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type ErrorMessageTemplateList,\n type FormPayload,\n type FormSubmissionError\n} from '~/src/server/plugins/engine/types.js'\n\nexport class EmailAddressField extends FormComponent {\n declare options: EmailAddressFieldComponent['options']\n\n constructor(\n def: EmailAddressFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { options } = def\n\n let formSchema = joi\n .string()\n .trim()\n .email()\n .custom((value, helpers: CustomHelpers<string>) =>\n preventUnicodeInEmail(value, helpers)\n )\n .label(this.label)\n .required()\n\n if (options.required === false) {\n formSchema = formSchema.allow('')\n }\n\n if (options.customValidationMessage) {\n const message = options.customValidationMessage\n\n formSchema = formSchema.messages({\n 'any.required': message,\n 'string.empty': message,\n 'string.email': message\n })\n } else if (options.customValidationMessages) {\n formSchema = formSchema.messages(options.customValidationMessages)\n }\n\n this.formSchema = formSchema.default('')\n this.stateSchema = formSchema.default(null).allow(null)\n this.options = options\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const viewModel = super.getViewModel(payload, errors)\n const { attributes } = viewModel\n\n attributes.autocomplete = 'email'\n\n return {\n ...viewModel,\n type: 'email'\n }\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return EmailAddressField.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: 'format', template: messageTemplate.format },\n { type: 'unicode', template: messageTemplate.unicode }\n ],\n advancedSettingsErrors: []\n }\n }\n}\n"],"mappings":"AAAA,SACEA,qBAAqB,QAEhB,oBAAoB;AAC3B,OAAOC,GAAG,MAA8B,KAAK;AAE7C,SAASC,aAAa;AACtB,SAASC,eAAe;AAOxB,OAAO,MAAMC,iBAAiB,SAASF,aAAa,CAAC;EAGnDG,WAAWA,CACTC,GAA+B,EAC/BC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC;IAAQ,CAAC,GAAGF,GAAG;IAEvB,IAAIG,UAAU,GAAGR,GAAG,CACjBS,MAAM,CAAC,CAAC,CACRC,IAAI,CAAC,CAAC,CACNC,KAAK,CAAC,CAAC,CACPC,MAAM,CAAC,CAACC,KAAK,EAAEC,OAA8B,KAC5Cf,qBAAqB,CAACc,KAAK,EAAEC,OAAO,CACtC,CAAC,CACAC,KAAK,CAAC,IAAI,CAACA,KAAK,CAAC,CACjBC,QAAQ,CAAC,CAAC;IAEb,IAAIT,OAAO,CAACS,QAAQ,KAAK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eAAe,CAACgC;MAAO,CAAC,EACpD;QAAEJ,IAAI,EAAE,SAAS;QAAEG,QAAQ,EAAE/B,eAAe,CAACiC;MAAQ,CAAC,CACvD;MACDC,sBAAsB,EAAE;IAC1B,CAAC;EACH;AACF","ignoreList":[]}
@@ -1,7 +1,7 @@
1
1
  import { ControllerType, getHiddenFields } from '@defra/forms-model';
2
- import { validate as isValidUUID } from 'uuid';
3
2
  import { getCacheService } from "../../helpers.js";
4
3
  import { CURRENT_PAGE_PATH_KEY, STATE_NOT_YET_VALIDATED } from "../../index.js";
4
+ import { isValidUUID } from "../../../../utils/utils.js";
5
5
  const GUID_LENGTH = 36;
6
6
 
7
7
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","names":["ControllerType","getHiddenFields","validate","isValidUUID","getCacheService","CURRENT_PAGE_PATH_KEY","STATE_NOT_YET_VALIDATED","GUID_LENGTH","paramLookupFunctions","formId","val","services","formTitle","meta","formsService","getFormMetadataById","title","key","value","stripParam","query","paramToRemove","params","Object","entries","keys","length","undefined","prefillStateFromQueryParameters","request","page","model","hiddenFieldNames","Set","def","map","field","name","size","has","lookupFunc","res","formData","getState","mergeState","checkSaveAndExitRepeater","context","potentiallyInvalidState","state","originalPath","repeaterPaths","pages","filter","controller","Repeat","p","basePath","path","segments","split","lastSegment","at","guidStartIndex","originalPathWithoutGuid","substring","includes","copyNotYetValidatedState","url","pathname","payload","cacheService","server","setState"],"sources":["../../../../../../src/server/plugins/engine/pageControllers/helpers/state.ts"],"sourcesContent":["import { ControllerType, getHiddenFields } from '@defra/forms-model'\nimport { validate as isValidUUID } from 'uuid'\n\nimport { getCacheService } from '~/src/server/plugins/engine/helpers.js'\nimport {\n CURRENT_PAGE_PATH_KEY,\n STATE_NOT_YET_VALIDATED\n} from '~/src/server/plugins/engine/index.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/FormModel.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'\nimport {\n type AnyFormRequest,\n type FormContext,\n type FormStateValue,\n type FormValue\n} from '~/src/server/plugins/engine/types.js'\nimport { type FormQuery } from '~/src/server/routes/types.js'\nimport { type Services } from '~/src/server/types.js'\n\nconst GUID_LENGTH = 36\n\n/**\n * A series of functions that can transform a pre-fill input parameter e.g lookup a form title based on form id\n */\nconst paramLookupFunctions = {\n formId: async (val: string, services: Services) => {\n let formTitle\n if (val) {\n const meta = await services.formsService.getFormMetadataById(val)\n formTitle = meta.title\n }\n return {\n key: 'formName',\n value: formTitle\n }\n }\n} as Partial<\n Record<\n string,\n (\n val: string,\n services: Services\n ) => Promise<{ key: string; value: string | undefined }>\n >\n>\n\nexport function stripParam(query: FormQuery, paramToRemove: string) {\n const params = {} as Record<string, FormStateValue | undefined>\n for (const [key, value = ''] of Object.entries(query)) {\n if (key !== paramToRemove) {\n params[key] = value\n }\n }\n return Object.keys(params).length ? (params as FormQuery) : undefined\n}\n\n/**\n * Any hidden parameters defined in the FormDefinition may be pre-filled by URL parameter values.\n * Other parameters are ignored for security reasons.\n * @param request\n * @param model\n */\nexport async function prefillStateFromQueryParameters(\n request: AnyFormRequest,\n page: PageControllerClass\n): Promise<boolean> {\n const { model } = page\n\n const hiddenFieldNames = new Set(\n getHiddenFields(model.def).map((field) => field.name)\n )\n\n if (!hiddenFieldNames.size) {\n return false\n }\n\n // Remove 'returnUrl' param\n const query = stripParam(request.query, 'returnUrl')\n\n if (!query) {\n return false\n }\n\n const params = {} as Record<string, FormStateValue | undefined>\n\n for (const [key, value = ''] of Object.entries(query)) {\n if (hiddenFieldNames.has(key)) {\n const lookupFunc = paramLookupFunctions[key]\n if (lookupFunc) {\n const res = await lookupFunc(value, model.services)\n // Store original value and result\n params[key] = value\n params[res.key] = res.value\n } else {\n params[key] = value\n }\n }\n }\n\n const formData = await page.getState(request)\n await page.mergeState(request, formData, params)\n\n return true\n}\n\n/**\n * Checks whether the save-and-exit finished on a repeater with partial state\n * @param context - the form context\n */\nexport function checkSaveAndExitRepeater(\n context: FormContext,\n model: FormModel\n) {\n const potentiallyInvalidState = context.state[STATE_NOT_YET_VALIDATED] as\n | Record<string, FormValue>\n | undefined\n if (!potentiallyInvalidState) {\n return\n }\n\n const originalPath = potentiallyInvalidState[CURRENT_PAGE_PATH_KEY]\n\n const repeaterPaths = model.def.pages\n .filter((page) => page.controller === ControllerType.Repeat)\n .map((p) => `/${model.basePath}${p.path}/`)\n\n if (typeof originalPath !== 'string') {\n return undefined\n }\n\n const segments = originalPath.split('/')\n const lastSegment = segments.at(-1) ?? ''\n\n if (!isValidUUID(lastSegment)) {\n return undefined\n }\n\n const guidStartIndex = originalPath.length - GUID_LENGTH\n const originalPathWithoutGuid = originalPath.substring(0, guidStartIndex)\n\n if (!repeaterPaths.includes(originalPathWithoutGuid)) {\n return undefined\n }\n\n return originalPath\n}\n\n/**\n * Copies any potentially invalid state into the payload, and removes those values from state\n * NOTE - this method has a side-effect on 'context.state' and 'context.payload'\n * @param request - the form request\n * @param context - the form context\n */\nexport async function copyNotYetValidatedState(\n request: AnyFormRequest,\n context: FormContext\n) {\n const potentiallyInvalidState = context.state[STATE_NOT_YET_VALIDATED] as\n | Record<string, FormValue>\n | undefined\n if (!potentiallyInvalidState) {\n return\n }\n\n const originalPath = potentiallyInvalidState[CURRENT_PAGE_PATH_KEY]\n\n if (originalPath && originalPath === request.url.pathname) {\n context.payload = {\n ...context.payload,\n ...potentiallyInvalidState,\n [CURRENT_PAGE_PATH_KEY]: undefined\n }\n\n // Remove any temporary 'not yet validated' state now it's been copied to the payload\n if (context.state[STATE_NOT_YET_VALIDATED]) {\n context.state[STATE_NOT_YET_VALIDATED] = undefined\n }\n\n const cacheService = getCacheService(request.server)\n await cacheService.setState(request, context.state)\n }\n}\n"],"mappings":"AAAA,SAASA,cAAc,EAAEC,eAAe,QAAQ,oBAAoB;AACpE,SAASC,QAAQ,IAAIC,WAAW,QAAQ,MAAM;AAE9C,SAASC,eAAe;AACxB,SACEC,qBAAqB,EACrBC,uBAAuB;AAazB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GAAGqB,SAAS;IACpD;IAEA,MAAM2C,YAAY,GAAGlE,eAAe,CAACyB,OAAO,CAAC0C,MAAM,CAAC;IACpD,MAAMD,YAAY,CAACE,QAAQ,CAAC3C,OAAO,EAAEiB,OAAO,CAACE,KAAK,CAAC;EACrD;AACF","ignoreList":[]}
1
+ {"version":3,"file":"state.js","names":["ControllerType","getHiddenFields","getCacheService","CURRENT_PAGE_PATH_KEY","STATE_NOT_YET_VALIDATED","isValidUUID","GUID_LENGTH","paramLookupFunctions","formId","val","services","formTitle","meta","formsService","getFormMetadataById","title","key","value","stripParam","query","paramToRemove","params","Object","entries","keys","length","undefined","prefillStateFromQueryParameters","request","page","model","hiddenFieldNames","Set","def","map","field","name","size","has","lookupFunc","res","formData","getState","mergeState","checkSaveAndExitRepeater","context","potentiallyInvalidState","state","originalPath","repeaterPaths","pages","filter","controller","Repeat","p","basePath","path","segments","split","lastSegment","at","guidStartIndex","originalPathWithoutGuid","substring","includes","copyNotYetValidatedState","url","pathname","payload","cacheService","server","setState"],"sources":["../../../../../../src/server/plugins/engine/pageControllers/helpers/state.ts"],"sourcesContent":["import { ControllerType, getHiddenFields } from '@defra/forms-model'\n\nimport { getCacheService } from '~/src/server/plugins/engine/helpers.js'\nimport {\n CURRENT_PAGE_PATH_KEY,\n STATE_NOT_YET_VALIDATED\n} from '~/src/server/plugins/engine/index.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/FormModel.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'\nimport {\n type AnyFormRequest,\n type FormContext,\n type FormStateValue,\n type FormValue\n} from '~/src/server/plugins/engine/types.js'\nimport { type FormQuery } from '~/src/server/routes/types.js'\nimport { type Services } from '~/src/server/types.js'\nimport { isValidUUID } from '~/src/server/utils/utils.js'\n\nconst GUID_LENGTH = 36\n\n/**\n * A series of functions that can transform a pre-fill input parameter e.g lookup a form title based on form id\n */\nconst paramLookupFunctions = {\n formId: async (val: string, services: Services) => {\n let formTitle\n if (val) {\n const meta = await services.formsService.getFormMetadataById(val)\n formTitle = meta.title\n }\n return {\n key: 'formName',\n value: formTitle\n }\n }\n} as Partial<\n Record<\n string,\n (\n val: string,\n services: Services\n ) => Promise<{ key: string; value: string | undefined }>\n >\n>\n\nexport function stripParam(query: FormQuery, paramToRemove: string) {\n const params = {} as Record<string, FormStateValue | undefined>\n for (const [key, value = ''] of Object.entries(query)) {\n if (key !== paramToRemove) {\n params[key] = value\n }\n }\n return Object.keys(params).length ? (params as FormQuery) : undefined\n}\n\n/**\n * Any hidden parameters defined in the FormDefinition may be pre-filled by URL parameter values.\n * Other parameters are ignored for security reasons.\n * @param request\n * @param model\n */\nexport async function prefillStateFromQueryParameters(\n request: AnyFormRequest,\n page: PageControllerClass\n): Promise<boolean> {\n const { model } = page\n\n const hiddenFieldNames = new Set(\n getHiddenFields(model.def).map((field) => field.name)\n )\n\n if (!hiddenFieldNames.size) {\n return false\n }\n\n // Remove 'returnUrl' param\n const query = stripParam(request.query, 'returnUrl')\n\n if (!query) {\n return false\n }\n\n const params = {} as Record<string, FormStateValue | undefined>\n\n for (const [key, value = ''] of Object.entries(query)) {\n if (hiddenFieldNames.has(key)) {\n const lookupFunc = paramLookupFunctions[key]\n if (lookupFunc) {\n const res = await lookupFunc(value, model.services)\n // Store original value and result\n params[key] = value\n params[res.key] = res.value\n } else {\n params[key] = value\n }\n }\n }\n\n const formData = await page.getState(request)\n await page.mergeState(request, formData, params)\n\n return true\n}\n\n/**\n * Checks whether the save-and-exit finished on a repeater with partial state\n * @param context - the form context\n */\nexport function checkSaveAndExitRepeater(\n context: FormContext,\n model: FormModel\n) {\n const potentiallyInvalidState = context.state[STATE_NOT_YET_VALIDATED] as\n | Record<string, FormValue>\n | undefined\n if (!potentiallyInvalidState) {\n return\n }\n\n const originalPath = potentiallyInvalidState[CURRENT_PAGE_PATH_KEY]\n\n const repeaterPaths = model.def.pages\n .filter((page) => page.controller === ControllerType.Repeat)\n .map((p) => `/${model.basePath}${p.path}/`)\n\n if (typeof originalPath !== 'string') {\n return undefined\n }\n\n const segments = originalPath.split('/')\n const lastSegment = segments.at(-1) ?? ''\n\n if (!isValidUUID(lastSegment)) {\n return undefined\n }\n\n const guidStartIndex = originalPath.length - GUID_LENGTH\n const originalPathWithoutGuid = originalPath.substring(0, guidStartIndex)\n\n if (!repeaterPaths.includes(originalPathWithoutGuid)) {\n return undefined\n }\n\n return originalPath\n}\n\n/**\n * Copies any potentially invalid state into the payload, and removes those values from state\n * NOTE - this method has a side-effect on 'context.state' and 'context.payload'\n * @param request - the form request\n * @param context - the form context\n */\nexport async function copyNotYetValidatedState(\n request: AnyFormRequest,\n context: FormContext\n) {\n const potentiallyInvalidState = context.state[STATE_NOT_YET_VALIDATED] as\n | Record<string, FormValue>\n | undefined\n if (!potentiallyInvalidState) {\n return\n }\n\n const originalPath = potentiallyInvalidState[CURRENT_PAGE_PATH_KEY]\n\n if (originalPath && originalPath === request.url.pathname) {\n context.payload = {\n ...context.payload,\n ...potentiallyInvalidState,\n [CURRENT_PAGE_PATH_KEY]: undefined\n }\n\n // Remove any temporary 'not yet validated' state now it's been copied to the payload\n if (context.state[STATE_NOT_YET_VALIDATED]) {\n context.state[STATE_NOT_YET_VALIDATED] = undefined\n }\n\n const cacheService = getCacheService(request.server)\n await cacheService.setState(request, context.state)\n }\n}\n"],"mappings":"AAAA,SAASA,cAAc,EAAEC,eAAe,QAAQ,oBAAoB;AAEpE,SAASC,eAAe;AACxB,SACEC,qBAAqB,EACrBC,uBAAuB;AAYzB,SAASC,WAAW;AAEpB,MAAMC,WAAW,GAAG,EAAE;;AAEtB;AACA;AACA;AACA,MAAMC,oBAAoB,GAAG;EAC3BC,MAAM,EAAE,MAAAA,CAAOC,GAAW,EAAEC,QAAkB,KAAK;IACjD,IAAIC,SAAS;IACb,IAAIF,GAAG,EAAE;MACP,MAAMG,IAAI,GAAG,MAAMF,QAAQ,CAACG,YAAY,CAACC,mBAAmB,CAACL,GAAG,CAAC;MACjEE,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,CAAC,CAAC,IAAI,EAAE;EAEzC,IAAI,CAACvD,WAAW,CAACsD,WAAW,CAAC,EAAE;IAC7B,OAAOjC,SAAS;EAClB;EAEA,MAAMmC,cAAc,GAAGb,YAAY,CAACvB,MAAM,GAAGnB,WAAW;EACxD,MAAMwD,uBAAuB,GAAGd,YAAY,CAACe,SAAS,CAAC,CAAC,EAAEF,cAAc,CAAC;EAEzE,IAAI,CAACZ,aAAa,CAACe,QAAQ,CAACF,uBAAuB,CAAC,EAAE;IACpD,OAAOpC,SAAS;EAClB;EAEA,OAAOsB,YAAY;AACrB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,eAAeiB,wBAAwBA,CAC5CrC,OAAuB,EACvBiB,OAAoB,EACpB;EACA,MAAMC,uBAAuB,GAAGD,OAAO,CAACE,KAAK,CAAC3C,uBAAuB,CAExD;EACb,IAAI,CAAC0C,uBAAuB,EAAE;IAC5B;EACF;EAEA,MAAME,YAAY,GAAGF,uBAAuB,CAAC3C,qBAAqB,CAAC;EAEnE,IAAI6C,YAAY,IAAIA,YAAY,KAAKpB,OAAO,CAACsC,GAAG,CAACC,QAAQ,EAAE;IACzDtB,OAAO,CAACuB,OAAO,GAAG;MAChB,GAAGvB,OAAO,CAACuB,OAAO;MAClB,GAAGtB,uBAAuB;MAC1B,CAAC3C,qBAAqB,GAAGuB;IAC3B,CAAC;;IAED;IACA,IAAImB,OAAO,CAACE,KAAK,CAAC3C,uBAAuB,CAAC,EAAE;MAC1CyC,OAAO,CAACE,KAAK,CAAC3C,uBAAuB,CAAC,GAAGsB,SAAS;IACpD;IAEA,MAAM2C,YAAY,GAAGnE,eAAe,CAAC0B,OAAO,CAAC0C,MAAM,CAAC;IACpD,MAAMD,YAAY,CAACE,QAAQ,CAAC3C,OAAO,EAAEiB,OAAO,CAACE,KAAK,CAAC;EACrD;AACF","ignoreList":[]}
@@ -21,6 +21,7 @@ export const messageTemplate = {
21
21
  minMax: '{{#label}} must be between {{#min}} and {{#max}} characters',
22
22
  pattern: joi.expression('Enter a valid {{lowerFirst(#label)}}', opts),
23
23
  format: joi.expression('Enter {{lowerFirst(#label)}} in the correct format', opts),
24
+ unicode: '{{#label}} includes invalid characters, for example, long dashes',
24
25
  number: '{{#label}} must be a number',
25
26
  numberPrecision: '{{#label}} must have {{#limit}} or fewer decimal places',
26
27
  numberInteger: '{{#label}} must be a whole number',
@@ -41,6 +42,7 @@ export const messages = {
41
42
  'string.empty': messageTemplate.required,
42
43
  'string.max': messageTemplate.max,
43
44
  'string.email': messageTemplate.format,
45
+ 'string.unicode': messageTemplate.unicode,
44
46
  'string.pattern.base': messageTemplate.pattern,
45
47
  'string.maxWords': messageTemplate.maxWords,
46
48
  'number.base': messageTemplate.number,
@@ -1 +1 @@
1
- {"version":3,"file":"validationOptions.js","names":["joi","lowerFirstPreserveProperNouns","opts","functions","lowerFirst","messageTemplate","declarationRequired","expression","required","selectRequired","selectYesNoRequired","max","min","minMax","pattern","format","number","numberPrecision","numberInteger","numberMin","numberMax","maxWords","objectRequired","objectMissing","dateFormat","dateMin","dateMax","messages","messagesPre","validationOptions","abortEarly","errors","wrap","array","label"],"sources":["../../../../../src/server/plugins/engine/pageControllers/validationOptions.ts"],"sourcesContent":["// Declaration above is needed for: https://github.com/hapijs/joi/issues/3064\n\nimport joi, {\n type JoiExpression,\n type LanguageMessages,\n type LanguageMessagesExt,\n type ReferenceOptions,\n type ValidationOptions\n} from 'joi'\n\nimport { lowerFirstPreserveProperNouns } from '~/src/server/plugins/engine/components/helpers/index.js'\n\nconst opts = {\n functions: {\n lowerFirst: lowerFirstPreserveProperNouns\n }\n} as ReferenceOptions\n\n/**\n * see @link https://joi.dev/api/?v=17.4.2#template-syntax for template syntax\n */\nexport const messageTemplate: Record<string, JoiExpression> = {\n declarationRequired: joi.expression(\n 'You must confirm you understand and agree with the {{lowerFirst(#label)}} to continue',\n opts\n ) as JoiExpression,\n required: joi.expression(\n 'Enter {{lowerFirst(#label)}}',\n opts\n ) as JoiExpression,\n selectRequired: joi.expression(\n 'Select {{lowerFirst(#label)}}',\n opts\n ) as JoiExpression,\n selectYesNoRequired: '{{#label}} - select yes or no',\n max: '{{#label}} must be {{#limit}} characters or less',\n min: '{{#label}} must be {{#limit}} characters or more',\n minMax: '{{#label}} must be between {{#min}} and {{#max}} characters',\n pattern: joi.expression(\n 'Enter a valid {{lowerFirst(#label)}}',\n opts\n ) as JoiExpression,\n format: joi.expression(\n 'Enter {{lowerFirst(#label)}} in the correct format',\n opts\n ) as JoiExpression,\n number: '{{#label}} must be a number',\n numberPrecision: '{{#label}} must have {{#limit}} or fewer decimal places',\n numberInteger: '{{#label}} must be a whole number',\n numberMin: '{{#label}} must be {{#limit}} or higher',\n numberMax: '{{#label}} must be {{#limit}} or lower',\n maxWords: '{{#label}} must be {{#limit}} words or fewer',\n\n // Nested fields use component title\n\n objectRequired: joi.expression('Enter {{#label}}', opts) as JoiExpression,\n objectMissing: joi.expression(\n '{{#title}} must include a {{lowerFirst(#label)}}',\n opts\n ) as JoiExpression,\n dateFormat: '{{#title}} must be a real date',\n dateMin: '{{#title}} must be the same as or after {{#limit}}',\n dateMax: '{{#title}} must be the same as or before {{#limit}}'\n}\n\nexport const messages: LanguageMessagesExt = {\n 'string.base': messageTemplate.required,\n 'string.min': messageTemplate.min,\n 'string.empty': messageTemplate.required,\n 'string.max': messageTemplate.max,\n 'string.email': messageTemplate.format,\n 'string.pattern.base': messageTemplate.pattern,\n 'string.maxWords': messageTemplate.maxWords,\n\n 'number.base': messageTemplate.number,\n 'number.precision': messageTemplate.numberPrecision,\n 'number.integer': messageTemplate.numberInteger,\n 'number.unsafe': messageTemplate.format,\n 'number.min': messageTemplate.numberMin,\n 'number.max': messageTemplate.numberMax,\n\n 'object.required': messageTemplate.objectRequired,\n 'object.and': messageTemplate.objectMissing,\n\n 'any.only': messageTemplate.selectRequired,\n 'any.required': messageTemplate.selectRequired,\n 'any.empty': messageTemplate.required,\n\n 'date.base': messageTemplate.dateFormat,\n 'date.format': messageTemplate.dateFormat,\n 'date.min': messageTemplate.dateMin,\n 'date.max': messageTemplate.dateMax,\n\n 'object.invalidjson': messageTemplate.format\n}\n\nexport const messagesPre: LanguageMessages =\n messages as unknown as LanguageMessages\n\nexport const validationOptions: ValidationOptions = {\n abortEarly: false,\n messages: messagesPre,\n errors: {\n wrap: {\n array: false,\n label: false\n }\n }\n}\n"],"mappings":"AAAA;;AAEA,OAAOA,GAAG,MAMH,KAAK;AAEZ,SAASC,6BAA6B;AAEtC,MAAMC,IAAI,GAAG;EACXC,SAAS,EAAE;IACTC,UAAU,EAAEH;EACd;AACF,CAAqB;;AAErB;AACA;AACA;AACA,OAAO,MAAMI,eAA8C,GAAG;EAC5DC,mBAAmB,EAAEN,GAAG,CAACO,UAAU,CACjC,uFAAuF,EACvFL,IACF,CAAkB;EAClBM,QAAQ,EAAER,GAAG,CAACO,UAAU,CACtB,8BAA8B,EAC9BL,IACF,CAAkB;EAClBO,cAAc,EAAET,GAAG,CAACO,UAAU,CAC5B,+BAA+B,EAC/BL,IACF,CAAkB;EAClBQ,mBAAmB,EAAE,+BAA+B;EACpDC,GAAG,EAAE,kDAAkD;EACvDC,GAAG,EAAE,kDAAkD;EACvDC,MAAM,EAAE,6DAA6D;EACrEC,OAAO,EAAEd,GAAG,CAACO,UAAU,CACrB,sCAAsC,EACtCL,IACF,CAAkB;EAClBa,MAAM,EAAEf,GAAG,CAACO,UAAU,CACpB,oDAAoD,EACpDL,IACF,CAAkB;EAClBc,MAAM,EAAE,6BAA6B;EACrCC,eAAe,EAAE,yDAAyD;EAC1EC,aAAa,EAAE,mCAAmC;EAClDC,SAAS,EAAE,yCAAyC;EACpDC,SAAS,EAAE,wCAAwC;EACnDC,QAAQ,EAAE,8CAA8C;EAExD;;EAEAC,cAAc,EAAEtB,GAAG,CAACO,UAAU,CAAC,kBAAkB,EAAEL,IAAI,CAAkB;EACzEqB,aAAa,EAAEvB,GAAG,CAACO,UAAU,CAC3B,kDAAkD,EAClDL,IACF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C,KAAK,EAAE;IACT;EACF;AACF,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"validationOptions.js","names":["joi","lowerFirstPreserveProperNouns","opts","functions","lowerFirst","messageTemplate","declarationRequired","expression","required","selectRequired","selectYesNoRequired","max","min","minMax","pattern","format","unicode","number","numberPrecision","numberInteger","numberMin","numberMax","maxWords","objectRequired","objectMissing","dateFormat","dateMin","dateMax","messages","messagesPre","validationOptions","abortEarly","errors","wrap","array","label"],"sources":["../../../../../src/server/plugins/engine/pageControllers/validationOptions.ts"],"sourcesContent":["// Declaration above is needed for: https://github.com/hapijs/joi/issues/3064\n\nimport joi, {\n type JoiExpression,\n type LanguageMessages,\n type LanguageMessagesExt,\n type ReferenceOptions,\n type ValidationOptions\n} from 'joi'\n\nimport { lowerFirstPreserveProperNouns } from '~/src/server/plugins/engine/components/helpers/index.js'\n\nconst opts = {\n functions: {\n lowerFirst: lowerFirstPreserveProperNouns\n }\n} as ReferenceOptions\n\n/**\n * see @link https://joi.dev/api/?v=17.4.2#template-syntax for template syntax\n */\nexport const messageTemplate: Record<string, JoiExpression> = {\n declarationRequired: joi.expression(\n 'You must confirm you understand and agree with the {{lowerFirst(#label)}} to continue',\n opts\n ) as JoiExpression,\n required: joi.expression(\n 'Enter {{lowerFirst(#label)}}',\n opts\n ) as JoiExpression,\n selectRequired: joi.expression(\n 'Select {{lowerFirst(#label)}}',\n opts\n ) as JoiExpression,\n selectYesNoRequired: '{{#label}} - select yes or no',\n max: '{{#label}} must be {{#limit}} characters or less',\n min: '{{#label}} must be {{#limit}} characters or more',\n minMax: '{{#label}} must be between {{#min}} and {{#max}} characters',\n pattern: joi.expression(\n 'Enter a valid {{lowerFirst(#label)}}',\n opts\n ) as JoiExpression,\n format: joi.expression(\n 'Enter {{lowerFirst(#label)}} in the correct format',\n opts\n ) as JoiExpression,\n unicode: '{{#label}} includes invalid characters, for example, long dashes',\n number: '{{#label}} must be a number',\n numberPrecision: '{{#label}} must have {{#limit}} or fewer decimal places',\n numberInteger: '{{#label}} must be a whole number',\n numberMin: '{{#label}} must be {{#limit}} or higher',\n numberMax: '{{#label}} must be {{#limit}} or lower',\n maxWords: '{{#label}} must be {{#limit}} words or fewer',\n\n // Nested fields use component title\n\n objectRequired: joi.expression('Enter {{#label}}', opts) as JoiExpression,\n objectMissing: joi.expression(\n '{{#title}} must include a {{lowerFirst(#label)}}',\n opts\n ) as JoiExpression,\n dateFormat: '{{#title}} must be a real date',\n dateMin: '{{#title}} must be the same as or after {{#limit}}',\n dateMax: '{{#title}} must be the same as or before {{#limit}}'\n}\n\nexport const messages: LanguageMessagesExt = {\n 'string.base': messageTemplate.required,\n 'string.min': messageTemplate.min,\n 'string.empty': messageTemplate.required,\n 'string.max': messageTemplate.max,\n 'string.email': messageTemplate.format,\n 'string.unicode': messageTemplate.unicode,\n 'string.pattern.base': messageTemplate.pattern,\n 'string.maxWords': messageTemplate.maxWords,\n\n 'number.base': messageTemplate.number,\n 'number.precision': messageTemplate.numberPrecision,\n 'number.integer': messageTemplate.numberInteger,\n 'number.unsafe': messageTemplate.format,\n 'number.min': messageTemplate.numberMin,\n 'number.max': messageTemplate.numberMax,\n\n 'object.required': messageTemplate.objectRequired,\n 'object.and': messageTemplate.objectMissing,\n\n 'any.only': messageTemplate.selectRequired,\n 'any.required': messageTemplate.selectRequired,\n 'any.empty': messageTemplate.required,\n\n 'date.base': messageTemplate.dateFormat,\n 'date.format': messageTemplate.dateFormat,\n 'date.min': messageTemplate.dateMin,\n 'date.max': messageTemplate.dateMax,\n\n 'object.invalidjson': messageTemplate.format\n}\n\nexport const messagesPre: LanguageMessages =\n messages as unknown as LanguageMessages\n\nexport const validationOptions: ValidationOptions = {\n abortEarly: false,\n messages: messagesPre,\n errors: {\n wrap: {\n array: false,\n label: false\n }\n }\n}\n"],"mappings":"AAAA;;AAEA,OAAOA,GAAG,MAMH,KAAK;AAEZ,SAASC,6BAA6B;AAEtC,MAAMC,IAAI,GAAG;EACXC,SAAS,EAAE;IACTC,UA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AgB,EAAEV,eAAe,CAACW,OAAO;EACzC,qBAAqB,EAAEX,eAAe,CAACS,OAAO;EAC9C,iBAAiB,EAAET,eAAe,CAACiB,QAAQ;EAE3C,aAAa,EAAEjB,eAAe,CAACY,MAAM;EACrC,kBAAkB,EAAEZ,eAAe,CAACa,eAAe;EACnD,gBAAgB,EAAEb,eAAe,CAACc,aAAa;EAC/C,eAAe,EAAEd,eAAe,CAACU,MAAM;EACvC,YAAY,EAAEV,eAAe,CAACe,SAAS;EACvC,YAAY,EAAEf,eAAe,CAACgB,SAAS;EAEvC,iBAAiB,EAAEhB,eAAe,CAACkB,cAAc;EACjD,YAAY,EAAElB,eAAe,CAACmB,aAAa;EAE3C,UAAU,EAAEnB,eAAe,CAACI,cAAc;EAC1C,cAAc,EAAEJ,eAAe,CAACI,cAAc;EAC9C,WAAW,EAAEJ,eAAe,CAACG,QAAQ;EAErC,WAAW,EAAEH,eAAe,CAACoB,UAAU;EACvC,aAAa,EAAEpB,eAAe,CAACoB,UAAU;EACzC,UAAU,EAAEpB,eAAe,CAACqB,OAAO;EACnC,UAAU,EAAErB,eAAe,CAACsB,OAAO;EAEnC,oBAAoB,EAAEtB,eAAe,CAACU;AACxC,CAAC;AAED,OAAO,MAAMc,WAA6B,GACxCD,QAAuC;AAEzC,OAAO,MAAME,iBAAoC,GAAG;EAClDC,UAAU,EAAE,KAAK;EACjBH,QAAQ,EAAEC,WAAW;EACrBG,MAAM,EAAE;IACNC,IAAI,EAAE;MACJC,KAAK,EAAE,KAAK;MACZC,KAAK,EAAE;IACT;EACF;AACF,CAAC","ignoreList":[]}
@@ -5,3 +5,9 @@
5
5
  * @returns {Record<string, string> | undefined} The merged headers, or undefined if no tracing header is available.
6
6
  */
7
7
  export function applyTraceHeaders(existingHeaders?: Record<string, string> | undefined, header?: string): Record<string, string> | undefined;
8
+ /**
9
+ * Validates if a string conforms to the uuid structure
10
+ * @param {string} str
11
+ * @returns
12
+ */
13
+ export function isValidUUID(str: string): boolean;
@@ -1,4 +1,5 @@
1
1
  import { getTraceId } from '@defra/hapi-tracing';
2
+ import Joi from 'joi';
2
3
  import { config } from "../../config/index.js";
3
4
 
4
5
  /**
@@ -17,4 +18,16 @@ export function applyTraceHeaders(existingHeaders, header = config.get('tracing'
17
18
  } : undefined;
18
19
  return existingHeaders ? Object.assign(existingHeaders, headers) : headers;
19
20
  }
21
+
22
+ /**
23
+ * Validates if a string conforms to the uuid structure
24
+ * @param {string} str
25
+ * @returns
26
+ */
27
+ export function isValidUUID(str) {
28
+ const {
29
+ error
30
+ } = Joi.string().uuid().validate(str);
31
+ return error === undefined;
32
+ }
20
33
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","names":["getTraceId","config","applyTraceHeaders","existingHeaders","header","get","traceId","headers","undefined","Object","assign"],"sources":["../../../src/server/utils/utils.js"],"sourcesContent":["import { getTraceId } from '@defra/hapi-tracing'\n\nimport { config } from '~/src/config/index.js'\n\n/**\n * Returns a set of headers to use in an HTTP request, merging them with any existing headers in options.\n * @param {Record<string, string> | undefined} [existingHeaders] - Optional existing headers to merge with the tracing headers.\n * @param {string} [header] - The tracing header name to use.\n * @returns {Record<string, string> | undefined} The merged headers, or undefined if no tracing header is available.\n */\nexport function applyTraceHeaders(\n existingHeaders,\n header = config.get('tracing').header\n) {\n if (!header) {\n return existingHeaders\n }\n\n const traceId = getTraceId()\n\n const headers = traceId ? { [header]: traceId } : undefined\n\n return existingHeaders ? Object.assign(existingHeaders, headers) : headers\n}\n"],"mappings":"AAAA,SAASA,UAAU,QAAQ,qBAAqB;AAEhD,SAASC,MAAM;;AAEf;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,iBAAiBA,CAC/BC,eAAe,EACfC,MAAM,GAAGH,MAAM,CAACI,GAAG,CAAC,SAAS,CAAC,CAACD,MAAM,EACrC;EACA,IAAI,CAACA,MAAM,EAAE;IACX,OAAOD,eAAe;EACxB;EAEA,MAAMG,OAAO,GAAGN,UAAU,CAAC,CAAC;EAE5B,MAAMO,OAAO,GAAGD,OAAO,GAAG;IAAE,CAACF,MAAM,GAAGE;EAAQ,CAAC,GAAGE,SAAS;EAE3D,OAAOL,eAAe,GAAGM,MAAM,CAACC,MAAM,CAACP,eAAe,EAAEI,OAAO,CAAC,GAAGA,OAAO;AAC5E","ignoreList":[]}
1
+ {"version":3,"file":"utils.js","names":["getTraceId","Joi","config","applyTraceHeaders","existingHeaders","header","get","traceId","headers","undefined","Object","assign","isValidUUID","str","error","string","uuid","validate"],"sources":["../../../src/server/utils/utils.js"],"sourcesContent":["import { getTraceId } from '@defra/hapi-tracing'\nimport Joi from 'joi'\n\nimport { config } from '~/src/config/index.js'\n\n/**\n * Returns a set of headers to use in an HTTP request, merging them with any existing headers in options.\n * @param {Record<string, string> | undefined} [existingHeaders] - Optional existing headers to merge with the tracing headers.\n * @param {string} [header] - The tracing header name to use.\n * @returns {Record<string, string> | undefined} The merged headers, or undefined if no tracing header is available.\n */\nexport function applyTraceHeaders(\n existingHeaders,\n header = config.get('tracing').header\n) {\n if (!header) {\n return existingHeaders\n }\n\n const traceId = getTraceId()\n\n const headers = traceId ? { [header]: traceId } : undefined\n\n return existingHeaders ? Object.assign(existingHeaders, headers) : headers\n}\n\n/**\n * Validates if a string conforms to the uuid structure\n * @param {string} str\n * @returns\n */\nexport function isValidUUID(str) {\n const { error } = Joi.string().uuid().validate(str)\n return error === undefined\n}\n"],"mappings":"AAAA,SAASA,UAAU,QAAQ,qBAAqB;AAChD,OAAOC,GAAG,MAAM,KAAK;AAErB,SAASC,MAAM;;AAEf;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,iBAAiBA,CAC/BC,eAAe,EACfC,MAAM,GAAGH,MAAM,CAACI,GAAG,CAAC,SAAS,CAAC,CAACD,MAAM,EACrC;EACA,IAAI,CAACA,MAAM,EAAE;IACX,OAAOD,eAAe;EACxB;EAEA,MAAMG,OAAO,GAAGP,UAAU,CAAC,CAAC;EAE5B,MAAMQ,OAAO,GAAGD,OAAO,GAAG;IAAE,CAACF,MAAM,GAAGE;EAAQ,CAAC,GAAGE,SAAS;EAE3D,OAAOL,eAAe,GAAGM,MAAM,CAACC,MAAM,CAACP,eAAe,EAAEI,OAAO,CAAC,GAAGA,OAAO;AAC5E;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASI,WAAWA,CAACC,GAAG,EAAE;EAC/B,MAAM;IAAEC;EAAM,CAAC,GAAGb,GAAG,CAACc,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC,CAAC,CAACC,QAAQ,CAACJ,GAAG,CAAC;EACnD,OAAOC,KAAK,KAAKL,SAAS;AAC5B","ignoreList":[]}
@@ -1,6 +1,6 @@
1
1
  import { getTraceId } from '@defra/hapi-tracing';
2
2
  import { config } from "../../config/index.js";
3
- import { applyTraceHeaders } from "./utils.js";
3
+ import { applyTraceHeaders, isValidUUID } from "./utils.js";
4
4
  jest.mock('@defra/hapi-tracing');
5
5
  describe('Header helper functions', () => {
6
6
  it('should include the trace id in the headers if available', () => {
@@ -45,5 +45,41 @@ describe('Header helper functions', () => {
45
45
  const result = applyTraceHeaders(existingHeaders, '');
46
46
  expect(result).toBe(existingHeaders);
47
47
  });
48
+ it.each([{
49
+ uuid: '1f457a37-7b99-452e-8324-df9e041abff2',
50
+ valid: true
51
+ }, {
52
+ uuid: '0c9a2690-9a0c-4a2c-98d7-e9ef95615ac9',
53
+ valid: true
54
+ }, {
55
+ uuid: 'f223de3b-5ae5-44b2-8cee-ea8439adc335',
56
+ valid: true
57
+ }, {
58
+ uuid: '82ecc90c-bc47-4ec5-80af-1a9fc1c4c08c',
59
+ valid: true
60
+ }, {
61
+ uuid: 'd99ff582-ecce-474f-a44b-bc5961d977c5',
62
+ valid: true
63
+ }, {
64
+ uuid: '7afffc8a-81ab-4aa6-a8f5-ecf6a600a781',
65
+ valid: true
66
+ }, {
67
+ uuid: '7afffc8a81ab4aa6a8f5ecf6a600a781',
68
+ valid: true
69
+ }, {
70
+ uuid: '',
71
+ valid: false
72
+ }, {
73
+ uuid: 'uuid',
74
+ valid: false
75
+ }, {
76
+ uuid: 'h4f84ef8-b5e1-4544-94aa-1b671d50d8cb',
77
+ valid: false
78
+ }])('should validate uuid appropriately %s', ({
79
+ uuid,
80
+ valid
81
+ }) => {
82
+ expect(isValidUUID(uuid)).toBe(valid);
83
+ });
48
84
  });
49
85
  //# sourceMappingURL=utils.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.test.js","names":["getTraceId","config","applyTraceHeaders","jest","mock","describe","it","mocked","mockReturnValue","result","expect","toEqual","get","header","toBeUndefined","existingHeaders","Authorization","toBe"],"sources":["../../../src/server/utils/utils.test.js"],"sourcesContent":["import { getTraceId } from '@defra/hapi-tracing'\n\nimport { config } from '~/src/config/index.js'\nimport { applyTraceHeaders } from '~/src/server/utils/utils.js'\n\njest.mock('@defra/hapi-tracing')\n\ndescribe('Header helper functions', () => {\n it('should include the trace id in the headers if available', () => {\n jest.mocked(getTraceId).mockReturnValue('my-trace-id')\n\n const result = applyTraceHeaders() // Updated to applyTraceHeaders\n expect(result).toEqual({\n [config.get('tracing').header]: 'my-trace-id'\n })\n })\n\n it('should exclude the trace id in the headers if missing', () => {\n jest.mocked(getTraceId).mockReturnValue(null)\n\n const result = applyTraceHeaders() // Updated to applyTraceHeaders\n expect(result).toBeUndefined()\n })\n\n it('should merge existing headers with the trace id if available', () => {\n jest.mocked(getTraceId).mockReturnValue('my-trace-id')\n\n const existingHeaders = { Authorization: 'Bearer token' }\n const result = applyTraceHeaders(existingHeaders) // Updated to applyTraceHeaders\n\n expect(result).toEqual({\n Authorization: 'Bearer token',\n [config.get('tracing').header]: 'my-trace-id'\n })\n })\n\n it('should return existing headers without modification if trace id is missing', () => {\n jest.mocked(getTraceId).mockReturnValue(null)\n\n const existingHeaders = { Authorization: 'Bearer token' }\n const result = applyTraceHeaders(existingHeaders) // Updated to applyTraceHeaders\n\n expect(result).toEqual({\n Authorization: 'Bearer token'\n })\n })\n\n it('should return existing headers if tracing header configuration is missing', () => {\n const existingHeaders = { Authorization: 'Bearer token' }\n const result = applyTraceHeaders(existingHeaders, '')\n\n expect(result).toBe(existingHeaders)\n })\n})\n"],"mappings":"AAAA,SAASA,UAAU,QAAQ,qBAAqB;AAEhD,SAASC,MAAM;AACf,SAASC,iBAAiB;AAE1BC,IAAI,CAACC,IAAI,CAAC,qBAAqB,CAAC;AAEhCC,QAAQ,CAAC,yBAAyB,EAAE,MAAM;EACxCC,EAAE,CAAC,yDAAyD,EAAE,MAAM;IAClEH,IAAI,CAACI,MAAM,CAACP,UAAU,CAAC,CAACQ,eAAe,CAAC,aAAa,CAAC;IAEtD,MAAMC,MAAM,GAAGP,iBAAiB,CAAC,CAAC,EAAC;IACnCQ,MAAM,CAACD,MAAM,CAAC,CAACE,OAAO,CAAC;MACrB,CAACV,MAAM,CAACW,GAAG,CAAC,SAAS,CAAC,CAACC,MAAM,GAAG;IAClC,CAAC,CAAC;EACJ,CAAC,CAAC;EAEFP,EAAE,CAAC,uDAAuD,EAAE,MAAM;IAChEH,IAAI,CAACI,MAAM,CAACP,UAAU,CAAC,CAACQ,eAAe,CAAC,IAAI,CAAC;IAE7C,MAAMC,MAAM,GAAGP,iBAAiB,CAAC,CAAC,EAAC;IACnCQ,MAAM,CAACD,MAAM,CAAC,CAACK,aAAa,CAAC,CAAC;EAChC,CAAC,CAAC;EAEFR,EAAE,CAAC,8DAA8D,EAAE,MAAM;IACvEH,IAAI,CAACI,MAAM,CAACP,UAAU,CAAC,CAACQ,eAAe,CAAC,aAAa,CAAC;IAEtD,MAAMO,eAAe,GAAG;MAAEC,aAAa,EAAE;IAAe,CAAC;IACzD,MAAMP,MAAM,GAAGP,iBAAiB,CAACa,eAAe,CAAC,EAAC;;IAElDL,MAAM,CAACD,MAAM,CAAC,CAACE,OAAO,CAAC;MACrBK,aAAa,EAAE,cAAc;MAC7B,CAACf,MAAM,CAACW,GAAG,CAAC,SAAS,CAAC,CAACC,MAAM,GAAG;IAClC,CAAC,CAAC;EACJ,CAAC,CAAC;EAEFP,EAAE,CAAC,4EAA4E,EAAE,MAAM;IACrFH,IAAI,CAACI,MAAM,CAACP,UAAU,CAAC,CAACQ,eAAe,CAAC,IAAI,CAAC;IAE7C,MAAMO,eAAe,GAAG;MAAEC,aAAa,EAAE;IAAe,CAAC;IACzD,MAAMP,MAAM,GAAGP,iBAAiB,CAACa,eAAe,CAAC,EAAC;;IAElDL,MAAM,CAACD,MAAM,CAAC,CAACE,OAAO,CAAC;MACrBK,aAAa,EAAE;IACjB,CAAC,CAAC;EACJ,CAAC,CAAC;EAEFV,EAAE,CAAC,2EAA2E,EAAE,MAAM;IACpF,MAAMS,eAAe,GAAG;MAAEC,aAAa,EAAE;IAAe,CAAC;IACzD,MAAMP,MAAM,GAAGP,iBAAiB,CAACa,eAAe,EAAE,EAAE,CAAC;IAErDL,MAAM,CAACD,MAAM,CAAC,CAACQ,IAAI,CAACF,eAAe,CAAC;EACtC,CAAC,CAAC;AACJ,CAAC,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"utils.test.js","names":["getTraceId","config","applyTraceHeaders","isValidUUID","jest","mock","describe","it","mocked","mockReturnValue","result","expect","toEqual","get","header","toBeUndefined","existingHeaders","Authorization","toBe","each","uuid","valid"],"sources":["../../../src/server/utils/utils.test.js"],"sourcesContent":["import { getTraceId } from '@defra/hapi-tracing'\n\nimport { config } from '~/src/config/index.js'\nimport { applyTraceHeaders, isValidUUID } from '~/src/server/utils/utils.js'\n\njest.mock('@defra/hapi-tracing')\n\ndescribe('Header helper functions', () => {\n it('should include the trace id in the headers if available', () => {\n jest.mocked(getTraceId).mockReturnValue('my-trace-id')\n\n const result = applyTraceHeaders() // Updated to applyTraceHeaders\n expect(result).toEqual({\n [config.get('tracing').header]: 'my-trace-id'\n })\n })\n\n it('should exclude the trace id in the headers if missing', () => {\n jest.mocked(getTraceId).mockReturnValue(null)\n\n const result = applyTraceHeaders() // Updated to applyTraceHeaders\n expect(result).toBeUndefined()\n })\n\n it('should merge existing headers with the trace id if available', () => {\n jest.mocked(getTraceId).mockReturnValue('my-trace-id')\n\n const existingHeaders = { Authorization: 'Bearer token' }\n const result = applyTraceHeaders(existingHeaders) // Updated to applyTraceHeaders\n\n expect(result).toEqual({\n Authorization: 'Bearer token',\n [config.get('tracing').header]: 'my-trace-id'\n })\n })\n\n it('should return existing headers without modification if trace id is missing', () => {\n jest.mocked(getTraceId).mockReturnValue(null)\n\n const existingHeaders = { Authorization: 'Bearer token' }\n const result = applyTraceHeaders(existingHeaders) // Updated to applyTraceHeaders\n\n expect(result).toEqual({\n Authorization: 'Bearer token'\n })\n })\n\n it('should return existing headers if tracing header configuration is missing', () => {\n const existingHeaders = { Authorization: 'Bearer token' }\n const result = applyTraceHeaders(existingHeaders, '')\n\n expect(result).toBe(existingHeaders)\n })\n\n it.each([\n { uuid: '1f457a37-7b99-452e-8324-df9e041abff2', valid: true },\n { uuid: '0c9a2690-9a0c-4a2c-98d7-e9ef95615ac9', valid: true },\n { uuid: 'f223de3b-5ae5-44b2-8cee-ea8439adc335', valid: true },\n { uuid: '82ecc90c-bc47-4ec5-80af-1a9fc1c4c08c', valid: true },\n { uuid: 'd99ff582-ecce-474f-a44b-bc5961d977c5', valid: true },\n { uuid: '7afffc8a-81ab-4aa6-a8f5-ecf6a600a781', valid: true },\n { uuid: '7afffc8a81ab4aa6a8f5ecf6a600a781', valid: true },\n { uuid: '', valid: false },\n { uuid: 'uuid', valid: false },\n { uuid: 'h4f84ef8-b5e1-4544-94aa-1b671d50d8cb', valid: false }\n ])('should validate uuid appropriately %s', ({ uuid, valid }) => {\n expect(isValidUUID(uuid)).toBe(valid)\n })\n})\n"],"mappings":"AAAA,SAASA,UAAU,QAAQ,qBAAqB;AAEhD,SAASC,MAAM;AACf,SAASC,iBAAiB,EAAEC,WAAW;AAEvCC,IAAI,CAACC,IAAI,CAAC,qBAAqB,CAAC;AAEhCC,QAAQ,CAAC,yBAAyB,EAAE,MAAM;EAC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{
2
2
  "name": "@defra/forms-engine-plugin",
3
- "version": "4.5.6",
3
+ "version": "4.6.0",
4
4
  "description": "Defra forms engine",
5
5
  "type": "module",
6
6
  "files": [
@@ -83,7 +83,7 @@
83
83
  },
84
84
  "license": "SEE LICENSE IN LICENSE",
85
85
  "dependencies": {
86
- "@defra/forms-model": "^3.0.637",
86
+ "@defra/forms-model": "^3.0.644",
87
87
  "@defra/hapi-tracing": "^1.29.0",
88
88
  "@defra/interactive-map": "^0.0.17-alpha",
89
89
  "@elastic/ecs-pino-format": "^1.5.0",
@@ -1,5 +1,8 @@
1
- import { type EmailAddressFieldComponent } from '@defra/forms-model'
2
- import joi from 'joi'
1
+ import {
2
+ preventUnicodeInEmail,
3
+ type EmailAddressFieldComponent
4
+ } from '@defra/forms-model'
5
+ import joi, { type CustomHelpers } from 'joi'
3
6
 
4
7
  import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
5
8
  import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
@@ -20,7 +23,15 @@ export class EmailAddressField extends FormComponent {
20
23
 
21
24
  const { options } = def
22
25
 
23
- let formSchema = joi.string().email().trim().label(this.label).required()
26
+ let formSchema = joi
27
+ .string()
28
+ .trim()
29
+ .email()
30
+ .custom((value, helpers: CustomHelpers<string>) =>
31
+ preventUnicodeInEmail(value, helpers)
32
+ )
33
+ .label(this.label)
34
+ .required()
24
35
 
25
36
  if (options.required === false) {
26
37
  formSchema = formSchema.allow('')
@@ -69,7 +80,8 @@ export class EmailAddressField extends FormComponent {
69
80
  return {
70
81
  baseErrors: [
71
82
  { type: 'required', template: messageTemplate.required },
72
- { type: 'format', template: messageTemplate.format }
83
+ { type: 'format', template: messageTemplate.format },
84
+ { type: 'unicode', template: messageTemplate.unicode }
73
85
  ],
74
86
  advancedSettingsErrors: []
75
87
  }
@@ -1,5 +1,4 @@
1
1
  import { ControllerType, getHiddenFields } from '@defra/forms-model'
2
- import { validate as isValidUUID } from 'uuid'
3
2
 
4
3
  import { getCacheService } from '~/src/server/plugins/engine/helpers.js'
5
4
  import {
@@ -16,6 +15,7 @@ import {
16
15
  } from '~/src/server/plugins/engine/types.js'
17
16
  import { type FormQuery } from '~/src/server/routes/types.js'
18
17
  import { type Services } from '~/src/server/types.js'
18
+ import { isValidUUID } from '~/src/server/utils/utils.js'
19
19
 
20
20
  const GUID_LENGTH = 36
21
21
 
@@ -44,6 +44,7 @@ export const messageTemplate: Record<string, JoiExpression> = {
44
44
  'Enter {{lowerFirst(#label)}} in the correct format',
45
45
  opts
46
46
  ) as JoiExpression,
47
+ unicode: '{{#label}} includes invalid characters, for example, long dashes',
47
48
  number: '{{#label}} must be a number',
48
49
  numberPrecision: '{{#label}} must have {{#limit}} or fewer decimal places',
49
50
  numberInteger: '{{#label}} must be a whole number',
@@ -69,6 +70,7 @@ export const messages: LanguageMessagesExt = {
69
70
  'string.empty': messageTemplate.required,
70
71
  'string.max': messageTemplate.max,
71
72
  'string.email': messageTemplate.format,
73
+ 'string.unicode': messageTemplate.unicode,
72
74
  'string.pattern.base': messageTemplate.pattern,
73
75
  'string.maxWords': messageTemplate.maxWords,
74
76
 
@@ -1,4 +1,5 @@
1
1
  import { getTraceId } from '@defra/hapi-tracing'
2
+ import Joi from 'joi'
2
3
 
3
4
  import { config } from '~/src/config/index.js'
4
5
 
@@ -22,3 +23,13 @@ export function applyTraceHeaders(
22
23
 
23
24
  return existingHeaders ? Object.assign(existingHeaders, headers) : headers
24
25
  }
26
+
27
+ /**
28
+ * Validates if a string conforms to the uuid structure
29
+ * @param {string} str
30
+ * @returns
31
+ */
32
+ export function isValidUUID(str) {
33
+ const { error } = Joi.string().uuid().validate(str)
34
+ return error === undefined
35
+ }
@@ -1,7 +1,7 @@
1
1
  import { getTraceId } from '@defra/hapi-tracing'
2
2
 
3
3
  import { config } from '~/src/config/index.js'
4
- import { applyTraceHeaders } from '~/src/server/utils/utils.js'
4
+ import { applyTraceHeaders, isValidUUID } from '~/src/server/utils/utils.js'
5
5
 
6
6
  jest.mock('@defra/hapi-tracing')
7
7
 
@@ -51,4 +51,19 @@ describe('Header helper functions', () => {
51
51
 
52
52
  expect(result).toBe(existingHeaders)
53
53
  })
54
+
55
+ it.each([
56
+ { uuid: '1f457a37-7b99-452e-8324-df9e041abff2', valid: true },
57
+ { uuid: '0c9a2690-9a0c-4a2c-98d7-e9ef95615ac9', valid: true },
58
+ { uuid: 'f223de3b-5ae5-44b2-8cee-ea8439adc335', valid: true },
59
+ { uuid: '82ecc90c-bc47-4ec5-80af-1a9fc1c4c08c', valid: true },
60
+ { uuid: 'd99ff582-ecce-474f-a44b-bc5961d977c5', valid: true },
61
+ { uuid: '7afffc8a-81ab-4aa6-a8f5-ecf6a600a781', valid: true },
62
+ { uuid: '7afffc8a81ab4aa6a8f5ecf6a600a781', valid: true },
63
+ { uuid: '', valid: false },
64
+ { uuid: 'uuid', valid: false },
65
+ { uuid: 'h4f84ef8-b5e1-4544-94aa-1b671d50d8cb', valid: false }
66
+ ])('should validate uuid appropriately %s', ({ uuid, valid }) => {
67
+ expect(isValidUUID(uuid)).toBe(valid)
68
+ })
54
69
  })