Skip to content

Commit 598ff20

Browse files
DF-1031: Update telephone number error messages (#420)
* Update telephone number error messages * Remove custom telephone error messages * Simplify telephone error message handling
1 parent 8f51f91 commit 598ff20

4 files changed

Lines changed: 19 additions & 53 deletions

File tree

src/server/plugins/engine/components/TelephoneNumberField.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import {
99
getAnswer,
1010
type Field
1111
} from '~/src/server/plugins/engine/components/helpers/components.js'
12-
import {
13-
INTERNATIONAL_ERROR_CODE,
14-
INVALID_ERROR_CODE,
15-
UK_ERROR_CODE
16-
} from '~/src/server/plugins/engine/components/helpers/telephone.js'
12+
import { INVALID_ERROR_CODE } from '~/src/server/plugins/engine/components/helpers/telephone.js'
1713
import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
1814
import definition from '~/test/form/definitions/blank.js'
1915
import { getFormData, getFormState } from '~/test/helpers/component-helpers.js'
@@ -315,9 +311,7 @@ describe('TelephoneNumberField', () => {
315311
'any.required': 'This is a custom required error',
316312
'string.empty': 'This is a custom empty string error',
317313
'string.pattern.base': 'This is a custom pattern error',
318-
[INVALID_ERROR_CODE]: 'This is a custom pattern error',
319-
[UK_ERROR_CODE]: 'This is a custom pattern error',
320-
[INTERNATIONAL_ERROR_CODE]: 'This is a custom pattern error'
314+
[INVALID_ERROR_CODE]: 'This is a custom pattern error'
321315
}
322316
}
323317
} satisfies TelephoneNumberFieldComponent,

src/server/plugins/engine/components/TelephoneNumberField.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { type StringSchema } from 'joi'
44
import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
55
import { addClassOptionIfNone } from '~/src/server/plugins/engine/components/helpers/index.js'
66
import {
7-
INTERNATIONAL_ERROR_CODE,
87
INVALID_ERROR_CODE,
9-
UK_ERROR_CODE,
108
joi
119
} from '~/src/server/plugins/engine/components/helpers/telephone.js'
1210
import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
@@ -48,9 +46,7 @@ export class TelephoneNumberField extends FormComponent {
4846
'any.required': message,
4947
'string.empty': message,
5048
'string.pattern.base': message,
51-
[INVALID_ERROR_CODE]: message,
52-
[UK_ERROR_CODE]: message,
53-
[INTERNATIONAL_ERROR_CODE]: message
49+
[INVALID_ERROR_CODE]: message
5450
})
5551
} else if (options.customValidationMessages) {
5652
formSchema = formSchema.messages(options.customValidationMessages)

src/server/plugins/engine/components/helpers/telephone.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Telephone validation helpers', () => {
3838

3939
expect(result.error).toBeDefined()
4040
expect(result.error?.message).toBe(
41-
'Enter home phone, like 01632 960 001, 07700 900 982 or +44 808 157 0192'
41+
'Enter home phone in the correct format'
4242
)
4343
expect(result.value).toBe('ABC')
4444
})
@@ -53,7 +53,7 @@ describe('Telephone validation helpers', () => {
5353

5454
expect(result.error).toBeDefined()
5555
expect(result.error?.message).toBe(
56-
'Enter home phone, like 01632 960 001, 07700 900 982 or +44 808 157 0192'
56+
'Enter home phone in the correct format'
5757
)
5858
expect(result.value).toBe('+1-212-456-7890')
5959
})
@@ -86,7 +86,7 @@ describe('Telephone validation helpers', () => {
8686

8787
expect(result.error).toBeDefined()
8888
expect(result.error?.message).toBe(
89-
'Enter home phone, starting with + and the country code, for example +92333 1234567 or 00923331234567'
89+
'Enter home phone in the correct format'
9090
)
9191
expect(result.value).toBe('ABC')
9292
})
@@ -103,7 +103,7 @@ describe('Telephone validation helpers', () => {
103103

104104
expect(result.error).toBeDefined()
105105
expect(result.error?.message).toBe(
106-
'Enter home phone, starting with + and the country code, for example +92333 1234567 or 00923331234567'
106+
'Enter home phone in the correct format'
107107
)
108108
expect(result.value).toBe('+44 1606 76477')
109109
})
@@ -135,7 +135,7 @@ describe('Telephone validation helpers', () => {
135135

136136
expect(result.error).toBeDefined()
137137
expect(result.error?.message).toBe(
138-
'Enter home phone, like 01632 960 001, 07700 900 982, +44 808 157 0192 or +924568456136'
138+
'Enter home phone in the correct format'
139139
)
140140
expect(result.value).toBe('ABC')
141141
})

