Big refactor of useFormValidator into useUserProfileForm
This commit is contained in:
parent
95c27dd97e
commit
2e9d2b8bd2
@ -5,10 +5,10 @@ import type { MessageKey } from "keycloakify/login/i18n/i18n";
|
|||||||
import type { Attribute, Validators } from "keycloakify/login/kcContext/KcContext";
|
import type { Attribute, Validators } from "keycloakify/login/kcContext/KcContext";
|
||||||
import { useConstCallback } from "keycloakify/tools/useConstCallback";
|
import { useConstCallback } from "keycloakify/tools/useConstCallback";
|
||||||
import { emailRegexp } from "keycloakify/tools/emailRegExp";
|
import { emailRegexp } from "keycloakify/tools/emailRegExp";
|
||||||
import type { KcContext } from "../kcContext";
|
import type { KcContext, PasswordPolicies } from "keycloakify/login/kcContext/KcContext";
|
||||||
import type { I18n } from "../i18n";
|
|
||||||
import type { Param0 } from "tsafe";
|
import type { Param0 } from "tsafe";
|
||||||
import { assert, type Equals } from "tsafe/assert";
|
import { assert, type Equals } from "tsafe/assert";
|
||||||
|
import type { I18n } from "../i18n";
|
||||||
|
|
||||||
export type FormFieldError = {
|
export type FormFieldError = {
|
||||||
errorMessage: JSX.Element;
|
errorMessage: JSX.Element;
|
||||||
@ -46,21 +46,24 @@ export type FormAction =
|
|||||||
name: string;
|
name: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ParamsOfUseFromValidation = {
|
export type KcContextLike = {
|
||||||
kcContext: {
|
|
||||||
messagesPerField: Pick<KcContext.Common["messagesPerField"], "existsError" | "get">;
|
messagesPerField: Pick<KcContext.Common["messagesPerField"], "existsError" | "get">;
|
||||||
profile: {
|
profile: {
|
||||||
attributes: Attribute[];
|
attributes: Attribute[];
|
||||||
};
|
};
|
||||||
passwordRequired?: boolean;
|
passwordRequired?: boolean;
|
||||||
realm: { registrationEmailAsUsername: boolean };
|
realm: { registrationEmailAsUsername: boolean };
|
||||||
|
passwordPolicies?: PasswordPolicies;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ParamsOfUseProfileAttributeForm = {
|
||||||
|
kcContext: KcContextLike;
|
||||||
passwordValidators?: Validators;
|
passwordValidators?: Validators;
|
||||||
requirePasswordConfirmation?: boolean;
|
requirePasswordConfirmation?: boolean;
|
||||||
i18n: I18n;
|
i18n: I18n;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ReturnTypeOfUseFormValidation = {
|
export type ReturnTypeOfUseProfileAttributeForm = {
|
||||||
formState: FormState;
|
formState: FormState;
|
||||||
dispatchFormAction: Dispatch<FormAction>;
|
dispatchFormAction: Dispatch<FormAction>;
|
||||||
attributesWithPassword: Attribute[];
|
attributesWithPassword: Attribute[];
|
||||||
@ -70,7 +73,7 @@ export type ReturnTypeOfUseFormValidation = {
|
|||||||
* NOTE: The attributesWithPassword returned is actually augmented with
|
* NOTE: The attributesWithPassword returned is actually augmented with
|
||||||
* artificial password related attributes only if kcContext.passwordRequired === true
|
* artificial password related attributes only if kcContext.passwordRequired === true
|
||||||
*/
|
*/
|
||||||
export function useFormValidation(params: ParamsOfUseFromValidation): ReturnTypeOfUseFormValidation {
|
export function useUserProfileForm(params: ParamsOfUseProfileAttributeForm): ReturnTypeOfUseProfileAttributeForm {
|
||||||
const { kcContext, passwordValidators = {}, requirePasswordConfirmation = true, i18n } = params;
|
const { kcContext, passwordValidators = {}, requirePasswordConfirmation = true, i18n } = params;
|
||||||
|
|
||||||
const attributesWithPassword = useMemo(() => {
|
const attributesWithPassword = useMemo(() => {
|
||||||
@ -189,7 +192,27 @@ export function useFormValidation(params: ParamsOfUseFromValidation): ReturnType
|
|||||||
break handle_multi_valued_attribute;
|
break handle_multi_valued_attribute;
|
||||||
}
|
}
|
||||||
|
|
||||||
const values = attribute.values ?? [attribute.value ?? ""];
|
const values = attribute.values ?? [""];
|
||||||
|
|
||||||
|
apply_validator_min_range: {
|
||||||
|
const validator = attribute.validators.multivalued;
|
||||||
|
|
||||||
|
if (validator === undefined) {
|
||||||
|
break apply_validator_min_range;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { min: minStr } = validator;
|
||||||
|
|
||||||
|
if (minStr === undefined) {
|
||||||
|
break apply_validator_min_range;
|
||||||
|
}
|
||||||
|
|
||||||
|
const min = parseInt(minStr);
|
||||||
|
|
||||||
|
for (let index = values.length; index < min; index++) {
|
||||||
|
values.push("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (let index = 0; index < values.length; index++) {
|
for (let index = 0; index < values.length; index++) {
|
||||||
initialFormFieldValues.push({
|
initialFormFieldValues.push({
|
||||||
@ -250,9 +273,7 @@ export function useFormValidation(params: ParamsOfUseFromValidation): ReturnType
|
|||||||
|
|
||||||
/** Expect to be used in a component wrapped within a <I18nProvider> */
|
/** Expect to be used in a component wrapped within a <I18nProvider> */
|
||||||
function useGetErrors(params: {
|
function useGetErrors(params: {
|
||||||
kcContext: {
|
kcContext: Pick<KcContextLike, "messagesPerField">;
|
||||||
messagesPerField: Pick<KcContext.Common["messagesPerField"], "existsError" | "get">;
|
|
||||||
};
|
|
||||||
attributes: {
|
attributes: {
|
||||||
name: string;
|
name: string;
|
||||||
validators: Validators;
|
validators: Validators;
|
Loading…
x
Reference in New Issue
Block a user