@defra/forms-engine-plugin 0.1.20 → 0.1.22
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/server/plugins/engine/components/DatePartsField.d.ts +4 -0
- package/.server/server/plugins/engine/components/DatePartsField.js +7 -0
- package/.server/server/plugins/engine/components/DatePartsField.js.map +1 -1
- package/.server/server/plugins/engine/components/EmailAddressField.d.ts +4 -0
- package/.server/server/plugins/engine/components/EmailAddressField.js +7 -0
- package/.server/server/plugins/engine/components/EmailAddressField.js.map +1 -1
- package/.server/server/plugins/engine/components/FileUploadField.d.ts +4 -0
- package/.server/server/plugins/engine/components/FileUploadField.js +7 -0
- package/.server/server/plugins/engine/components/FileUploadField.js.map +1 -1
- package/.server/server/plugins/engine/components/FormComponent.d.ts +7 -0
- package/.server/server/plugins/engine/components/FormComponent.js +11 -0
- package/.server/server/plugins/engine/components/FormComponent.js.map +1 -1
- package/.server/server/plugins/engine/components/ListFormComponent.d.ts +4 -0
- package/.server/server/plugins/engine/components/ListFormComponent.js +7 -0
- package/.server/server/plugins/engine/components/ListFormComponent.js.map +1 -1
- package/.server/server/plugins/engine/components/MonthYearField.d.ts +4 -0
- package/.server/server/plugins/engine/components/MonthYearField.js +7 -0
- package/.server/server/plugins/engine/components/MonthYearField.js.map +1 -1
- package/.server/server/plugins/engine/components/MultilineTextField.d.ts +4 -0
- package/.server/server/plugins/engine/components/MultilineTextField.js +14 -3
- package/.server/server/plugins/engine/components/MultilineTextField.js.map +1 -1
- package/.server/server/plugins/engine/components/NumberField.d.ts +4 -0
- package/.server/server/plugins/engine/components/NumberField.js +7 -0
- package/.server/server/plugins/engine/components/NumberField.js.map +1 -1
- package/.server/server/plugins/engine/components/SelectionControlField.d.ts +4 -0
- package/.server/server/plugins/engine/components/SelectionControlField.js +7 -0
- package/.server/server/plugins/engine/components/SelectionControlField.js.map +1 -1
- package/.server/server/plugins/engine/components/TelephoneNumberField.d.ts +4 -0
- package/.server/server/plugins/engine/components/TelephoneNumberField.js +7 -0
- package/.server/server/plugins/engine/components/TelephoneNumberField.js.map +1 -1
- package/.server/server/plugins/engine/components/TextField.d.ts +4 -0
- package/.server/server/plugins/engine/components/TextField.js +7 -0
- package/.server/server/plugins/engine/components/TextField.js.map +1 -1
- package/.server/server/plugins/engine/components/UkAddressField.d.ts +4 -0
- package/.server/server/plugins/engine/components/UkAddressField.js +7 -0
- package/.server/server/plugins/engine/components/UkAddressField.js.map +1 -1
- package/.server/server/plugins/engine/components/YesNoField.d.ts +4 -0
- package/.server/server/plugins/engine/components/YesNoField.js +7 -0
- package/.server/server/plugins/engine/components/YesNoField.js.map +1 -1
- package/package.json +1 -1
- package/src/server/plugins/engine/components/DatePartsField.test.ts +24 -1
- package/src/server/plugins/engine/components/DatePartsField.ts +7 -0
- package/src/server/plugins/engine/components/EmailAddressField.ts +7 -0
- package/src/server/plugins/engine/components/FileUploadField.ts +7 -0
- package/src/server/plugins/engine/components/FormComponent.ts +10 -0
- package/src/server/plugins/engine/components/ListFormComponent.ts +7 -0
- package/src/server/plugins/engine/components/MonthYearField.ts +7 -0
- package/src/server/plugins/engine/components/MultilineTextField.ts +16 -3
- package/src/server/plugins/engine/components/NumberField.ts +7 -0
- package/src/server/plugins/engine/components/SelectionControlField.ts +7 -0
- package/src/server/plugins/engine/components/TelephoneNumberField.ts +7 -0
- package/src/server/plugins/engine/components/TextField.ts +7 -0
- package/src/server/plugins/engine/components/UkAddressField.ts +7 -0
- package/src/server/plugins/engine/components/YesNoField.ts +7 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultilineTextField.js","names":["Joi","FormComponent","messageTemplate","MultilineTextField","isCharacterOrWordCount","constructor","def","props","schema","options","formSchema","string","trim","label","required","allow","length","max","min","maxWords","custom","getValidatorMaxWords","regex","pattern","RegExp","customValidationMessage","message","messages","customValidationMessages","minMaxErrorText","buildMinMaxText","ruleset","default","stateSchema","getViewModel","payload","errors","viewModel","maxlength","maxwords","rows","minMaxError","minMax","replace","toString","getAllPossibleErrors","baseErrors","type","template","advancedSettingsErrors","component","validator","value","helpers","limit","count","error","text","tokens","match"],"sources":["../../../../../src/server/plugins/engine/components/MultilineTextField.ts"],"sourcesContent":["import { type MultilineTextFieldComponent } from '@defra/forms-model'\nimport Joi, { type CustomValidator, type StringSchema } from 'joi'\n\nimport { type ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'\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 MultilineTextField extends FormComponent {\n declare options: MultilineTextFieldComponent['options']\n declare schema: MultilineTextFieldComponent['schema']\n declare formSchema: StringSchema\n declare stateSchema: StringSchema\n\n isCharacterOrWordCount = false\n\n constructor(\n def: MultilineTextFieldComponent,\n props: ConstructorParameters<typeof ComponentBase>[1]\n ) {\n super(def, props)\n\n const { schema, options } = def\n\n let formSchema = Joi.string().trim().label(this.label).required()\n\n if (options.required === false) {\n formSchema = formSchema.allow('')\n }\n\n if (typeof schema.length !== 'number') {\n if (typeof schema.max === 'number') {\n formSchema = formSchema.max(schema.max)\n this.isCharacterOrWordCount = true\n }\n\n if (typeof schema.min === 'number') {\n formSchema = formSchema.min(schema.min)\n }\n } else {\n formSchema = formSchema.length(schema.length)\n }\n\n if (typeof options.maxWords === 'number') {\n formSchema = formSchema.custom(\n getValidatorMaxWords(this),\n 'max words validation'\n )\n\n this.isCharacterOrWordCount = true\n }\n\n if (schema.regex) {\n const pattern = new RegExp(schema.regex)\n formSchema = formSchema.pattern(pattern)\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.max': message,\n 'string.min': message,\n 'string.length': message,\n 'string.pattern.base': message,\n 'string.maxWords': message\n })\n } else if (options.customValidationMessages) {\n formSchema = formSchema.messages(options.customValidationMessages)\n } else if (\n typeof schema.max === 'number' &&\n typeof schema.min === 'number'\n ) {\n const minMaxErrorText = this.buildMinMaxText(schema.min, schema.max)\n formSchema = formSchema.ruleset\n .min(schema.min)\n .max(schema.max)\n .message(minMaxErrorText)\n }\n\n this.formSchema = formSchema.default('')\n this.stateSchema = formSchema.default(null).allow(null)\n this.options = options\n this.schema = schema\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { schema, options, isCharacterOrWordCount } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { maxlength, maxwords, rows } = viewModel\n\n if (schema.max) {\n maxlength = schema.max\n }\n\n if (options.maxWords) {\n maxwords = options.maxWords\n }\n\n if (options.rows) {\n rows = options.rows\n }\n\n return {\n ...viewModel,\n isCharacterOrWordCount,\n maxlength,\n maxwords,\n rows\n }\n }\n\n buildMinMaxText(min?: number, max?: number): string {\n const minMaxError = messageTemplate.minMax as string\n return minMaxError\n .replace('{{#min}}', min ? min.toString() : '[min length]')\n .replace('{{#max}}', max ? max.toString() : '[max length]')\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [{ type: 'required', template: messageTemplate.required }],\n advancedSettingsErrors: [\n { type: 'min', template: messageTemplate.min },\n { type: 'max', template: messageTemplate.max },\n {\n type: 'minMax',\n template: this.buildMinMaxText(this.schema.min, this.schema.max)\n }\n ]\n }\n }\n}\n\nfunction getValidatorMaxWords(component: MultilineTextField) {\n const validator: CustomValidator = (value: string, helpers) => {\n const { options } = component\n\n const {\n customValidationMessage: custom,\n maxWords: limit // See {{#limit}} variable\n } = options\n\n if (!limit || count(value) <= limit) {\n return value\n }\n\n return custom\n ? helpers.message({ custom }, { limit })\n : helpers.error('string.maxWords', { limit })\n }\n\n /**\n * Count the number of words in the given text\n * @see GOV.UK Frontend {@link https://github.com/alphagov/govuk-frontend/blob/v5.4.0/packages/govuk-frontend/src/govuk/components/character-count/character-count.mjs#L343 | Character count `maxwords` implementation}\n */\n function count(text: string) {\n const tokens = text.match(/\\S+/g) ?? []\n return tokens.length\n }\n\n return validator\n}\n"],"mappings":"AACA,OAAOA,GAAG,MAAmD,KAAK;AAGlE,SAASC,aAAa;AACtB,SAASC,eAAe;AAOxB,OAAO,MAAMC,kBAAkB,SAASF,aAAa,CAAC;EAMpDG,sBAAsB,GAAG,KAAK;EAE9BC,WAAWA,CACTC,GAAgC,EAChCC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC,MAAM;MAAEC;IAAQ,CAAC,GAAGH,GAAG;IAE/B,IAAII,UAAU,GAAGV,GAAG,CAACW,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC,CAAC,CAACC,KAAK,CAAC,IAAI,CAACA,KAAK,CAAC,CAACC,QAAQ,CAAC,CAAC;IAEjE,IAAIL,OAAO,CAACK,QAAQ,KAAK,KAAK,EAAE;MAC9BJ,UAAU,GAAGA,UAAU,CAACK,KAAK,CAAC,EAAE,CAAC;IACnC;IAEA,IAAI,OAAOP,MAAM,CAACQ,MAAM,KAAK,QAAQ,EAAE;MACrC,IAAI,OAAOR,MAAM,CAACS,GAAG,KAAK,QAAQ,EAAE;QAClCP,UAAU,GAAGA,UAAU,CAACO,GAAG,CAACT,MAAM,CAACS,GAAG,CAAC;QACvC,IAAI,CAACb,sBAAsB,GAAG,IAAI;MACpC;MAEA,IAAI,OAAOI,MAAM,CAACU,GAAG,KAAK,QAAQ,EAAE;QAClCR,UAAU,GAAGA,UAAU,CAACQ,GAAG,CAACV,MAAM,CAACU,GAAG,CAAC;MACzC;IACF,CAAC,MAAM;MACLR,UAAU,GAAGA,UAAU,CAACM,MAAM,CAACR,MAAM,CAACQ,MAAM,CAAC;IAC/C;IAEA,IAAI,OAAOP,OAAO,CAACU,QAAQ,KAAK,QAAQ,EAAE;MACxCT,UAAU,GAAGA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Ae,CAACY;MAAS,CAAC,CAAC;MACtEmC,sBAAsB,EAAE,CACtB;QAAEF,IAAI,EAAE,KAAK;QAAEC,QAAQ,EAAE9C,eAAe,CAACgB;MAAI,CAAC,EAC9C;QAAE6B,IAAI,EAAE,KAAK;QAAEC,QAAQ,EAAE9C,eAAe,CAACe;MAAI,CAAC,EAC9C;QACE8B,IAAI,EAAE,QAAQ;QACdC,QAAQ,EAAE,IAAI,CAAClB,eAAe,CAAC,IAAI,CAACtB,MAAM,CAACU,GAAG,EAAE,IAAI,CAACV,MAAM,CAACS,GAAG;MACjE,CAAC;IAEL,CAAC;EACH;AACF;AAEA,SAASI,oBAAoBA,CAAC6B,SAA6B,EAAE;EAC3D,MAAMC,SAA0B,GAAGA,CAACC,KAAa,EAAEC,OAAO,KAAK;IAC7D,MAAM;MAAE5C;IAAQ,CAAC,GAAGyC,SAAS;IAE7B,MAAM;MACJzB,uBAAuB,EAAEL,MAAM;MAC/BD,QAAQ,EAAEmC,KAAK,CAAC;IAClB,CAAC,GAAG7C,OAAO;IAEX,IAAI,CAAC6C,KAAK,IAAIC,KAAK,CAACH,KAAK,CAAC,IAAIE,KAAK,EAAE;MACnC,OAAOF,KAAK;IACd;IAEA,OAAOhC,MAAM,GACTiC,OAAO,CAAC3B,OAAO,CAAC;MAAEN;IAAO,CAAC,EAAE;MAAEkC;IAAM,CAAC,CAAC,GACtCD,OAAO,CAACG,KAAK,CAAC,iBAAiB,EAAE;MAAEF;IAAM,CAAC,CAAC;EACjD,CAAC;;EAED;AACF;AACA;AACA;EACE,SAASC,KAAKA,CAACE,IAAY,EAAE;IAC3B,MAAMC,MAAM,GAAGD,IAAI,CAACE,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE;IACvC,OAAOD,MAAM,CAAC1C,MAAM;EACtB;EAEA,OAAOmC,SAAS;AAClB","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"MultilineTextField.js","names":["Joi","FormComponent","messageTemplate","MultilineTextField","isCharacterOrWordCount","constructor","def","props","schema","options","formSchema","string","trim","label","required","allow","length","max","min","maxWords","custom","getValidatorMaxWords","regex","pattern","RegExp","customValidationMessage","message","messages","customValidationMessages","minMaxErrorText","buildMinMaxText","ruleset","default","stateSchema","getViewModel","payload","errors","viewModel","maxlength","maxwords","rows","minMaxError","minMax","replace","toString","getAllPossibleErrors","staticErrors","advancedSettingsErrors","type","template","baseErrors","component","validator","value","helpers","limit","count","error","text","tokens","match"],"sources":["../../../../../src/server/plugins/engine/components/MultilineTextField.ts"],"sourcesContent":["import { type MultilineTextFieldComponent } from '@defra/forms-model'\nimport Joi, { type CustomValidator, type StringSchema } from 'joi'\n\nimport { type ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'\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 MultilineTextField extends FormComponent {\n declare options: MultilineTextFieldComponent['options']\n declare schema: MultilineTextFieldComponent['schema']\n declare formSchema: StringSchema\n declare stateSchema: StringSchema\n\n isCharacterOrWordCount = false\n\n constructor(\n def: MultilineTextFieldComponent,\n props: ConstructorParameters<typeof ComponentBase>[1]\n ) {\n super(def, props)\n\n const { schema, options } = def\n\n let formSchema = Joi.string().trim().label(this.label).required()\n\n if (options.required === false) {\n formSchema = formSchema.allow('')\n }\n\n if (typeof schema.length !== 'number') {\n if (typeof schema.max === 'number') {\n formSchema = formSchema.max(schema.max)\n this.isCharacterOrWordCount = true\n }\n\n if (typeof schema.min === 'number') {\n formSchema = formSchema.min(schema.min)\n }\n } else {\n formSchema = formSchema.length(schema.length)\n }\n\n if (typeof options.maxWords === 'number') {\n formSchema = formSchema.custom(\n getValidatorMaxWords(this),\n 'max words validation'\n )\n\n this.isCharacterOrWordCount = true\n }\n\n if (schema.regex) {\n const pattern = new RegExp(schema.regex)\n formSchema = formSchema.pattern(pattern)\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.max': message,\n 'string.min': message,\n 'string.length': message,\n 'string.pattern.base': message,\n 'string.maxWords': message\n })\n } else if (options.customValidationMessages) {\n formSchema = formSchema.messages(options.customValidationMessages)\n } else if (\n typeof schema.max === 'number' &&\n typeof schema.min === 'number'\n ) {\n const minMaxErrorText = this.buildMinMaxText(schema.min, schema.max)\n formSchema = formSchema.ruleset\n .min(schema.min)\n .max(schema.max)\n .message(minMaxErrorText)\n }\n\n this.formSchema = formSchema.default('')\n this.stateSchema = formSchema.default(null).allow(null)\n this.options = options\n this.schema = schema\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { schema, options, isCharacterOrWordCount } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { maxlength, maxwords, rows } = viewModel\n\n if (schema.max) {\n maxlength = schema.max\n }\n\n if (options.maxWords) {\n maxwords = options.maxWords\n }\n\n if (options.rows) {\n rows = options.rows\n }\n\n return {\n ...viewModel,\n isCharacterOrWordCount,\n maxlength,\n maxwords,\n rows\n }\n }\n\n buildMinMaxText(min?: number, max?: number): string {\n const minMaxError = messageTemplate.minMax as string\n return minMaxError\n .replace('{{#min}}', min ? min.toString() : '[min length]')\n .replace('{{#max}}', max ? max.toString() : '[max length]')\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n const staticErrors = MultilineTextField.getAllPossibleErrors()\n return {\n ...staticErrors,\n advancedSettingsErrors: [\n ...staticErrors.advancedSettingsErrors,\n {\n type: 'minMax',\n template: this.buildMinMaxText(this.schema.min, this.schema.max)\n }\n ]\n }\n }\n\n /**\n * Static version of getAllPossibleErrors that doesn't require a component instance.\n */\n static getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [{ type: 'required', template: messageTemplate.required }],\n advancedSettingsErrors: [\n { type: 'min', template: messageTemplate.min },\n { type: 'max', template: messageTemplate.max }\n ]\n }\n }\n}\n\nfunction getValidatorMaxWords(component: MultilineTextField) {\n const validator: CustomValidator = (value: string, helpers) => {\n const { options } = component\n\n const {\n customValidationMessage: custom,\n maxWords: limit // See {{#limit}} variable\n } = options\n\n if (!limit || count(value) <= limit) {\n return value\n }\n\n return custom\n ? helpers.message({ custom }, { limit })\n : helpers.error('string.maxWords', { limit })\n }\n\n /**\n * Count the number of words in the given text\n * @see GOV.UK Frontend {@link https://github.com/alphagov/govuk-frontend/blob/v5.4.0/packages/govuk-frontend/src/govuk/components/character-count/character-count.mjs#L343 | Character count `maxwords` implementation}\n */\n function count(text: string) {\n const tokens = text.match(/\\S+/g) ?? []\n return tokens.length\n }\n\n return validator\n}\n"],"mappings":"AACA,OAAOA,GAAG,MAAmD,KAAK;AAGlE,SAASC,aAAa;AACtB,SAASC,eAAe;AAOxB,OAAO,MAAMC,kBAAkB,SAASF,aAAa,CAAC;EAMpDG,sBAAsB,GAAG,KAAK;EAE9BC,WAAWA,CACTC,GAAgC,EAChCC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC,MAAM;MAAEC;IAAQ,CAAC,GAAGH,GAAG;IAE/B,IAAII,UAAU,GAAGV,GAAG,CAACW,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC,CAAC,CAACC,KAAK,CAAC,IAAI,CAACA,KAAK,CAAC,CAACC,QAAQ,CAAC,CAAC;IAEjE,IAAIL,OAAO,CAACK,QAAQ,KAAK,KAAK,EAAE;MAC9BJ,UAAU,GAAGA,UAAU,CAACK,KAAK,CAAC,EAAE,CAAC;IACnC;IAEA,IAAI,OAAOP,MAAM,CAACQ,MAAM,KAAK,QAAQ,EAAE;MACrC,IAAI,OAAOR,MAAM,CAACS,GAAG,KAAK,QAAQ,EAAE;QAClCP,UAAU,GAAGA,UAAU,CAACO,GAAG,CAACT,MAAM,CA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X,IAAI,CAAC8C,KAAK,IAAIC,KAAK,CAACH,KAAK,CAAC,IAAIE,KAAK,EAAE;MACnC,OAAOF,KAAK;IACd;IAEA,OAAOjC,MAAM,GACTkC,OAAO,CAAC5B,OAAO,CAAC;MAAEN;IAAO,CAAC,EAAE;MAAEmC;IAAM,CAAC,CAAC,GACtCD,OAAO,CAACG,KAAK,CAAC,iBAAiB,EAAE;MAAEF;IAAM,CAAC,CAAC;EACjD,CAAC;;EAED;AACF;AACA;AACA;EACE,SAASC,KAAKA,CAACE,IAAY,EAAE;IAC3B,MAAMC,MAAM,GAAGD,IAAI,CAACE,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE;IACvC,OAAOD,MAAM,CAAC3C,MAAM;EACtB;EAEA,OAAOoC,SAAS;AAClB","ignoreList":[]}
|
|
@@ -63,6 +63,10 @@ export declare class NumberField extends FormComponent {
|
|
|
63
63
|
* For error preview page that shows all possible errors on a component
|
|
64
64
|
*/
|
|
65
65
|
getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
66
|
+
/**
|
|
67
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
68
|
+
*/
|
|
69
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
66
70
|
static isNumber(value?: FormStateValue | FormState): value is number;
|
|
67
71
|
}
|
|
68
72
|
export declare function getValidatorPrecision(component: NumberField): joi.CustomValidator;
|
|
@@ -103,6 +103,13 @@ export class NumberField extends FormComponent {
|
|
|
103
103
|
* For error preview page that shows all possible errors on a component
|
|
104
104
|
*/
|
|
105
105
|
getAllPossibleErrors() {
|
|
106
|
+
return NumberField.getAllPossibleErrors();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
111
|
+
*/
|
|
112
|
+
static getAllPossibleErrors() {
|
|
106
113
|
return {
|
|
107
114
|
baseErrors: [{
|
|
108
115
|
type: 'required',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NumberField.js","names":["joi","FormComponent","isFormValue","messageTemplate","NumberField","constructor","def","props","options","schema","formSchema","number","custom","getValidatorPrecision","label","required","allow","messages","customValidationMessages","empty","min","max","precision","integer","customValidationMessage","message","default","stateSchema","getFormValueFromState","state","name","getFormValue","value","isValue","undefined","getViewModel","payload","errors","viewModel","attributes","prefix","suffix","inputmode","text","isNumber","getAllPossibleErrors","baseErrors","type","template","numberInteger","advancedSettingsErrors","numberMin","numberMax","numberPrecision","component","validator","helpers","limit","validationSchema","prefs","convert","attempt","error"],"sources":["../../../../../src/server/plugins/engine/components/NumberField.ts"],"sourcesContent":["import { type NumberFieldComponent } from '@defra/forms-model'\nimport joi, { type CustomValidator, type NumberSchema } from 'joi'\n\nimport {\n FormComponent,\n isFormValue\n} 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 FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState\n} from '~/src/server/plugins/engine/types.js'\n\nexport class NumberField extends FormComponent {\n declare options: NumberFieldComponent['options']\n declare schema: NumberFieldComponent['schema']\n declare formSchema: NumberSchema\n declare stateSchema: NumberSchema\n\n constructor(\n def: NumberFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { options, schema } = def\n\n let formSchema = joi\n .number()\n .custom(getValidatorPrecision(this))\n .label(this.label)\n .required()\n\n if (options.required === false) {\n formSchema = formSchema.allow('')\n } else {\n const messages = options.customValidationMessages\n\n formSchema = formSchema.empty('').messages({\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n 'any.required':\n messages?.['any.required'] ?? (messageTemplate.required as string)\n })\n }\n\n if (typeof schema.min === 'number') {\n formSchema = formSchema.min(schema.min)\n }\n\n if (typeof schema.max === 'number') {\n formSchema = formSchema.max(schema.max)\n }\n\n if (typeof schema.precision === 'number' && schema.precision <= 0) {\n formSchema = formSchema.integer()\n }\n\n if (options.customValidationMessage) {\n const message = options.customValidationMessage\n\n formSchema = formSchema.messages({\n 'any.required': message,\n 'number.base': message,\n 'number.precision': message,\n 'number.integer': message,\n 'number.min': message,\n 'number.max': 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 this.schema = schema\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const { name } = this\n return this.getFormValue(state[name])\n }\n\n getFormValue(value?: FormStateValue | FormState) {\n return this.isValue(value) ? value : undefined\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { options, schema } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { attributes, prefix, suffix, value } = viewModel\n\n if (typeof schema.precision === 'undefined' || schema.precision <= 0) {\n // If precision isn't provided or provided and\n // less than or equal to 0, use numeric inputmode\n attributes.inputmode = 'numeric'\n }\n\n if (options.prefix) {\n prefix = {\n text: options.prefix\n }\n }\n\n if (options.suffix) {\n suffix = {\n text: options.suffix\n }\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 ...viewModel,\n attributes,\n prefix,\n suffix,\n value\n }\n }\n\n isValue(value?: FormStateValue | FormState) {\n return NumberField.isNumber(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [\n { type: 'required', template: messageTemplate.required },\n { type: 'numberInteger', template: messageTemplate.numberInteger }\n ],\n advancedSettingsErrors: [\n { type: 'numberMin', template: messageTemplate.numberMin },\n { type: 'numberMax', template: messageTemplate.numberMax },\n { type: 'numberPrecision', template: messageTemplate.numberPrecision }\n ]\n }\n }\n\n static isNumber(value?: FormStateValue | FormState): value is number {\n return typeof value === 'number'\n }\n}\n\nexport function getValidatorPrecision(component: NumberField) {\n const validator: CustomValidator = (value: number, helpers) => {\n const { options, schema } = component\n\n const { customValidationMessage: custom } = options\n const { precision: limit } = schema\n\n if (!limit || limit <= 0) {\n return value\n }\n\n const validationSchema = joi\n .number()\n .precision(limit)\n .prefs({ convert: false })\n\n try {\n return joi.attempt(value, validationSchema)\n } catch {\n return custom\n ? helpers.message({ custom }, { limit })\n : helpers.error('number.precision', { limit })\n }\n }\n\n return validator\n}\n"],"mappings":"AACA,OAAOA,GAAG,MAAmD,KAAK;AAElE,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,eAAe;AAUxB,OAAO,MAAMC,WAAW,SAASH,aAAa,CAAC;EAM7CI,WAAWA,CACTC,GAAyB,EACzBC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC,OAAO;MAAEC;IAAO,CAAC,GAAGH,GAAG;IAE/B,IAAII,UAAU,GAAGV,GAAG,CACjBW,MAAM,CAAC,CAAC,CACRC,MAAM,CAACC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CACnCC,KAAK,CAAC,IAAI,CAACA,KAAK,CAAC,CACjBC,QAAQ,CAAC,CAAC;IAEb,IAAIP,OAAO,CAACO,QAAQ,KAAK,KAAK,EAAE;MAC9BL,UAAU,GAAGA,UAAU,CAACM,KAAK,CAAC,EAAE,CAAC;IACnC,CAAC,MAAM;MACL,MAAMC,QAAQ,GAAGT,OAAO,CAACU,wBAAwB;MAEjDR,UAAU,GAAGA,UAAU,CAACS,KAAK,CAAC,EAAE,CAAC,CAACF,QAAQ,CAAC;QACzC;QACA,cAAc,EACZA,QAAQ,GAAG,cAAc,CAAC,IAAKd,eAAe,CAACY;MACnD,CAAC,CAAC;IACJ;IAEA,IAAI,OAAON,MAAM,CAACW,GAAG,KAAK,QAAQ,EAAE;MAClCV,UAAU,GAAGA,UAAU,CAACU,GAAG,CAACX,MAAM,CAACW,GAAG,CAAC;IACzC;IAEA,IAA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umberField","constructor","def","props","options","schema","formSchema","number","custom","getValidatorPrecision","label","required","allow","messages","customValidationMessages","empty","min","max","precision","integer","customValidationMessage","message","default","stateSchema","getFormValueFromState","state","name","getFormValue","value","isValue","undefined","getViewModel","payload","errors","viewModel","attributes","prefix","suffix","inputmode","text","isNumber","getAllPossibleErrors","baseErrors","type","template","numberInteger","advancedSettingsErrors","numberMin","numberMax","numberPrecision","component","validator","helpers","limit","validationSchema","prefs","convert","attempt","error"],"sources":["../../../../../src/server/plugins/engine/components/NumberField.ts"],"sourcesContent":["import { type NumberFieldComponent } from '@defra/forms-model'\nimport joi, { type CustomValidator, type NumberSchema } from 'joi'\n\nimport {\n FormComponent,\n isFormValue\n} 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 FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState\n} from '~/src/server/plugins/engine/types.js'\n\nexport class NumberField extends FormComponent {\n declare options: NumberFieldComponent['options']\n declare schema: NumberFieldComponent['schema']\n declare formSchema: NumberSchema\n declare stateSchema: NumberSchema\n\n constructor(\n def: NumberFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { options, schema } = def\n\n let formSchema = joi\n .number()\n .custom(getValidatorPrecision(this))\n .label(this.label)\n .required()\n\n if (options.required === false) {\n formSchema = formSchema.allow('')\n } else {\n const messages = options.customValidationMessages\n\n formSchema = formSchema.empty('').messages({\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n 'any.required':\n messages?.['any.required'] ?? (messageTemplate.required as string)\n })\n }\n\n if (typeof schema.min === 'number') {\n formSchema = formSchema.min(schema.min)\n }\n\n if (typeof schema.max === 'number') {\n formSchema = formSchema.max(schema.max)\n }\n\n if (typeof schema.precision === 'number' && schema.precision <= 0) {\n formSchema = formSchema.integer()\n }\n\n if (options.customValidationMessage) {\n const message = options.customValidationMessage\n\n formSchema = formSchema.messages({\n 'any.required': message,\n 'number.base': message,\n 'number.precision': message,\n 'number.integer': message,\n 'number.min': message,\n 'number.max': 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 this.schema = schema\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const { name } = this\n return this.getFormValue(state[name])\n }\n\n getFormValue(value?: FormStateValue | FormState) {\n return this.isValue(value) ? value : undefined\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { options, schema } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { attributes, prefix, suffix, value } = viewModel\n\n if (typeof schema.precision === 'undefined' || schema.precision <= 0) {\n // If precision isn't provided or provided and\n // less than or equal to 0, use numeric inputmode\n attributes.inputmode = 'numeric'\n }\n\n if (options.prefix) {\n prefix = {\n text: options.prefix\n }\n }\n\n if (options.suffix) {\n suffix = {\n text: options.suffix\n }\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 ...viewModel,\n attributes,\n prefix,\n suffix,\n value\n }\n }\n\n isValue(value?: FormStateValue | FormState) {\n return NumberField.isNumber(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return NumberField.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: 'numberInteger', template: messageTemplate.numberInteger }\n ],\n advancedSettingsErrors: [\n { type: 'numberMin', template: messageTemplate.numberMin },\n { type: 'numberMax', template: messageTemplate.numberMax },\n { type: 'numberPrecision', template: messageTemplate.numberPrecision }\n ]\n }\n }\n\n static isNumber(value?: FormStateValue | FormState): value is number {\n return typeof value === 'number'\n }\n}\n\nexport function getValidatorPrecision(component: NumberField) {\n const validator: CustomValidator = (value: number, helpers) => {\n const { options, schema } = component\n\n const { customValidationMessage: custom } = options\n const { precision: limit } = schema\n\n if (!limit || limit <= 0) {\n return value\n }\n\n const validationSchema = joi\n .number()\n .precision(limit)\n .prefs({ convert: false })\n\n try {\n return joi.attempt(value, validationSchema)\n } catch {\n return custom\n ? helpers.message({ custom }, { limit })\n : helpers.error('number.precision', { limit })\n }\n }\n\n return validator\n}\n"],"mappings":"AACA,OAAOA,GAAG,MAAmD,KAAK;AAElE,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,eAAe;AAUxB,OAAO,MAAMC,WAAW,SAASH,aAAa,CAAC;EAM7CI,WAAWA,CACTC,GAAyB,EACzBC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC,OAAO;MAAEC;IAAO,CAAC,GAAGH,GAAG;IAE/B,IAAII,UAAU,GAAGV,GAAG,CACjBW,MAAM,CAAC,CAAC,CACRC,MAAM,CAACC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CACnCC,KAAK,CAAC,IAAI,CAACA,KAAK,CAAC,CACjBC,QAAQ,CAAC,CAAC;IAEb,IAAIP,OAAO,CAACO,QAAQ,KAAK,KAAK,EAAE;MAC9BL,UAAU,GAAGA,UAAU,CAACM,KAAK,CAAC,EAAE,CAAC;IACnC,CAAC,MAAM;MACL,MAAMC,QAAQ,GAAGT,OAAO,CAACU,wBAAwB;MAEjDR,UAAU,GAAGA,UAAU,CAACS,KAAK,CAAC,EAAE,CAAC,CAACF,QAAQ,CAAC;QACzC;QACA,cAAc,EACZA,QAAQ,GAAG,cAAc,CAAC,IAAKd,eAAe,CAACY;MACnD,CAAC,CAAC;IACJ;IAEA,IAAI,OAAON,MAAM,CAACW,GAAG,KAAK,QAAQ,EAAE;MAClCV,UAAU,GAAGA,UAAU,CAACU,GAAG,CAAC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,EAAE;QAAEL;MAAM,CAAC,CAAC;IAClD;EACF,CAAC;EAED,OAAOF,SAAS;AAClB","ignoreList":[]}
|
|
@@ -57,4 +57,8 @@ export declare class SelectionControlField extends ListFormComponent {
|
|
|
57
57
|
* For error preview page that shows all possible errors on a component
|
|
58
58
|
*/
|
|
59
59
|
getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
60
|
+
/**
|
|
61
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
62
|
+
*/
|
|
63
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
60
64
|
}
|
|
@@ -45,6 +45,13 @@ export class SelectionControlField extends ListFormComponent {
|
|
|
45
45
|
* For error preview page that shows all possible errors on a component
|
|
46
46
|
*/
|
|
47
47
|
getAllPossibleErrors() {
|
|
48
|
+
return SelectionControlField.getAllPossibleErrors();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
53
|
+
*/
|
|
54
|
+
static getAllPossibleErrors() {
|
|
48
55
|
return {
|
|
49
56
|
baseErrors: [{
|
|
50
57
|
type: 'selectRequired',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SelectionControlField.js","names":["ListFormComponent","messageTemplate","SelectionControlField","getViewModel","payload","errors","options","viewModel","fieldset","items","label","legend","text","classes","map","item","selected","checked","itemModel","bold","getAllPossibleErrors","baseErrors","type","template","selectRequired","advancedSettingsErrors"],"sources":["../../../../../src/server/plugins/engine/components/SelectionControlField.ts"],"sourcesContent":["import { ListFormComponent } from '~/src/server/plugins/engine/components/ListFormComponent.js'\nimport { type ListItem } from '~/src/server/plugins/engine/components/types.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\n/**\n * \"Selection controls\" are checkboxes and radios (and switches), as per Material UI nomenclature.\n */\nexport class SelectionControlField extends ListFormComponent {\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { options } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { fieldset, items, label } = viewModel\n\n fieldset ??= {\n legend: {\n text: label.text,\n classes: 'govuk-fieldset__legend--m'\n }\n }\n\n items = items.map((item) => {\n const { selected: checked } = item\n const itemModel = { ...item, checked } satisfies ListItem\n\n if ('bold' in options && options.bold) {\n itemModel.label ??= {}\n itemModel.label.classes = 'govuk-label--s'\n }\n\n return itemModel\n })\n\n return {\n ...viewModel,\n fieldset,\n items\n }\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [\n { type: 'selectRequired', template: messageTemplate.selectRequired }\n ],\n advancedSettingsErrors: []\n }\n }\n}\n"],"mappings":"AAAA,SAASA,iBAAiB;AAE1B,SAASC,eAAe;AAOxB;AACA;AACA;AACA,OAAO,MAAMC,qBAAqB,SAASF,iBAAiB,CAAC;EAC3DG,YAAYA,CAACC,OAAoB,EAAEC,MAA8B,EAAE;IACjE,MAAM;MAAEC;IAAQ,CAAC,GAAG,IAAI;IAExB,MAAMC,SAAS,GAAG,KAAK,CAACJ,YAAY,CAACC,OAAO,EAAEC,MAAM,CAAC;IACrD,IAAI;MAAEG,QAAQ;MAAEC,KAAK;MAAEC;IAAM,CAAC,GAAGH,SAAS;IAE1CC,QAAQ,KAAK;MACXG,MAAM,EAAE;QACNC,IAAI,EAAEF,KAAK,CAACE,IAAI;QAChBC,OAAO,EAAE;MACX;IACF,CAAC;IAEDJ,KAAK,GAAGA,KAAK,CAACK,GAAG,CAAEC,IAAI,IAAK;MAC1B,MAAM;QAAEC,QAAQ,EAAEC;MAAQ,CAAC,GAAGF,IAAI;MAClC,MAAMG,SAAS,GAAG;QAAE,GAAGH,IAAI;QAAEE;MAAQ,CAAoB;MAEzD,IAAI,MAAM,IAAIX,OAAO,IAAIA,OAAO,CAACa,IAAI,EAAE;QACrCD,SAAS,CAACR,KAAK,KAAK,CAAC,CAAC;QACtBQ,SAAS,CAACR,KAAK,CAACG,OAAO,GAAG,gBAAgB;MAC5C;MAEA,OAAOK,SAAS;IAClB,CAAC,CAAC;IAEF,OAAO;MACL,GAAGX,SAAS;MACZC,QAAQ;MACRC;IACF,CAAC;EACH;;EAEA;AACF;AACA;EACEW,oBAAoBA,CAAA,EAA6B;IAC/C,OAAO;MACLC,UAAU,EAAE,CACV;QAAEC,IAAI,EAAE,gBAAgB;QAAEC,QAAQ,EAAEtB,eAAe,CAACuB;MAAe,CAAC,CACrE;MACDC,sBAAsB,EAAE;IAC1B,CAAC;EACH;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"SelectionControlField.js","names":["ListFormComponent","messageTemplate","SelectionControlField","getViewModel","payload","errors","options","viewModel","fieldset","items","label","legend","text","classes","map","item","selected","checked","itemModel","bold","getAllPossibleErrors","baseErrors","type","template","selectRequired","advancedSettingsErrors"],"sources":["../../../../../src/server/plugins/engine/components/SelectionControlField.ts"],"sourcesContent":["import { ListFormComponent } from '~/src/server/plugins/engine/components/ListFormComponent.js'\nimport { type ListItem } from '~/src/server/plugins/engine/components/types.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\n/**\n * \"Selection controls\" are checkboxes and radios (and switches), as per Material UI nomenclature.\n */\nexport class SelectionControlField extends ListFormComponent {\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { options } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { fieldset, items, label } = viewModel\n\n fieldset ??= {\n legend: {\n text: label.text,\n classes: 'govuk-fieldset__legend--m'\n }\n }\n\n items = items.map((item) => {\n const { selected: checked } = item\n const itemModel = { ...item, checked } satisfies ListItem\n\n if ('bold' in options && options.bold) {\n itemModel.label ??= {}\n itemModel.label.classes = 'govuk-label--s'\n }\n\n return itemModel\n })\n\n return {\n ...viewModel,\n fieldset,\n items\n }\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return SelectionControlField.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: 'selectRequired', template: messageTemplate.selectRequired }\n ],\n advancedSettingsErrors: []\n }\n }\n}\n"],"mappings":"AAAA,SAASA,iBAAiB;AAE1B,SAASC,eAAe;AAOxB;AACA;AACA;AACA,OAAO,MAAMC,qBAAqB,SAASF,iBAAiB,CAAC;EAC3DG,YAAYA,CAACC,OAAoB,EAAEC,MAA8B,EAAE;IACjE,MAAM;MAAEC;IAAQ,CAAC,GAAG,IAAI;IAExB,MAAMC,SAAS,GAAG,KAAK,CAACJ,YAAY,CAACC,OAAO,EAAEC,MAAM,CAAC;IACrD,IAAI;MAAEG,QAAQ;MAAEC,KAAK;MAAEC;IAAM,CAAC,GAAGH,SAAS;IAE1CC,QAAQ,KAAK;MACXG,MAAM,EAAE;QACNC,IAAI,EAAEF,KAAK,CAACE,IAAI;QAChBC,OAAO,EAAE;MACX;IACF,CAAC;IAEDJ,KAAK,GAAGA,KAAK,CAACK,GAAG,CAAEC,IAAI,IAAK;MAC1B,MAAM;QAAEC,QAAQ,EAAEC;MAAQ,CAAC,GAAGF,IAAI;MAClC,MAAMG,SAAS,GAAG;QAAE,GAAGH,IAAI;QAAEE;MAAQ,CAAoB;MAEzD,IAAI,MAAM,IAAIX,OAAO,IAAIA,OAAO,CAACa,IAAI,EAAE;QACrCD,SAAS,CAACR,KAAK,KAAK,CAAC,CAAC;QACtBQ,SAAS,CAACR,KAAK,CAACG,OAAO,GAAG,gBAAgB;MAC5C;MAEA,OAAOK,SAAS;IAClB,CAAC,CAAC;IAEF,OAAO;MACL,GAAGX,SAAS;MACZC,QAAQ;MACRC;IACF,CAAC;EACH;;EAEA;AACF;AACA;EACEW,oBAAoBA,CAAA,EAA6B;IAC/C,OAAOlB,qBAAqB,CAACkB,oBAAoB,CAAC,CAAC;EACrD;;EAEA;AACF;AACA;EACE,OAAOA,oBAAoBA,CAAA,EAA6B;IACtD,OAAO;MACLC,UAAU,EAAE,CACV;QAAEC,IAAI,EAAE,gBAAgB;QAAEC,QAAQ,EAAEtB,eAAe,CAACuB;MAAe,CAAC,CACrE;MACDC,sBAAsB,EAAE;IAC1B,CAAC;EACH;AACF","ignoreList":[]}
|
|
@@ -59,4 +59,8 @@ export declare class TelephoneNumberField extends FormComponent {
|
|
|
59
59
|
* For error preview page that shows all possible errors on a component
|
|
60
60
|
*/
|
|
61
61
|
getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
62
|
+
/**
|
|
63
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
64
|
+
*/
|
|
65
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
62
66
|
}
|
|
@@ -44,6 +44,13 @@ export class TelephoneNumberField extends FormComponent {
|
|
|
44
44
|
* For error preview page that shows all possible errors on a component
|
|
45
45
|
*/
|
|
46
46
|
getAllPossibleErrors() {
|
|
47
|
+
return TelephoneNumberField.getAllPossibleErrors();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
52
|
+
*/
|
|
53
|
+
static getAllPossibleErrors() {
|
|
47
54
|
return {
|
|
48
55
|
baseErrors: [{
|
|
49
56
|
type: 'required',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TelephoneNumberField.js","names":["joi","FormComponent","addClassOptionIfNone","messageTemplate","PATTERN","TelephoneNumberField","constructor","def","props","options","formSchema","string","trim","pattern","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/TelephoneNumberField.ts"],"sourcesContent":["import { type TelephoneNumberFieldComponent } from '@defra/forms-model'\nimport joi, { type StringSchema } from 'joi'\n\nimport { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { addClassOptionIfNone } from '~/src/server/plugins/engine/components/helpers.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\nconst PATTERN = /^[0-9\\\\\\s+()-]*$/\n\nexport class TelephoneNumberField extends FormComponent {\n declare options: TelephoneNumberFieldComponent['options']\n declare formSchema: StringSchema\n declare stateSchema: StringSchema\n\n constructor(\n def: TelephoneNumberFieldComponent,\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 .pattern(PATTERN)\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.pattern.base': message\n })\n } else if (options.customValidationMessages) {\n formSchema = formSchema.messages(options.customValidationMessages)\n }\n\n addClassOptionIfNone(options, 'govuk-input--width-20')\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 = 'tel'\n\n return {\n ...viewModel,\n type: 'tel'\n }\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [\n { type: 'required', template: messageTemplate.required },\n { type: 'format', template: messageTemplate.format }\n ],\n advancedSettingsErrors: []\n }\n }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O;EACxB;EAEAc,YAAYA,CAACC,OAAoB,EAAEC,MAA8B,EAAE;IACjE,MAAMC,SAAS,GAAG,KAAK,CAACH,YAAY,CAACC,OAAO,EAAEC,MAAM,CAAC;IACrD,MAAM;MAAEE;IAAW,CAAC,GAAGD,SAAS;IAEhCC,UAAU,CAACC,YAAY,GAAG,KAAK;IAE/B,OAAO;MACL,GAAGF,SAAS;MACZG,IAAI,EAAE;IACR,CAAC;EACH;;EAEA;AACF;AACA;EACEC,oBAAoBA,CAAA,EAA6B;IAC/C,OAAO;MACLC,UAAU,EAAE,CACV;QAAEF,IAAI,EAAE,UAAU;QAAEG,QAAQ,EAAE7B,eAAe,CAACY;MAAS,CAAC,EACxD;QAAEc,IAAI,EAAE,QAAQ;QAAEG,QAAQ,EAAE7B,eAAe,CAAC8B;MAAO,CAAC,CACrD;MACDC,sBAAsB,EAAE;IAC1B,CAAC;EACH;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"TelephoneNumberField.js","names":["joi","FormComponent","addClassOptionIfNone","messageTemplate","PATTERN","TelephoneNumberField","constructor","def","props","options","formSchema","string","trim","pattern","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/TelephoneNumberField.ts"],"sourcesContent":["import { type TelephoneNumberFieldComponent } from '@defra/forms-model'\nimport joi, { type StringSchema } from 'joi'\n\nimport { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { addClassOptionIfNone } from '~/src/server/plugins/engine/components/helpers.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\nconst PATTERN = /^[0-9\\\\\\s+()-]*$/\n\nexport class TelephoneNumberField extends FormComponent {\n declare options: TelephoneNumberFieldComponent['options']\n declare formSchema: StringSchema\n declare stateSchema: StringSchema\n\n constructor(\n def: TelephoneNumberFieldComponent,\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 .pattern(PATTERN)\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.pattern.base': message\n })\n } else if (options.customValidationMessages) {\n formSchema = formSchema.messages(options.customValidationMessages)\n }\n\n addClassOptionIfNone(options, 'govuk-input--width-20')\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 = 'tel'\n\n return {\n ...viewModel,\n type: 'tel'\n }\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return TelephoneNumberField.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,MAA6B,KAAK;AAE5C,SAASC,aAAa;AACtB,SAASC,oBAAoB;AAC7B,SAASC,eAAe;AAOxB,MAAMC,OAAO,GAAG,kBAAkB;AAElC,OAAO,MAAMC,oBAAoB,SAASJ,aAAa,CAAC;EAKtDK,WAAWA,CACTC,GAAkC,EAClCC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC;IAAQ,CAAC,GAAGF,GAAG;I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C/C,OAAOzB,oBAAoB,CAACyB,oBAAoB,CAAC,CAAC;EACpD;;EAEA;AACF;AACA;EACE,OAAOA,oBAAoBA,CAAA,EAA6B;IACtD,OAAO;MACLC,UAAU,EAAE,CACV;QAAEF,IAAI,EAAE,UAAU;QAAEG,QAAQ,EAAE7B,eAAe,CAACY;MAAS,CAAC,EACxD;QAAEc,IAAI,EAAE,QAAQ;QAAEG,QAAQ,EAAE7B,eAAe,CAAC8B;MAAO,CAAC,CACrD;MACDC,sBAAsB,EAAE;IAC1B,CAAC;EACH;AACF","ignoreList":[]}
|
|
@@ -15,5 +15,9 @@ export declare class TextField extends FormComponent {
|
|
|
15
15
|
* For error preview page that shows all possible errors on a component
|
|
16
16
|
*/
|
|
17
17
|
getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
18
|
+
/**
|
|
19
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
20
|
+
*/
|
|
21
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
18
22
|
static isText(value?: FormStateValue | FormState): value is string;
|
|
19
23
|
}
|
|
@@ -61,6 +61,13 @@ export class TextField extends FormComponent {
|
|
|
61
61
|
* For error preview page that shows all possible errors on a component
|
|
62
62
|
*/
|
|
63
63
|
getAllPossibleErrors() {
|
|
64
|
+
return TextField.getAllPossibleErrors();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
69
|
+
*/
|
|
70
|
+
static getAllPossibleErrors() {
|
|
64
71
|
return {
|
|
65
72
|
baseErrors: [{
|
|
66
73
|
type: 'required',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TextField.js","names":["joi","FormComponent","isFormValue","messageTemplate","TextField","constructor","def","props","options","schema","formSchema","string","trim","label","required","allow","length","max","min","regex","pattern","RegExp","customValidationMessage","message","messages","customValidationMessages","default","stateSchema","getFormValueFromState","state","name","getFormValue","value","isValue","undefined","isText","getAllPossibleErrors","baseErrors","type","template","advancedSettingsErrors"],"sources":["../../../../../src/server/plugins/engine/components/TextField.ts"],"sourcesContent":["import {\n type EmailAddressFieldComponent,\n type TextFieldComponent\n} from '@defra/forms-model'\nimport joi, { type StringSchema } from 'joi'\n\nimport {\n FormComponent,\n isFormValue\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type ErrorMessageTemplateList,\n type FormState,\n type FormStateValue,\n type FormSubmissionState\n} from '~/src/server/plugins/engine/types.js'\n\nexport class TextField extends FormComponent {\n declare options:\n | TextFieldComponent['options']\n | EmailAddressFieldComponent['options']\n\n declare schema: TextFieldComponent['schema']\n\n declare formSchema: StringSchema\n declare stateSchema: StringSchema\n\n constructor(\n def: TextFieldComponent | EmailAddressFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { options } = def\n const schema = 'schema' in def ? def.schema : {}\n\n let formSchema = joi.string().trim().label(this.label).required()\n\n if (options.required === false) {\n formSchema = formSchema.allow('')\n }\n\n if (typeof schema.length !== 'number') {\n if (typeof schema.max === 'number') {\n formSchema = formSchema.max(schema.max)\n }\n\n if (typeof schema.min === 'number') {\n formSchema = formSchema.min(schema.min)\n }\n } else {\n formSchema = formSchema.length(schema.length)\n }\n\n if (schema.regex) {\n const pattern = new RegExp(schema.regex)\n formSchema = formSchema.pattern(pattern)\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.max': message,\n 'string.min': message,\n 'string.length': message,\n 'string.pattern.base': 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 this.schema = schema\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const { name } = this\n return this.getFormValue(state[name])\n }\n\n getFormValue(value?: FormStateValue | FormState) {\n return this.isValue(value) ? value : undefined\n }\n\n isValue(value?: FormStateValue | FormState): value is string {\n return TextField.isText(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [{ type: 'required', template: messageTemplate.required }],\n advancedSettingsErrors: [\n { type: 'min', template: messageTemplate.min },\n { type: 'max', template: messageTemplate.max }\n ]\n }\n }\n\n static isText(value?: FormStateValue | FormState): value is string {\n return isFormValue(value) && typeof value === 'string'\n }\n}\n"],"mappings":"AAIA,OAAOA,GAAG,MAA6B,KAAK;AAE5C,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,eAAe;AAQxB,OAAO,MAAMC,SAAS,SAASH,aAAa,CAAC;EAU3CI,WAAWA,CACTC,GAAoD,EACpDC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC;IAAQ,CAAC,GAAGF,GAAG;IACvB,MAAMG,MAAM,GAAG,QAAQ,IAAIH,GAAG,GAAGA,GAAG,CAACG,MAAM,GAAG,CAAC,CAAC;IAEhD,IAAIC,UAAU,GAAGV,GAAG,CAACW,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC,CAAC,CAACC,KAAK,CAAC,IAAI,CAACA,KAAK,CAAC,CAACC,QAAQ,CAAC,CAAC;IAEjE,IAAIN,OAAO,CAACM,QAAQ,KAAK,KAAK,EAAE;MAC9BJ,UAAU,GAAGA,UAAU,CAACK,KAAK,CAAC,EAAE,CAAC;IACnC;IAEA,IAAI,OAAON,MAAM,CAACO,MAAM,KAAK,QAAQ,EAAE;MACrC,IAAI,OAAOP,MAAM,CAACQ,GAAG,KAAK,QAAQ,EAAE;QAClCP,UAAU,GAAGA,UAAU,CAACO,GAAG,CAACR,MAAM,CAACQ,GAAG,CAAC;MACzC;MAEA,IAAI,OAAOR,MAAM,CAACS,GAAG,KAAK,QAAQ,EAAE;QACl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,OAAO,CAACD,KAAK,CAAC,GAAGA,KAAK,GAAGE,SAAS;EAChD;EAEAD,OAAOA,CAACD,KAAkC,EAAmB;IAC3D,OAAO5B,SAAS,CAAC+B,MAAM,CAACH,KAAK,CAAC;EAChC;;EAEA;AACF;AACA;EACEI,oBAAoBA,CAAA,EAA6B;IAC/C,OAAO;MACLC,UAAU,EAAE,CAAC;QAAEC,IAAI,EAAE,UAAU;QAAEC,QAAQ,EAAEpC,eAAe,CAACW;MAAS,CAAC,CAAC;MACtE0B,sBAAsB,EAAE,CACtB;QAAEF,IAAI,EAAE,KAAK;QAAEC,QAAQ,EAAEpC,eAAe,CAACe;MAAI,CAAC,EAC9C;QAAEoB,IAAI,EAAE,KAAK;QAAEC,QAAQ,EAAEpC,eAAe,CAACc;MAAI,CAAC;IAElD,CAAC;EACH;EAEA,OAAOkB,MAAMA,CAACH,KAAkC,EAAmB;IACjE,OAAO9B,WAAW,CAAC8B,KAAK,CAAC,IAAI,OAAOA,KAAK,KAAK,QAAQ;EACxD;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"TextField.js","names":["joi","FormComponent","isFormValue","messageTemplate","TextField","constructor","def","props","options","schema","formSchema","string","trim","label","required","allow","length","max","min","regex","pattern","RegExp","customValidationMessage","message","messages","customValidationMessages","default","stateSchema","getFormValueFromState","state","name","getFormValue","value","isValue","undefined","isText","getAllPossibleErrors","baseErrors","type","template","advancedSettingsErrors"],"sources":["../../../../../src/server/plugins/engine/components/TextField.ts"],"sourcesContent":["import {\n type EmailAddressFieldComponent,\n type TextFieldComponent\n} from '@defra/forms-model'\nimport joi, { type StringSchema } from 'joi'\n\nimport {\n FormComponent,\n isFormValue\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type ErrorMessageTemplateList,\n type FormState,\n type FormStateValue,\n type FormSubmissionState\n} from '~/src/server/plugins/engine/types.js'\n\nexport class TextField extends FormComponent {\n declare options:\n | TextFieldComponent['options']\n | EmailAddressFieldComponent['options']\n\n declare schema: TextFieldComponent['schema']\n\n declare formSchema: StringSchema\n declare stateSchema: StringSchema\n\n constructor(\n def: TextFieldComponent | EmailAddressFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { options } = def\n const schema = 'schema' in def ? def.schema : {}\n\n let formSchema = joi.string().trim().label(this.label).required()\n\n if (options.required === false) {\n formSchema = formSchema.allow('')\n }\n\n if (typeof schema.length !== 'number') {\n if (typeof schema.max === 'number') {\n formSchema = formSchema.max(schema.max)\n }\n\n if (typeof schema.min === 'number') {\n formSchema = formSchema.min(schema.min)\n }\n } else {\n formSchema = formSchema.length(schema.length)\n }\n\n if (schema.regex) {\n const pattern = new RegExp(schema.regex)\n formSchema = formSchema.pattern(pattern)\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.max': message,\n 'string.min': message,\n 'string.length': message,\n 'string.pattern.base': 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 this.schema = schema\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const { name } = this\n return this.getFormValue(state[name])\n }\n\n getFormValue(value?: FormStateValue | FormState) {\n return this.isValue(value) ? value : undefined\n }\n\n isValue(value?: FormStateValue | FormState): value is string {\n return TextField.isText(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return TextField.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: [{ type: 'required', template: messageTemplate.required }],\n advancedSettingsErrors: [\n { type: 'min', template: messageTemplate.min },\n { type: 'max', template: messageTemplate.max }\n ]\n }\n }\n\n static isText(value?: FormStateValue | FormState): value is string {\n return isFormValue(value) && typeof value === 'string'\n }\n}\n"],"mappings":"AAIA,OAAOA,GAAG,MAA6B,KAAK;AAE5C,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,eAAe;AAQxB,OAAO,MAAMC,SAAS,SAASH,aAAa,CAAC;EAU3CI,WAAWA,CACTC,GAAoD,EACpDC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC;IAAQ,CAAC,GAAGF,GAAG;IACvB,MAAMG,MAAM,GAAG,QAAQ,IAAIH,GAAG,GAAGA,GAAG,CAACG,MAAM,GAAG,CAAC,CAAC;IAEhD,IAAIC,UAAU,GAAGV,GAAG,CAACW,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC,CAAC,CAACC,KAAK,CAAC,IAAI,CAACA,KAAK,CAAC,CAACC,QAAQ,CAAC,CAAC;IAEjE,IAAIN,OAAO,CAACM,QAAQ,KAAK,KAAK,EAAE;MAC9BJ,UAAU,GAAGA,UAAU,CAACK,KAAK,CAAC,EAAE,CAAC;IACnC;IAEA,IAAI,OAAON,MAAM,CAACO,MAAM,KAAK,QAAQ,EAAE;MACrC,IAAI,OAAOP,MAAM,CAACQ,GAAG,KAAK,QAAQ,EAAE;QAClCP,UAAU,GAAGA,UAAU,CAACO,GAAG,CAACR,MAAM,CAACQ,GAAG,CAAC;MACzC;MAEA,IAAI,OAAOR,MAAM,CAACS,GAAG,KAAK,QAAQ,EAAE;QAClCR,UAAU,GAAGA,UAAU,CAACQ,GAAG,CAACT,MAAM,CAACS,GAAG,CAAC;MACzC;IACF,CAAC,MAAM;MACLR,UAAU,GAAGA,UAAU,CAACM,MAAM,CAACP,MAAM,CAACO,MAAM,CAAC;IAC/C;IAEA,IAAIP,MAAM,CAACU,KAAK,EAAE;MAChB,MAAMC,OAAO,GAAG,IAAIC,MAAM,CAACZ,MAAM,CAACU,KAAK,CAAC;MACxCT,UA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A,oBAAoBA,CAAA,EAA6B;IACtD,OAAO;MACLC,UAAU,EAAE,CAAC;QAAEC,IAAI,EAAE,UAAU;QAAEC,QAAQ,EAAEpC,eAAe,CAACW;MAAS,CAAC,CAAC;MACtE0B,sBAAsB,EAAE,CACtB;QAAEF,IAAI,EAAE,KAAK;QAAEC,QAAQ,EAAEpC,eAAe,CAACe;MAAI,CAAC,EAC9C;QAAEoB,IAAI,EAAE,KAAK;QAAEC,QAAQ,EAAEpC,eAAe,CAACc;MAAI,CAAC;IAElD,CAAC;EACH;EAEA,OAAOkB,MAAMA,CAACH,KAAkC,EAAmB;IACjE,OAAO9B,WAAW,CAAC8B,KAAK,CAAC,IAAI,OAAOA,KAAK,KAAK,QAAQ;EACxD;AACF","ignoreList":[]}
|
|
@@ -69,6 +69,10 @@ export declare class UkAddressField extends FormComponent {
|
|
|
69
69
|
* For error preview page that shows all possible errors on a component
|
|
70
70
|
*/
|
|
71
71
|
getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
72
|
+
/**
|
|
73
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
74
|
+
*/
|
|
75
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
72
76
|
static isUkAddress(value?: FormStateValue | FormState): value is UkAddressState;
|
|
73
77
|
}
|
|
74
78
|
export interface UkAddressState extends Record<string, string> {
|
|
@@ -147,6 +147,13 @@ export class UkAddressField extends FormComponent {
|
|
|
147
147
|
* For error preview page that shows all possible errors on a component
|
|
148
148
|
*/
|
|
149
149
|
getAllPossibleErrors() {
|
|
150
|
+
return UkAddressField.getAllPossibleErrors();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
155
|
+
*/
|
|
156
|
+
static getAllPossibleErrors() {
|
|
150
157
|
return {
|
|
151
158
|
baseErrors: [{
|
|
152
159
|
type: 'required',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UkAddressField.js","names":["ComponentType","ComponentCollection","FormComponent","isFormState","TextField","UkAddressField","constructor","def","props","name","options","isRequired","required","hideOptional","optionalText","hideTitle","collection","type","title","schema","max","autocomplete","classes","regex","parent","formSchema","stateSchema","getFormValueFromState","state","value","isState","undefined","getDisplayStringFromState","getContextValueFromState","join","Object","values","filter","Boolean","getViewErrors","errors","getErrors","error","index","self","findIndex","err","getViewModel","payload","viewModel","components","fieldset","hint","label","legend","text","id","attributes","isUkAddress","getAllPossibleErrors","baseErrors","template","advancedSettingsErrors","isText","addressLine1","town","postcode"],"sources":["../../../../../src/server/plugins/engine/components/UkAddressField.ts"],"sourcesContent":["import { ComponentType, type UkAddressFieldComponent } from '@defra/forms-model'\nimport { type ObjectSchema } from 'joi'\n\nimport { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'\nimport {\n FormComponent,\n isFormState\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { TextField } from '~/src/server/plugins/engine/components/TextField.js'\nimport { type QuestionPageController } from '~/src/server/plugins/engine/pageControllers/QuestionPageController.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'\n\nexport class UkAddressField extends FormComponent {\n declare options: UkAddressFieldComponent['options']\n declare formSchema: ObjectSchema<FormPayload>\n declare stateSchema: ObjectSchema<FormState>\n declare collection: ComponentCollection\n\n constructor(\n def: UkAddressFieldComponent,\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 const hideOptional = !!options.optionalText\n const hideTitle = !!options.hideTitle\n\n this.collection = new ComponentCollection(\n [\n {\n type: ComponentType.TextField,\n name: `${name}__addressLine1`,\n title: 'Address line 1',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-line1',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__addressLine2`,\n title: 'Address line 2',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-line2',\n required: false,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__town`,\n title: 'Town or city',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-level2',\n classes: 'govuk-!-width-two-thirds',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__county`,\n title: 'County',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-level1',\n required: false,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__postcode`,\n title: 'Postcode',\n schema: {\n regex: '^[a-zA-Z]{1,2}\\\\d[a-zA-Z\\\\d]?\\\\s?\\\\d[a-zA-Z]{2}$'\n },\n options: {\n autocomplete: 'postal-code',\n classes: 'govuk-input--width-10',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n }\n ],\n { ...props, parent: this }\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 getDisplayStringFromState(state: FormSubmissionState) {\n return this.getContextValueFromState(state)?.join(', ') ?? ''\n }\n\n getContextValueFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n if (!value) {\n return null\n }\n\n return Object.values(value).filter(Boolean)\n }\n\n /**\n * Returns one error per child field\n */\n getViewErrors(\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n return this.getErrors(errors)?.filter(\n (error, index, self) =>\n index === self.findIndex((err) => err.name === error.name)\n )\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { collection, name, options } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { components, fieldset, hint, label } = viewModel\n\n fieldset ??= {\n legend: {\n text: label.text,\n\n /**\n * For screen readers, only hide legend visually. This can be overridden\n * by single component {@link QuestionPageController | `showTitle` handling}\n */\n classes: options.hideTitle\n ? 'govuk-visually-hidden'\n : 'govuk-fieldset__legend--m'\n }\n }\n\n if (hint) {\n hint.id ??= `${name}-hint`\n fieldset.attributes ??= {\n 'aria-describedby': hint.id\n }\n }\n\n components = collection.getViewModel(payload, errors)\n\n return {\n ...viewModel,\n fieldset,\n components\n }\n }\n\n isState(value?: FormStateValue | FormState): value is UkAddressState {\n return UkAddressField.isUkAddress(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [\n { type: 'required', template: 'Enter address line 1' },\n { type: 'required', template: 'Enter town or city' },\n { type: 'required', template: 'Enter postcode' },\n { type: 'format', template: 'Enter valid postcode' }\n ],\n advancedSettingsErrors: []\n }\n }\n\n static isUkAddress(\n value?: FormStateValue | FormState\n ): value is UkAddressState {\n return (\n isFormState(value) &&\n TextField.isText(value.addressLine1) &&\n TextField.isText(value.town) &&\n TextField.isText(value.postcode)\n )\n }\n}\n\nexport interface UkAddressState extends Record<string, string> {\n addressLine1: string\n addressLine2: string\n town: string\n county: string\n postcode: string\n}\n"],"mappings":"AAAA,SAASA,aAAa,QAAsC,oBAAoB;AAGhF,SAASC,mBAAmB;AAC5B,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,SAAS;AAWlB,OAAO,MAAMC,cAAc,SAASH,aAAa,CAAC;EAMhDI,WAAWA,CACTC,GAA4B,EAC5BC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC,IAAI;MAAEC;IAAQ,CAAC,GAAGH,GAAG;IAE7B,MAAMI,UAAU,GAAGD,OAAO,CAACE,QAAQ,KAAK,KAAK;IAC7C,MAAMC,YAAY,GAAG,CAAC,CAACH,OAAO,CAACI,YAAY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;IAC/C,OAAO;MACLC,UAAU,EAAE,CACV;QAAE3C,IAAI,EAAE,UAAU;QAAE4C,QAAQ,EAAE;MAAuB,CAAC,EACtD;QAAE5C,IAAI,EAAE,UAAU;QAAE4C,QAAQ,EAAE;MAAqB,CAAC,EACpD;QAAE5C,IAAI,EAAE,UAAU;QAAE4C,QAAQ,EAAE;MAAiB,CAAC,EAChD;QAAE5C,IAAI,EAAE,QAAQ;QAAE4C,QAAQ,EAAE;MAAuB,CAAC,CACrD;MACDC,sBAAsB,EAAE;IAC1B,CAAC;EACH;EAEA,OAAOJ,WAAWA,CAChB7B,KAAkC,EACT;IACzB,OACE1B,WAAW,CAAC0B,KAAK,CAAC,IAClBzB,SAAS,CAAC2D,MAAM,CAAClC,KAAK,CAACmC,YAAY,CAAC,IACpC5D,SAAS,CAAC2D,MAAM,CAAClC,KAAK,CAACoC,IAAI,CAAC,IAC5B7D,SAAS,CAAC2D,MAAM,CAAClC,KAAK,CAACqC,QAAQ,CAAC;EAEpC;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"UkAddressField.js","names":["ComponentType","ComponentCollection","FormComponent","isFormState","TextField","UkAddressField","constructor","def","props","name","options","isRequired","required","hideOptional","optionalText","hideTitle","collection","type","title","schema","max","autocomplete","classes","regex","parent","formSchema","stateSchema","getFormValueFromState","state","value","isState","undefined","getDisplayStringFromState","getContextValueFromState","join","Object","values","filter","Boolean","getViewErrors","errors","getErrors","error","index","self","findIndex","err","getViewModel","payload","viewModel","components","fieldset","hint","label","legend","text","id","attributes","isUkAddress","getAllPossibleErrors","baseErrors","template","advancedSettingsErrors","isText","addressLine1","town","postcode"],"sources":["../../../../../src/server/plugins/engine/components/UkAddressField.ts"],"sourcesContent":["import { ComponentType, type UkAddressFieldComponent } from '@defra/forms-model'\nimport { type ObjectSchema } from 'joi'\n\nimport { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'\nimport {\n FormComponent,\n isFormState\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { TextField } from '~/src/server/plugins/engine/components/TextField.js'\nimport { type QuestionPageController } from '~/src/server/plugins/engine/pageControllers/QuestionPageController.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'\n\nexport class UkAddressField extends FormComponent {\n declare options: UkAddressFieldComponent['options']\n declare formSchema: ObjectSchema<FormPayload>\n declare stateSchema: ObjectSchema<FormState>\n declare collection: ComponentCollection\n\n constructor(\n def: UkAddressFieldComponent,\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 const hideOptional = !!options.optionalText\n const hideTitle = !!options.hideTitle\n\n this.collection = new ComponentCollection(\n [\n {\n type: ComponentType.TextField,\n name: `${name}__addressLine1`,\n title: 'Address line 1',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-line1',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__addressLine2`,\n title: 'Address line 2',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-line2',\n required: false,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__town`,\n title: 'Town or city',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-level2',\n classes: 'govuk-!-width-two-thirds',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__county`,\n title: 'County',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-level1',\n required: false,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__postcode`,\n title: 'Postcode',\n schema: {\n regex: '^[a-zA-Z]{1,2}\\\\d[a-zA-Z\\\\d]?\\\\s?\\\\d[a-zA-Z]{2}$'\n },\n options: {\n autocomplete: 'postal-code',\n classes: 'govuk-input--width-10',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n }\n ],\n { ...props, parent: this }\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 getDisplayStringFromState(state: FormSubmissionState) {\n return this.getContextValueFromState(state)?.join(', ') ?? ''\n }\n\n getContextValueFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n if (!value) {\n return null\n }\n\n return Object.values(value).filter(Boolean)\n }\n\n /**\n * Returns one error per child field\n */\n getViewErrors(\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n return this.getErrors(errors)?.filter(\n (error, index, self) =>\n index === self.findIndex((err) => err.name === error.name)\n )\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { collection, name, options } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { components, fieldset, hint, label } = viewModel\n\n fieldset ??= {\n legend: {\n text: label.text,\n\n /**\n * For screen readers, only hide legend visually. This can be overridden\n * by single component {@link QuestionPageController | `showTitle` handling}\n */\n classes: options.hideTitle\n ? 'govuk-visually-hidden'\n : 'govuk-fieldset__legend--m'\n }\n }\n\n if (hint) {\n hint.id ??= `${name}-hint`\n fieldset.attributes ??= {\n 'aria-describedby': hint.id\n }\n }\n\n components = collection.getViewModel(payload, errors)\n\n return {\n ...viewModel,\n fieldset,\n components\n }\n }\n\n isState(value?: FormStateValue | FormState): value is UkAddressState {\n return UkAddressField.isUkAddress(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return UkAddressField.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: 'Enter address line 1' },\n { type: 'required', template: 'Enter town or city' },\n { type: 'required', template: 'Enter postcode' },\n { type: 'format', template: 'Enter valid postcode' }\n ],\n advancedSettingsErrors: []\n }\n }\n\n static isUkAddress(\n value?: FormStateValue | FormState\n ): value is UkAddressState {\n return (\n isFormState(value) &&\n TextField.isText(value.addressLine1) &&\n TextField.isText(value.town) &&\n TextField.isText(value.postcode)\n )\n }\n}\n\nexport interface UkAddressState extends Record<string, string> {\n addressLine1: string\n addressLine2: string\n town: string\n county: string\n postcode: string\n}\n"],"mappings":"AAAA,SAASA,aAAa,QAAsC,oBAAoB;AAGhF,SAASC,mBAAmB;AAC5B,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,SAAS;AAWlB,OAAO,MAAMC,cAAc,SAASH,aAAa,CAAC;EAMhDI,WAAWA,CACTC,GAA4B,EAC5BC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC,IAAI;MAAEC;IAAQ,CAAC,GAAGH,GAAG;IAE7B,MAAMI,UAAU,GAAGD,OAAO,CAACE,QAAQ,KAAK,KAAK;IAC7C,MAAMC,YAAY,GAAG,CAAC,CAACH,OAAO,CAACI,YAAY;IAC3C,MAAMC,SAAS,GAAG,CAAC,CAACL,OAAO,CAACK,SAAS;IAErC,IAAI,CAACC,UAAU,GAAG,IAAIf,mBAAmB,CACvC,CACE;MACEgB,IAAI,EAAEjB,aAAa,CAACI,SAAS;MAC7BK,IAAI,EAAE,GAAGA,IAAI,gBAAgB;MAC7BS,KAAK,EAAE,gBAAgB;MACvBC,MAAM,EAAE;QAAEC,GAAG,EAAE;MAAI,CAAC;MACpBV,OAAO,EAAE;QACPW,YAAY,EAAE,eAAe;QAC7BT,QAAQ,EAAED,UAAU;QACpBG,YAAY,EAAE,CAACH,UAAU,KAAKE,YAAY,IAAI,CAACE,SAAS;MAC1D;IACF,CAAC,EACD;MACEE,IAAI,EAAEjB,aAAa,CAACI,S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,WAAWA,CAChB7B,KAAkC,EACT;IACzB,OACE1B,WAAW,CAAC0B,KAAK,CAAC,IAClBzB,SAAS,CAAC2D,MAAM,CAAClC,KAAK,CAACmC,YAAY,CAAC,IACpC5D,SAAS,CAAC2D,MAAM,CAAClC,KAAK,CAACoC,IAAI,CAAC,IAC5B7D,SAAS,CAAC2D,MAAM,CAAClC,KAAK,CAACqC,QAAQ,CAAC;EAEpC;AACF","ignoreList":[]}
|
|
@@ -12,4 +12,8 @@ export declare class YesNoField extends SelectionControlField {
|
|
|
12
12
|
* For error preview page that shows all possible errors on a component
|
|
13
13
|
*/
|
|
14
14
|
getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
15
|
+
/**
|
|
16
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
17
|
+
*/
|
|
18
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
15
19
|
}
|
|
@@ -34,6 +34,13 @@ export class YesNoField extends SelectionControlField {
|
|
|
34
34
|
* For error preview page that shows all possible errors on a component
|
|
35
35
|
*/
|
|
36
36
|
getAllPossibleErrors() {
|
|
37
|
+
return YesNoField.getAllPossibleErrors();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
42
|
+
*/
|
|
43
|
+
static getAllPossibleErrors() {
|
|
37
44
|
return {
|
|
38
45
|
baseErrors: [{
|
|
39
46
|
type: 'selectYesNoRequired',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"YesNoField.js","names":["SelectionControlField","addClassOptionIfNone","messageTemplate","convertToLanguageMessages","YesNoField","constructor","def","props","list","options","formSchema","required","optional","messages","selectYesNoRequired","getAllPossibleErrors","baseErrors","type","template","advancedSettingsErrors"],"sources":["../../../../../src/server/plugins/engine/components/YesNoField.ts"],"sourcesContent":["import { type YesNoFieldComponent } from '@defra/forms-model'\n\nimport { SelectionControlField } from '~/src/server/plugins/engine/components/SelectionControlField.js'\nimport { addClassOptionIfNone } from '~/src/server/plugins/engine/components/helpers.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport { type ErrorMessageTemplateList } from '~/src/server/plugins/engine/types.js'\nimport { convertToLanguageMessages } from '~/src/server/utils/type-utils.js'\n\n/**\n * @description\n * YesNoField is a radiosField with predefined values.\n */\nexport class YesNoField extends SelectionControlField {\n declare options: YesNoFieldComponent['options']\n\n constructor(\n def: YesNoFieldComponent,\n props: ConstructorParameters<typeof SelectionControlField>[1]\n ) {\n super({ ...def, list: '__yesNo' }, props)\n\n const { options } = def\n let { formSchema } = this\n\n addClassOptionIfNone(options, 'govuk-radios--inline')\n\n if (options.required === false) {\n formSchema = formSchema.optional()\n }\n\n formSchema = formSchema.messages(\n convertToLanguageMessages({\n 'any.required': messageTemplate.selectYesNoRequired\n })\n )\n\n this.formSchema = formSchema\n this.options = options\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [\n {\n type: 'selectYesNoRequired',\n template: messageTemplate.selectYesNoRequired\n }\n ],\n advancedSettingsErrors: []\n }\n }\n}\n"],"mappings":"AAEA,SAASA,qBAAqB;AAC9B,SAASC,oBAAoB;AAC7B,SAASC,eAAe;AAExB,SAASC,yBAAyB;;AAElC;AACA;AACA;AACA;AACA,OAAO,MAAMC,UAAU,SAASJ,qBAAqB,CAAC;EAGpDK,WAAWA,CACTC,GAAwB,EACxBC,KAA6D,EAC7D;IACA,KAAK,CAAC;MAAE,GAAGD,GAAG;MAAEE,IAAI,EAAE;IAAU,CAAC,EAAED,KAAK,CAAC;IAEzC,MAAM;MAAEE;IAAQ,CAAC,GAAGH,GAAG;IACvB,IAAI;MAAEI;IAAW,CAAC,GAAG,IAAI;IAEzBT,oBAAoB,CAACQ,OAAO,EAAE,sBAAsB,CAAC;IAErD,IAAIA,OAAO,CAACE,QAAQ,KAAK,KAAK,EAAE;MAC9BD,UAAU,GAAGA,UAAU,CAACE,QAAQ,CAAC,CAAC;IACpC;IAEAF,UAAU,GAAGA,UAAU,CAACG,QAAQ,CAC9BV,yBAAyB,CAAC;MACxB,cAAc,EAAED,eAAe,CAACY;IAClC,CAAC,CACH,CAAC;IAED,IAAI,CAACJ,UAAU,GAAGA,UAAU;IAC5B,IAAI,CAACD,OAAO,GAAGA,OAAO;EACxB;;EAEA;AACF;AACA;EACEM,oBAAoBA,CAAA,EAA6B;IAC/C,OAAO;MACLC,UAAU,EAAE,CACV;QACEC,IAAI,EAAE,qBAAqB;QAC3BC,QAAQ,EAAEhB,eAAe,CAACY;MAC5B,CAAC,CACF;MACDK,sBAAsB,EAAE;IAC1B,CAAC;EACH;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"YesNoField.js","names":["SelectionControlField","addClassOptionIfNone","messageTemplate","convertToLanguageMessages","YesNoField","constructor","def","props","list","options","formSchema","required","optional","messages","selectYesNoRequired","getAllPossibleErrors","baseErrors","type","template","advancedSettingsErrors"],"sources":["../../../../../src/server/plugins/engine/components/YesNoField.ts"],"sourcesContent":["import { type YesNoFieldComponent } from '@defra/forms-model'\n\nimport { SelectionControlField } from '~/src/server/plugins/engine/components/SelectionControlField.js'\nimport { addClassOptionIfNone } from '~/src/server/plugins/engine/components/helpers.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport { type ErrorMessageTemplateList } from '~/src/server/plugins/engine/types.js'\nimport { convertToLanguageMessages } from '~/src/server/utils/type-utils.js'\n\n/**\n * @description\n * YesNoField is a radiosField with predefined values.\n */\nexport class YesNoField extends SelectionControlField {\n declare options: YesNoFieldComponent['options']\n\n constructor(\n def: YesNoFieldComponent,\n props: ConstructorParameters<typeof SelectionControlField>[1]\n ) {\n super({ ...def, list: '__yesNo' }, props)\n\n const { options } = def\n let { formSchema } = this\n\n addClassOptionIfNone(options, 'govuk-radios--inline')\n\n if (options.required === false) {\n formSchema = formSchema.optional()\n }\n\n formSchema = formSchema.messages(\n convertToLanguageMessages({\n 'any.required': messageTemplate.selectYesNoRequired\n })\n )\n\n this.formSchema = formSchema\n this.options = options\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return YesNoField.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 {\n type: 'selectYesNoRequired',\n template: messageTemplate.selectYesNoRequired\n }\n ],\n advancedSettingsErrors: []\n }\n }\n}\n"],"mappings":"AAEA,SAASA,qBAAqB;AAC9B,SAASC,oBAAoB;AAC7B,SAASC,eAAe;AAExB,SAASC,yBAAyB;;AAElC;AACA;AACA;AACA;AACA,OAAO,MAAMC,UAAU,SAASJ,qBAAqB,CAAC;EAGpDK,WAAWA,CACTC,GAAwB,EACxBC,KAA6D,EAC7D;IACA,KAAK,CAAC;MAAE,GAAGD,GAAG;MAAEE,IAAI,EAAE;IAAU,CAAC,EAAED,KAAK,CAAC;IAEzC,MAAM;MAAEE;IAAQ,CAAC,GAAGH,GAAG;IACvB,IAAI;MAAEI;IAAW,CAAC,GAAG,IAAI;IAEzBT,oBAAoB,CAACQ,OAAO,EAAE,sBAAsB,CAAC;IAErD,IAAIA,OAAO,CAACE,QAAQ,KAAK,KAAK,EAAE;MAC9BD,UAAU,GAAGA,UAAU,CAACE,QAAQ,CAAC,CAAC;IACpC;IAEAF,UAAU,GAAGA,UAAU,CAACG,QAAQ,CAC9BV,yBAAyB,CAAC;MACxB,cAAc,EAAED,eAAe,CAACY;IAClC,CAAC,CACH,CAAC;IAED,IAAI,CAACJ,UAAU,GAAGA,UAAU;IAC5B,IAAI,CAACD,OAAO,GAAGA,OAAO;EACxB;;EAEA;AACF;AACA;EACEM,oBAAoBA,CAAA,EAA6B;IAC/C,OAAOX,UAAU,CAACW,oBAAoB,CAAC,CAAC;EAC1C;;EAEA;AACF;AACA;EACE,OAAOA,oBAAoBA,CAAA,EAA6B;IACtD,OAAO;MACLC,UAAU,EAAE,CACV;QACEC,IAAI,EAAE,qBAAqB;QAC3BC,QAAQ,EAAEhB,eAAe,CAACY;MAC5B,CAAC,CACF;MACDK,sBAAsB,EAAE;IAC1B,CAAC;EACH;AACF","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import { type DateInputItem } from '~/src/server/plugins/engine/components/types.js'
|
|
10
10
|
import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
|
|
11
11
|
import {
|
|
12
|
+
type ErrorMessageTemplateList,
|
|
12
13
|
type FormPayload,
|
|
13
14
|
type FormState
|
|
14
15
|
} from '~/src/server/plugins/engine/types.js'
|
|
@@ -412,11 +413,33 @@ describe('DatePartsField', () => {
|
|
|
412
413
|
})
|
|
413
414
|
|
|
414
415
|
describe('AllPossibleErrors', () => {
|
|
415
|
-
it('should return errors', () => {
|
|
416
|
+
it('should return errors from instance method', () => {
|
|
416
417
|
const errors = field.getAllPossibleErrors()
|
|
417
418
|
expect(errors.baseErrors).not.toBeEmpty()
|
|
418
419
|
expect(errors.advancedSettingsErrors).not.toBeEmpty()
|
|
419
420
|
})
|
|
421
|
+
|
|
422
|
+
it('should return errors from static method', () => {
|
|
423
|
+
const staticErrors = (
|
|
424
|
+
field.constructor as typeof field.constructor & {
|
|
425
|
+
getAllPossibleErrors(): ErrorMessageTemplateList
|
|
426
|
+
}
|
|
427
|
+
).getAllPossibleErrors()
|
|
428
|
+
expect(staticErrors.baseErrors).not.toBeEmpty()
|
|
429
|
+
expect(staticErrors.advancedSettingsErrors).not.toBeEmpty()
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
it('instance method should delegate to static method', () => {
|
|
433
|
+
const staticResult = (
|
|
434
|
+
field.constructor as typeof field.constructor & {
|
|
435
|
+
getAllPossibleErrors(): ErrorMessageTemplateList
|
|
436
|
+
}
|
|
437
|
+
).getAllPossibleErrors()
|
|
438
|
+
|
|
439
|
+
const instanceResult = field.getAllPossibleErrors()
|
|
440
|
+
|
|
441
|
+
expect(instanceResult).toEqual(staticResult)
|
|
442
|
+
})
|
|
420
443
|
})
|
|
421
444
|
})
|
|
422
445
|
|
|
@@ -195,6 +195,13 @@ export class DatePartsField extends FormComponent {
|
|
|
195
195
|
* For error preview page that shows all possible errors on a component
|
|
196
196
|
*/
|
|
197
197
|
getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
198
|
+
return DatePartsField.getAllPossibleErrors()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
203
|
+
*/
|
|
204
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
198
205
|
return {
|
|
199
206
|
baseErrors: [
|
|
200
207
|
{ type: 'required', template: messageTemplate.required },
|
|
@@ -59,6 +59,13 @@ export class EmailAddressField extends FormComponent {
|
|
|
59
59
|
* For error preview page that shows all possible errors on a component
|
|
60
60
|
*/
|
|
61
61
|
getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
62
|
+
return EmailAddressField.getAllPossibleErrors()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
67
|
+
*/
|
|
68
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
62
69
|
return {
|
|
63
70
|
baseErrors: [
|
|
64
71
|
{ type: 'required', template: messageTemplate.required },
|
|
@@ -270,6 +270,13 @@ export class FileUploadField extends FormComponent {
|
|
|
270
270
|
* For error preview page that shows all possible errors on a component
|
|
271
271
|
*/
|
|
272
272
|
getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
273
|
+
return FileUploadField.getAllPossibleErrors()
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
278
|
+
*/
|
|
279
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
273
280
|
return {
|
|
274
281
|
baseErrors: [
|
|
275
282
|
{ type: 'selectRequired', template: messageTemplate.selectRequired },
|
|
@@ -191,7 +191,17 @@ export class FormComponent extends ComponentBase {
|
|
|
191
191
|
return isFormState(value)
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
+
/**
|
|
195
|
+
* For error preview page that shows all possible errors on a component
|
|
196
|
+
*/
|
|
194
197
|
getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
198
|
+
return FormComponent.getAllPossibleErrors()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
203
|
+
*/
|
|
204
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
195
205
|
return {
|
|
196
206
|
baseErrors: [],
|
|
197
207
|
advancedSettingsErrors: []
|
|
@@ -148,6 +148,13 @@ export class ListFormComponent extends FormComponent {
|
|
|
148
148
|
* For error preview page that shows all possible errors on a component
|
|
149
149
|
*/
|
|
150
150
|
getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
151
|
+
return ListFormComponent.getAllPossibleErrors()
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
156
|
+
*/
|
|
157
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
151
158
|
return {
|
|
152
159
|
baseErrors: [
|
|
153
160
|
{ type: 'selectRequired', template: messageTemplate.selectRequired }
|
|
@@ -189,6 +189,13 @@ export class MonthYearField extends FormComponent {
|
|
|
189
189
|
* For error preview page that shows all possible errors on a component
|
|
190
190
|
*/
|
|
191
191
|
getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
192
|
+
return MonthYearField.getAllPossibleErrors()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
197
|
+
*/
|
|
198
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList {
|
|
192
199
|
return {
|
|
193
200
|
baseErrors: [
|
|
194
201
|
{ type: 'required', template: messageTemplate.required },
|