src/server/plugins/engine/components/helpers/telephone.ts

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,24 @@
11
import { TelephoneNumberFieldOptionsFormatEnum } from '@defra/forms-model'
22
import LibPhoneNumber from 'google-libphonenumber'
3-
import JoiBase, { type JoiExpression, type LanguageMessages } from 'joi'
3+
import JoiBase, { type LanguageMessages } from 'joi'
44

5-
import { opts } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
5+
import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
66

77
const phoneUtil = LibPhoneNumber.PhoneNumberUtil.getInstance()
88

99
export const COUNTRY = 'GB'
1010
export const INVALID_ERROR_CODE = 'phoneNumber.invalid'
11-
export const UK_ERROR_CODE = 'phoneNumber.uk'
12-
export const INTERNATIONAL_ERROR_CODE = 'phoneNumber.international'
1311

1412
export const isUKNumber = (value: LibPhoneNumber.PhoneNumber) => {
1513
return phoneUtil.isValidNumberForRegion(value, COUNTRY)
1614
}
1715

18-
export function getErrorCode(format?: TelephoneNumberFieldOptionsFormatEnum) {
19-
if (format === TelephoneNumberFieldOptionsFormatEnum.UK) {
20-
return UK_ERROR_CODE
21-
} else if (format === TelephoneNumberFieldOptionsFormatEnum.International) {
22-
return INTERNATIONAL_ERROR_CODE
23-
}
24-
25-
return INVALID_ERROR_CODE
26-
}
27-
2816
export const joi = JoiBase.extend({
2917
type: 'string',
3018
base: JoiBase.string(),
3119
messages: {
32-
[INVALID_ERROR_CODE]: JoiBase.expression(
33-
'Enter {{lowerFirst(#label)}}, like 01632 960 001, 07700 900 982, +44 808 157 0192 or +924568456136',
34-
opts
35-
) as JoiExpression,
36-
[UK_ERROR_CODE]: JoiBase.expression(
37-
'Enter {{lowerFirst(#label)}}, like 01632 960 001, 07700 900 982 or +44 808 157 0192',
38-
opts
39-
) as JoiExpression,
40-
[INTERNATIONAL_ERROR_CODE]: JoiBase.expression(
41-
'Enter {{lowerFirst(#label)}}, starting with + and the country code, for example +92333 1234567 or 00923331234567',
42-
opts
43-
) as JoiExpression
44-
} as unknown as LanguageMessages,
20+
[INVALID_ERROR_CODE]: messageTemplate.format
21+
} as LanguageMessages,
4522
rules: {
4623
phoneNumber: {
4724
method({
@@ -69,25 +46,24 @@ export const joi = JoiBase.extend({
6946
const parsed = phoneUtil.parse(value, COUNTRY)
7047

7148
if (!phoneUtil.isValidNumber(parsed)) {
72-
return error(getErrorCode(format))
49+
return error(INVALID_ERROR_CODE)
7350
}
7451

7552
if (format) {
7653
const isUK = isUKNumber(parsed)
7754

78-
if (!isUK && format === TelephoneNumberFieldOptionsFormatEnum.UK) {
79-
return error(UK_ERROR_CODE)
80-
} else if (
81-
isUK &&
82-
format === TelephoneNumberFieldOptionsFormatEnum.International
55+
if (
56+
(!isUK && format === TelephoneNumberFieldOptionsFormatEnum.UK) ||
57+
(isUK &&
58+
format === TelephoneNumberFieldOptionsFormatEnum.International)
8359
) {
84-
return error(INTERNATIONAL_ERROR_CODE)
60+
return error(INVALID_ERROR_CODE)
8561
}
8662
}
8763

8864
return value
8965
} catch {
90-
return error(getErrorCode(format))
66+
return error(INVALID_ERROR_CODE)
9167
}
9268
}
9369
}

0 commit comments

Comments
 (0)