From b9134582319ad2fd05c6d5403ffd33d5870c1f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Huchet?= Date: Tue, 7 Jun 2022 12:19:01 +0200 Subject: [PATCH] feat: add website validation for demarches simplifiees --- src/services/__tests__/validation.spec.ts | 34 +++++++++++++++++++ .../validate-psychologist.ts | 10 +++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/services/__tests__/validation.spec.ts b/src/services/__tests__/validation.spec.ts index 791f36e8..37c21b5d 100644 --- a/src/services/__tests__/validation.spec.ts +++ b/src/services/__tests__/validation.spec.ts @@ -10,6 +10,7 @@ import { psychologistLastNameDoesNotMatchMessage, validatePsychologist, ValidationAdeliData, + webSiteIsInvalidMessage, } from "../demarchesSimplifiees/validate-psychologist"; describe("validatePsychologist", () => { @@ -158,4 +159,37 @@ describe("validatePsychologist", () => { candidateIsNotAPsychologistMessage("73") ); }); + + it("should fail when website is wrong", () => { + const invalidPsychologists: CandidatePsychologist[] = [ + { + ...validPsychologist, + website: "abcdefg", + }, + { + ...validPsychologist, + website: "www.example.org", + }, + ]; + + for (const invalidPsychologist of invalidPsychologists) { + const validation = validatePsychologist(invalidPsychologist, [adeliData]); + assertIsValidationError(validation); + + expect(validation.error.issues[0]?.message).toEqual( + webSiteIsInvalidMessage(invalidPsychologist.website) + ); + } + }); + + it("should work when website is ok", () => { + const { success } = validatePsychologist( + { + ...validPsychologist, + website: "https://example.org", + }, + [adeliData] + ); + expect(success).toBe(true); + }); }); diff --git a/src/services/demarchesSimplifiees/validate-psychologist.ts b/src/services/demarchesSimplifiees/validate-psychologist.ts index 15cac59c..140f1a6f 100644 --- a/src/services/demarchesSimplifiees/validate-psychologist.ts +++ b/src/services/demarchesSimplifiees/validate-psychologist.ts @@ -8,7 +8,9 @@ import { areSimilar, firstWordAreSimilar } from "../../utils/string"; export type CandidatePsychologist = Pick< Psychologist, "lastName" | "firstName" | "email" ->; +> & { + website?: string; +}; export type ValidationAdeliData = Pick< AdeliData, "Nom d'exercice" | "Prénom d'exercice" | "Code profession" @@ -68,6 +70,9 @@ export const validatePsychologist = ( return z .object({ psychologist: z.object({ + website: z.optional( + z.string().url(webSiteIsInvalidMessage(psychologist.website)) + ), lastName: validateWithAdeliData({ adeliData, message: psychologistLastNameDoesNotMatchMessage, @@ -116,3 +121,6 @@ export const candidateIsNotAPsychologistMessage = (code) => export const emailIsInvalidMessage = (email) => `L'email renseigné (${email}) n'est pas valide`; + +export const webSiteIsInvalidMessage = (url) => + `Le site web renseigné (${url}) n'est pas valide`;