From bccb56ed614496d06be35c96a807ef3799de9733 Mon Sep 17 00:00:00 2001 From: garronej Date: Fri, 18 Mar 2022 00:46:12 +0100 Subject: [PATCH] Add support for options validator --- README.md | 9 +++ src/lib/components/RegisterUserProfile.tsx | 66 +++++++++++++++------- src/lib/getKcContext/KcContextBase.ts | 4 ++ src/lib/i18n/kcMessages/login.ts | 2 + src/lib/useFormValidationSlice.tsx | 28 ++++++++- 5 files changed, 87 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 9ad1f78e..4b9d3fb1 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,10 @@

+> New in v4.7: +> Register with user profile enabled: Out of the box `options` validator support. +> [Example](https://user-images.githubusercontent.com/6702424/158911163-81e6bbe8-feb0-4dc8-abff-de199d7a678e.mov) + # Motivations Keycloak provides [theme support](https://www.keycloak.org/docs/latest/server_development/#_themes) for web pages. This allows customizing the look and feel of end-user facing pages so they can be integrated with your applications. @@ -474,6 +478,11 @@ and `kcRegisterContext["authorizedMailDomains"]` to validate on. # Changelog highlights +# v4.7.0 + +Register with user profile enabled: Out of the box `options` validator support. +[Example](https://user-images.githubusercontent.com/6702424/158911163-81e6bbe8-feb0-4dc8-abff-de199d7a678e.mov) + # v4.6.0 `tss-react` and `powerhooks` are no longer peer dependencies of `keycloakify`. diff --git a/src/lib/components/RegisterUserProfile.tsx b/src/lib/components/RegisterUserProfile.tsx index ab39cee8..05e6858c 100644 --- a/src/lib/components/RegisterUserProfile.tsx +++ b/src/lib/components/RegisterUserProfile.tsx @@ -95,7 +95,7 @@ const UserProfileFormFields = memo(({ kcContext, onIsFormSubmittableValueChange, { target: { value }, }, - ]: [React.ChangeEvent], + ]: [React.ChangeEvent], ) => formValidationReducer({ "action": "update value", @@ -148,26 +148,50 @@ const UserProfileFormFields = memo(({ kcContext, onIsFormSubmittableValueChange, {attribute.required && <>*}
- { - switch (attribute.name) { - case "password-confirm": - case "password": - return "password"; - default: - return "text"; - } - })()} - id={attribute.name} - name={attribute.name} - value={value} - onChange={onChangeFactory(attribute.name)} - className={cx(props.kcInputClass)} - aria-invalid={displayableErrors.length !== 0} - disabled={attribute.readOnly} - autoComplete={attribute.autocomplete} - onBlur={onBlurFactory(attribute.name)} - /> + {(() => { + const { options } = attribute.validators; + + if (options !== undefined) { + return ( + + ); + } + + return ( + { + switch (attribute.name) { + case "password-confirm": + case "password": + return "password"; + default: + return "text"; + } + })()} + id={attribute.name} + name={attribute.name} + value={value} + onChange={onChangeFactory(attribute.name)} + className={cx(props.kcInputClass)} + aria-invalid={displayableErrors.length !== 0} + disabled={attribute.readOnly} + autoComplete={attribute.autocomplete} + onBlur={onBlurFactory(attribute.name)} + /> + ); + })()} {displayableErrors.length !== 0 && ( ; export declare namespace Validators { @@ -331,6 +332,9 @@ export declare namespace Validators { min?: `${number}`; max?: `${number}`; }; + export type Options = { + options: string[]; + }; } assert>(); diff --git a/src/lib/i18n/kcMessages/login.ts b/src/lib/i18n/kcMessages/login.ts index 305398fd..2a00f275 100644 --- a/src/lib/i18n/kcMessages/login.ts +++ b/src/lib/i18n/kcMessages/login.ts @@ -10,6 +10,7 @@ const kcMessages = { "shouldBeDifferent": "{0} should be different to {1}", "shouldMatchPattern": "Pattern should match: `/{0}/`", "mustBeAnInteger": "Must be an integer", + "notAValidOption": "Not a valid option", }, "fr": { ...kcMessagesBase["fr"], @@ -18,6 +19,7 @@ const kcMessages = { "shouldBeDifferent": "{0} doit être différent de {1}", "shouldMatchPattern": "Dois respecter le schéma: `/{0}/`", "mustBeAnInteger": "Doit être un nombre entiers", + "notAValidOption": "N'est pas une option valide", /* spell-checker: enable */ }, }; diff --git a/src/lib/useFormValidationSlice.tsx b/src/lib/useFormValidationSlice.tsx index 277cf213..9efa36df 100644 --- a/src/lib/useFormValidationSlice.tsx +++ b/src/lib/useFormValidationSlice.tsx @@ -213,7 +213,7 @@ export function useGetErrors(params: { break scope; } - const msgArgs = ["invalidEmailMessage"] as const; + const msgArgs = [id("invalidEmailMessage")] as const; errors.push({ validatorName, @@ -276,6 +276,32 @@ export function useGetErrors(params: { } } + scope: { + const validatorName = "options"; + + const validator = validators[validatorName]; + + if (validator === undefined) { + break scope; + } + + if (value === "") { + break scope; + } + + if (validator.options.indexOf(value) >= 0) { + break scope; + } + + const msgArgs = [id("notAValidOption")] as const; + + errors.push({ + validatorName, + "errorMessage": {advancedMsg(...msgArgs)}, + "errorMessageStr": advancedMsgStr(...msgArgs), + }); + } + //TODO: Implement missing validators. return errors;