|
1 | 1 | import { TelephoneNumberFieldOptionsFormatEnum } from '@defra/forms-model' |
2 | 2 | import LibPhoneNumber from 'google-libphonenumber' |
3 | | -import JoiBase, { type JoiExpression, type LanguageMessages } from 'joi' |
| 3 | +import JoiBase, { type LanguageMessages } from 'joi' |
4 | 4 |
|
5 | | -import { opts } from '~/src/server/plugins/engine/pageControllers/validationOptions.js' |
| 5 | +import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js' |
6 | 6 |
|
7 | 7 | const phoneUtil = LibPhoneNumber.PhoneNumberUtil.getInstance() |
8 | 8 |
|
9 | 9 | export const COUNTRY = 'GB' |
10 | 10 | export const INVALID_ERROR_CODE = 'phoneNumber.invalid' |
11 | | -export const UK_ERROR_CODE = 'phoneNumber.uk' |
12 | | -export const INTERNATIONAL_ERROR_CODE = 'phoneNumber.international' |
13 | 11 |
|
14 | 12 | export const isUKNumber = (value: LibPhoneNumber.PhoneNumber) => { |
15 | 13 | return phoneUtil.isValidNumberForRegion(value, COUNTRY) |
16 | 14 | } |
17 | 15 |
|
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 | | - |
28 | 16 | export const joi = JoiBase.extend({ |
29 | 17 | type: 'string', |
30 | 18 | base: JoiBase.string(), |
31 | 19 | 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, |
45 | 22 | rules: { |
46 | 23 | phoneNumber: { |
47 | 24 | method({ |
@@ -69,25 +46,24 @@ export const joi = JoiBase.extend({ |
69 | 46 | const parsed = phoneUtil.parse(value, COUNTRY) |
70 | 47 |
|
71 | 48 | if (!phoneUtil.isValidNumber(parsed)) { |
72 | | - return error(getErrorCode(format)) |
| 49 | + return error(INVALID_ERROR_CODE) |
73 | 50 | } |
74 | 51 |
|
75 | 52 | if (format) { |
76 | 53 | const isUK = isUKNumber(parsed) |
77 | 54 |
|
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) |
83 | 59 | ) { |
84 | | - return error(INTERNATIONAL_ERROR_CODE) |
| 60 | + return error(INVALID_ERROR_CODE) |
85 | 61 | } |
86 | 62 | } |
87 | 63 |
|
88 | 64 | return value |
89 | 65 | } catch { |
90 | | - return error(getErrorCode(format)) |
| 66 | + return error(INVALID_ERROR_CODE) |
91 | 67 | } |
92 | 68 | } |
93 | 69 | } |
|
0 commit comments