-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from dataforgoodfr/fix/D4G-293-transform-emai…
…l-to-lowercase fix: Prevent uppercased letters in email
- Loading branch information
Showing
11 changed files
with
116 additions
and
14 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
31 changes: 31 additions & 0 deletions
31
packages/client/src/modules/common/hooks/useForm.formatters.ts
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,31 @@ | ||
import { FieldValues } from "react-hook-form"; | ||
|
||
export { formatData }; | ||
|
||
const FIELD_NAME_TO_FORMATTER: Record< | ||
string, | ||
{ | ||
formatter: (value: string) => string; | ||
} | ||
> = { | ||
email: { | ||
formatter: formatEmail, | ||
}, | ||
}; | ||
|
||
function formatEmail(email: string = ""): string { | ||
return email.trim().toLowerCase(); | ||
} | ||
|
||
function formatData<TFieldValues extends FieldValues = FieldValues>( | ||
data: TFieldValues | ||
): TFieldValues { | ||
return Object.fromEntries( | ||
Object.entries(data).map(([fieldName, fieldValue]) => { | ||
const formatter = | ||
FIELD_NAME_TO_FORMATTER[fieldName]?.formatter || | ||
((value: any) => value); | ||
return [fieldName, formatter(fieldValue)]; | ||
}) | ||
) as TFieldValues; | ||
} |
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,45 @@ | ||
import { useCallback, useMemo } from "react"; | ||
import { | ||
// eslint-disable-next-line no-restricted-imports | ||
useForm as useFormLib, | ||
FieldValues, | ||
UseFormProps, | ||
UseFormReturn, | ||
UseFormHandleSubmit, | ||
SubmitHandler, | ||
SubmitErrorHandler, | ||
} from "react-hook-form"; | ||
import { formatData } from "./useForm.formatters"; | ||
|
||
export { useForm }; | ||
|
||
const useForm = < | ||
TFieldValues extends FieldValues = FieldValues, | ||
TContext = any | ||
>( | ||
props?: UseFormProps<TFieldValues, TContext> | ||
): UseFormReturn<TFieldValues, TContext> => { | ||
const form = useFormLib(props); | ||
|
||
const handleSubmit: UseFormHandleSubmit<TFieldValues> = useCallback( | ||
( | ||
onValid: SubmitHandler<TFieldValues>, | ||
onInvalid?: SubmitErrorHandler<TFieldValues> | ||
) => { | ||
return form.handleSubmit((data: TFieldValues, ...args) => { | ||
onValid(formatData(data), ...args); | ||
}, onInvalid); | ||
}, | ||
[form] | ||
); | ||
|
||
const newForm = useMemo( | ||
() => ({ | ||
...form, | ||
handleSubmit, | ||
}), | ||
[form, handleSubmit] | ||
); | ||
|
||
return newForm; | ||
}; |
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
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,9 @@ | ||
import { z } from "zod"; | ||
|
||
export { getEmailSchema }; | ||
|
||
const getEmailSchema = () => | ||
z | ||
.string() | ||
.email() | ||
.transform((email) => (email || "").trim().toLowerCase()); |