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

feat: add website validation for demarches simplifiees #205

Merged
merged 1 commit into from
Jun 7, 2022
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
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`;