Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
feat: add website validation for demarches simplifiees (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre authored Jun 7, 2022
1 parent 26fb420 commit e4c20b3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/services/__tests__/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
psychologistLastNameDoesNotMatchMessage,
validatePsychologist,
ValidationAdeliData,
webSiteIsInvalidMessage,
} from "../demarchesSimplifiees/validate-psychologist";

describe("validatePsychologist", () => {
Expand Down Expand Up @@ -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);
});
});
10 changes: 9 additions & 1 deletion src/services/demarchesSimplifiees/validate-psychologist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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`;

0 comments on commit e4c20b3

Please sign in to comment.