Skip to content

Commit

Permalink
Merge pull request #195 from ukhsa-collaboration/feat/OOC-4943-teleph…
Browse files Browse the repository at this point in the history
…one-validation

https://jira.collab.test-and-trace.nhs.uk/browse/OOC-4393: OOC-4943 Update phone number validation
  • Loading branch information
kathryn-dale authored Jun 19, 2024
2 parents e5b15a3 + 2515a0b commit 4d8cef3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions model/src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export interface TelephoneNumberFieldComponent extends TextFieldBase {
type: "TelephoneNumberField";
options: TextFieldBase["options"] & {
customValidationMessage?: string;
requiredFieldValidationMessage?: string;
};
}

Expand Down
6 changes: 3 additions & 3 deletions runner/src/server/forms/ReportAnOutbreak.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"title": "S1Q7. Telephone number/s of Key Contact Person",
"hint": "Please consider using a non-personal number.",
"nameHasError": false,
"schema": {}
"schema": { "regex": "^(\\+44|0)(\\d\\s?){10,11}$" }
},
{
"name": "S1Q8",
Expand Down Expand Up @@ -194,7 +194,7 @@
"title": "S1Q7. Telephone number/s of Key Contact Person",
"hint": "Please consider using a non-personal number.",
"nameHasError": false,
"schema": {}
"schema": { "regex": "^(\\+44|0)(\\d\\s?){10,11}$" }
},
{
"name": "S1Q8",
Expand Down Expand Up @@ -257,7 +257,7 @@
"title": "S1Q7. Telephone number/s of Key Contact Person",
"hint": "Please consider using a non-personal number.",
"nameHasError": false,
"schema": {}
"schema": { "regex": "^(\\+44|0)(\\d\\s?){10,11}$" }
},
{
"name": "S1Q8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import joi, { Schema } from "joi";

const PATTERN = /^((\+\d{0,4})|(0))[0-9\s()+]{0,20}$/;
const DEFAULT_MESSAGE = "Enter a telephone number in the correct format";
const REQUIRED_MESSAGE =
"Enter a telephone number, like 01632 960 001, 07700 900 982 or +44 808 157 0192";
export class TelephoneNumberField extends FormComponent {
constructor(def: TelephoneNumberFieldComponent, model: FormModel) {
super(def, model);
Expand All @@ -28,7 +30,14 @@ export class TelephoneNumberField extends FormComponent {
}
componentSchema = componentSchema
.pattern(pattern)
.message(def.options?.customValidationMessage ?? DEFAULT_MESSAGE)
.messages({
"any.required":
def.options?.requiredFieldValidationMessage ?? REQUIRED_MESSAGE,
"string.empty":
def.options?.requiredFieldValidationMessage ?? REQUIRED_MESSAGE,
"string.pattern.base":
def.options?.customValidationMessage ?? DEFAULT_MESSAGE,
})
.label(def.title);

if (schema.max) {
Expand Down

0 comments on commit 4d8cef3

Please sign in to comment.