-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pc/modals: Validate phone and email even when they're optional
- Loading branch information
1 parent
02a6af3
commit a1e71c7
Showing
5 changed files
with
71 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as Yup from "yup"; | ||
|
||
export const nameValidation = (option?: string) => { | ||
let validation = Yup.string(); | ||
if (option === "required") { | ||
validation = validation.required("Name is required"); | ||
} else { | ||
validation = validation.optional(); | ||
} | ||
return validation; | ||
}; | ||
|
||
export const emailValidation = (option?: string) => { | ||
let validation = Yup.string().email("Email is invalid"); | ||
if (option === "required") { | ||
validation = validation.required("Email is required"); | ||
} else { | ||
validation = validation.optional(); | ||
} | ||
return validation; | ||
}; | ||
|
||
export const phoneValidation = (option?: string) => { | ||
// E.164 international standard format | ||
let validation = Yup.string().matches( | ||
/^\+[1-9]\d{1,14}$/, | ||
"Phone is invalid" | ||
); | ||
if (option === "required") { | ||
validation = validation.required("Phone is required"); | ||
} else { | ||
validation = validation.optional(); | ||
} | ||
return validation; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,5 +53,29 @@ describe("Privacy request", () => { | |
|
||
cy.getByTestId("request-submitted"); | ||
}); | ||
|
||
it("requires valid inputs", () => { | ||
cy.visit("/"); | ||
cy.getByTestId("card").contains("Access your data").click(); | ||
|
||
cy.getByTestId("privacy-request-form").within(() => { | ||
// This block uses `.root()` to keep queries within the form. This is necessary because of | ||
// `.blur()` which triggers input validation. | ||
cy.root().get("input#email").type("invalid email"); | ||
cy.root().get("input#phone").type("123 456 7890 1234567").blur(); | ||
|
||
cy.root().should("contain", "Email is invalid"); | ||
cy.root().should("contain", "Phone is invalid"); | ||
cy.root().get("button").contains("Continue").should("be.disabled"); | ||
|
||
cy.root().get("input#email").type("[email protected]"); | ||
cy.root().get("input#phone").clear().type("123 456 7890").blur(); | ||
cy.root().get("button").contains("Continue").should("be.enabled"); | ||
|
||
// The phone input is optional (in the default config) so it can be left blank. | ||
cy.root().get("input#phone").clear().blur(); | ||
cy.root().get("button").contains("Continue").should("be.enabled"); | ||
}); | ||
}); | ||
}); | ||
}); |