Implement and type validators

This commit is contained in:
garronej 2021-10-11 20:56:43 +02:00
parent 637bc75fc2
commit c4ba470dc4
2 changed files with 260 additions and 36 deletions

View File

@ -1,8 +1,8 @@
import type { PageId } from "../../bin/build-keycloak-theme/generateFtl";
import type { KcLanguageTag } from "../i18n/KcLanguageTag";
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
import type { MessageKey } from "../i18n/useKcMessage";
import type { LanguageLabel } from "../i18n/KcLanguageTag";
@ -135,20 +135,13 @@ export declare namespace KcContextBase {
};
};
export type RegisterUserProfile = RegisterCommon & {
pageId: "register-user-profile.ftl";
profile: {
attributes: {
name: string;
displayName?: string;
required: boolean;
value?: string;
group?: string;
groupDisplayHeader?: string;
groupDisplayDescription?: string;
readOnly: boolean;
autocomplete?: string;
}[];
context: "REGISTRATION_PROFILE";
attributes: Attribute[];
attributesByName: Record<string, Attribute>;
}
};
@ -213,6 +206,61 @@ export declare namespace KcContextBase {
}
export type Attribute = {
name: string;
displayName?: string;
required: boolean;
value?: string;
group?: string;
groupDisplayHeader?: string;
groupDisplayDescription?: string;
readOnly: boolean;
autocomplete?: string;
validators: Validators;
annotations: Record<string, string>;
groupAnnotations: Record<string, string>
};
export type Validators = Partial<{
length: Validators.DoIgnoreEmpty & Validators.Range;
double: Validators.DoIgnoreEmpty & Validators.Range;
integer: Validators.DoIgnoreEmpty & Validators.Range;
email: Validators.DoIgnoreEmpty;
'up-immutable-attribute': {};
'up-attribute-required-by-metadata-value': {};
'up-username-has-value': {};
'up-duplicate-username': {};
'up-username-mutation': {};
'up-email-exists-as-username': {};
'up-blank-attribute-value': Validators.ErrorMessage & {
'fail-on-null': boolean;
};
'up-duplicate-email': {};
'local-date': Validators.DoIgnoreEmpty;
pattern: Validators.DoIgnoreEmpty & Validators.ErrorMessage & { pattern: string; };
'person-name-prohibited-characters': Validators.DoIgnoreEmpty & Validators.ErrorMessage;
uri: Validators.DoIgnoreEmpty;
'username-prohibited-characters': Validators.DoIgnoreEmpty & Validators.ErrorMessage;
}>;
export declare namespace Validators {
export type DoIgnoreEmpty = {
'ignore.empty.value'?: boolean;
};
export type ErrorMessage = {
'error-message'?: string;
}
export type Range = {
/** "0", "1", "2"... yeah I know, don't tell me */
min?: string;
max?: string;
};
}
assert<Equals<KcContextBase["pageId"], PageId>>();

View File

@ -1,5 +1,6 @@
import type { KcContextBase } from "../KcContextBase";
import "minimal-polyfills/Object.fromEntries";
import type { KcContextBase, Attribute } from "../KcContextBase";
import { getEvtKcLanguage } from "../../i18n/useKcLanguageTag";
import { getKcLanguageTagLabel } from "../../i18n/KcLanguageTag";
//NOTE: Aside because we want to be able to import them from node
@ -163,25 +164,200 @@ export const kcContextMocks: KcContextBase[] = [
"registrationDisabled": false,
}),
id<KcContextBase.Register>({
...(() => {
const registerCommon: KcContextBase.RegisterCommon = {
...kcContextCommonMock,
"pageId": "register.ftl",
"url": {
...loginUrl,
"registrationAction": "http://localhost:8080/auth/realms/myrealm/login-actions/registration?session_code=gwZdUeO7pbYpFTRxiIxRg_QtzMbtFTKrNu6XW_f8asM&execution=12146ce0-b139-4bbd-b25b-0eccfee6577e&client_id=account&tab_id=uS8lYfebLa0"
},
"scripts": [],
"isAppInitiatedAction": false,
"register": {
"formData": {}
},
"passwordRequired": true,
"recaptchaRequired": false,
"social": {
"displayInfo": true
},
};
return [
id<KcContextBase.Register>({
"pageId": "register.ftl",
...registerCommon,
"register": {
"formData": {}
},
}),
id<KcContextBase.RegisterUserProfile>({
"pageId": "register-user-profile.ftl",
...registerCommon,
"profile": {
"context": "REGISTRATION_PROFILE" as const,
...(() => {
const attributes: Attribute[] = [
{
"validators": {
"username-prohibited-characters": {
"ignore.empty.value": true
},
"up-username-has-value": {
},
"length": {
"ignore.empty.value": true,
"min": "3",
"max": "255"
},
"up-duplicate-username": {
},
"up-username-mutation": {
}
},
"displayName": "${username}",
"annotations": {
},
"required": true,
"groupAnnotations": {
},
"autocomplete": "username",
"readOnly": false,
"name": "username"
},
{
"validators": {
"up-email-exists-as-username": {
},
"length": {
"max": "255",
"ignore.empty.value": true
},
"up-blank-attribute-value": {
"error-message": "missingEmailMessage",
"fail-on-null": false
},
"up-duplicate-email": {
},
"email": {
"ignore.empty.value": true
}
},
"displayName": "${email}",
"annotations": {
},
"required": true,
"groupAnnotations": {
},
"autocomplete": "email",
"readOnly": false,
"name": "email"
},
{
"validators": {
"length": {
"max": "255",
"ignore.empty.value": true
},
"person-name-prohibited-characters": {
"ignore.empty.value": true
},
"up-immutable-attribute": {
},
"up-attribute-required-by-metadata-value": {
}
},
"displayName": "${firstName}",
"annotations": {
},
"required": true,
"groupAnnotations": {
},
"readOnly": false,
"name": "firstName"
},
{
"validators": {
"length": {
"max": "255",
"ignore.empty.value": true
},
"person-name-prohibited-characters": {
"ignore.empty.value": true
},
"up-immutable-attribute": {},
"up-attribute-required-by-metadata-value": {}
},
"displayName": "${lastName}",
"annotations": {
},
"required": true,
"groupAnnotations": {
},
"readOnly": false,
"name": "lastName"
},
{
"validators": {
"length": {
"ignore.empty.value": true,
"min": "3",
"max": "9"
},
"up-immutable-attribute": {},
"up-attribute-required-by-metadata-value": {},
"email": {
"ignore.empty.value": true
}
},
"displayName": "${foo}",
"annotations": {
"this_is_second_key": "this_is_second_value",
"this_is_first_key": "this_is_first_value"
},
"required": true,
"groupAnnotations": {
},
"readOnly": false,
"name": "foo"
}
];
return {
attributes,
"attributesByName": Object.fromEntries(
attributes.map(attribute => [attribute.name, attribute])
) as any
} as any;
})()
}
})
];
})(),
id<KcContextBase.Info>({
...kcContextCommonMock,
"pageId": "info.ftl",