Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {
getAnswer,
type Field
} from '~/src/server/plugins/engine/components/helpers/components.js'
import {
INTERNATIONAL_ERROR_CODE,
INVALID_ERROR_CODE,
UK_ERROR_CODE
} from '~/src/server/plugins/engine/components/helpers/telephone.js'
import { INVALID_ERROR_CODE } from '~/src/server/plugins/engine/components/helpers/telephone.js'
import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
import definition from '~/test/form/definitions/blank.js'
import { getFormData, getFormState } from '~/test/helpers/component-helpers.js'
Expand Down Expand Up @@ -315,9 +311,7 @@ describe('TelephoneNumberField', () => {
'any.required': 'This is a custom required error',
'string.empty': 'This is a custom empty string error',
'string.pattern.base': 'This is a custom pattern error',
[INVALID_ERROR_CODE]: 'This is a custom pattern error',
[UK_ERROR_CODE]: 'This is a custom pattern error',
[INTERNATIONAL_ERROR_CODE]: 'This is a custom pattern error'
[INVALID_ERROR_CODE]: 'This is a custom pattern error'
}
}
} satisfies TelephoneNumberFieldComponent,
Expand Down
6 changes: 1 addition & 5 deletions src/server/plugins/engine/components/TelephoneNumberField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { type StringSchema } from 'joi'
import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'
import { addClassOptionIfNone } from '~/src/server/plugins/engine/components/helpers/index.js'
import {
INTERNATIONAL_ERROR_CODE,
INVALID_ERROR_CODE,
UK_ERROR_CODE,
joi
} from '~/src/server/plugins/engine/components/helpers/telephone.js'
import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
Expand Down Expand Up @@ -48,9 +46,7 @@ export class TelephoneNumberField extends FormComponent {
'any.required': message,
'string.empty': message,
'string.pattern.base': message,
[INVALID_ERROR_CODE]: message,
[UK_ERROR_CODE]: message,
[INTERNATIONAL_ERROR_CODE]: message
[INVALID_ERROR_CODE]: message
})
} else if (options.customValidationMessages) {
formSchema = formSchema.messages(options.customValidationMessages)
Expand Down
10 changes: 5 additions & 5 deletions src/server/plugins/engine/components/helpers/telephone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Telephone validation helpers', () => {

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

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

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

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

expect(result.error).toBeDefined()
expect(result.error?.message).toBe(
'Enter home phone, like 01632 960 001, 07700 900 982, +44 808 157 0192 or +924568456136'
'Enter home phone in the correct format'
)
expect(result.value).toBe('ABC')
})
Expand Down
46 changes: 11 additions & 35 deletions src/server/plugins/engine/components/helpers/telephone.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
import { TelephoneNumberFieldOptionsFormatEnum } from '@defra/forms-model'
import LibPhoneNumber from 'google-libphonenumber'
import JoiBase, { type JoiExpression, type LanguageMessages } from 'joi'
import JoiBase, { type LanguageMessages } from 'joi'

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

const phoneUtil = LibPhoneNumber.PhoneNumberUtil.getInstance()

export const COUNTRY = 'GB'
export const INVALID_ERROR_CODE = 'phoneNumber.invalid'
export const UK_ERROR_CODE = 'phoneNumber.uk'
export const INTERNATIONAL_ERROR_CODE = 'phoneNumber.international'

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

export function getErrorCode(format?: TelephoneNumberFieldOptionsFormatEnum) {
if (format === TelephoneNumberFieldOptionsFormatEnum.UK) {
return UK_ERROR_CODE
} else if (format === TelephoneNumberFieldOptionsFormatEnum.International) {
return INTERNATIONAL_ERROR_CODE
}

return INVALID_ERROR_CODE
}

export const joi = JoiBase.extend({
type: 'string',
base: JoiBase.string(),
messages: {
[INVALID_ERROR_CODE]: JoiBase.expression(
'Enter {{lowerFirst(#label)}}, like 01632 960 001, 07700 900 982, +44 808 157 0192 or +924568456136',
opts
) as JoiExpression,
[UK_ERROR_CODE]: JoiBase.expression(
'Enter {{lowerFirst(#label)}}, like 01632 960 001, 07700 900 982 or +44 808 157 0192',
opts
) as JoiExpression,
[INTERNATIONAL_ERROR_CODE]: JoiBase.expression(
'Enter {{lowerFirst(#label)}}, starting with + and the country code, for example +92333 1234567 or 00923331234567',
opts
) as JoiExpression
} as unknown as LanguageMessages,
[INVALID_ERROR_CODE]: messageTemplate.format
} as LanguageMessages,
rules: {
phoneNumber: {
method({
Expand Down Expand Up @@ -69,25 +46,24 @@ export const joi = JoiBase.extend({
const parsed = phoneUtil.parse(value, COUNTRY)

if (!phoneUtil.isValidNumber(parsed)) {
return error(getErrorCode(format))
return error(INVALID_ERROR_CODE)
}

if (format) {
const isUK = isUKNumber(parsed)

if (!isUK && format === TelephoneNumberFieldOptionsFormatEnum.UK) {
return error(UK_ERROR_CODE)
} else if (
isUK &&
format === TelephoneNumberFieldOptionsFormatEnum.International
if (
(!isUK && format === TelephoneNumberFieldOptionsFormatEnum.UK) ||
(isUK &&
format === TelephoneNumberFieldOptionsFormatEnum.International)
) {
return error(INTERNATIONAL_ERROR_CODE)
return error(INVALID_ERROR_CODE)
}
}

return value
} catch {
return error(getErrorCode(format))
return error(INVALID_ERROR_CODE)
}
}
}
Expand Down
Loading