adds schema parsing for UserFormInfoPart & ConnectionFormOptionsPart

This commit is contained in:
sergeyteleshev
2025-08-21 17:01:28 +02:00
parent 7f7f6f79fc
commit 9c9e843c98
5 changed files with 44 additions and 57 deletions
+3 -1
View File
@@ -57,4 +57,6 @@ export const schemaExtra = {
},
};
export { z as schema };
const RecordKeySchema = z.union([z.string(), z.number(), z.symbol()]);
export { z as schema, RecordKeySchema };
@@ -1,17 +1,20 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
* Copyright (C) 2020-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
export interface IUserFormInfoState {
userId: string;
enabled: boolean;
password: string;
metaParameters: Record<string, any>;
teams: string[];
import { RecordKeySchema, schema } from '@cloudbeaver/core-utils';
authRole: string; // used in TE product
}
export const USER_FORM_INFO_PART_SCHEMA = schema.object({
userId: schema.string().trim(),
enabled: schema.boolean(),
password: schema.string().trim(),
metaParameters: schema.record(RecordKeySchema, schema.string().trim().or(schema.any())),
teams: schema.array(schema.string().trim()),
authRole: schema.string().trim(),
});
export type IUserFormInfoState = schema.infer<typeof USER_FORM_INFO_PART_SCHEMA>;
@@ -15,7 +15,7 @@ import { isArraysEqual, isObjectsEqual, isValuesEqual } from '@cloudbeaver/core-
import { isDefined } from '@dbeaver/js-helpers';
import type { IUserFormState } from '../AdministrationUserFormService.js';
import type { IUserFormInfoState } from './IUserFormInfoState.js';
import { USER_FORM_INFO_PART_SCHEMA, type IUserFormInfoState } from './IUserFormInfoState.js';
const DEFAULT_ENABLED = true;
@@ -27,35 +27,22 @@ export class UserFormInfoPart extends FormPart<IUserFormInfoState, IUserFormStat
private readonly usersResource: UsersResource,
private readonly usersMetaParametersResource: UsersMetaParametersResource,
) {
super(formState, {
userId: formState.state.userId || '',
enabled: DEFAULT_ENABLED,
password: '',
metaParameters: {},
teams: [],
authRole: '',
});
super(
formState,
{
userId: formState.state.userId || '',
enabled: DEFAULT_ENABLED,
password: '',
metaParameters: {},
teams: [],
authRole: '',
},
USER_FORM_INFO_PART_SCHEMA,
);
this.disableUser = this.disableUser.bind(this);
}
protected override format(data: IFormState<IUserFormState>, contexts: IExecutionContextProvider<IFormState<IUserFormState>>): void | Promise<void> {
this.state.password = this.state.password.trim();
const metaParameters = this.state.metaParameters;
if (this.formState.mode === FormMode.Create) {
this.state.userId = this.state.userId.trim();
}
for (const key in metaParameters) {
const value = metaParameters[key];
if (typeof value === 'string') {
metaParameters[key] = value.trim();
}
}
}
override isOutdated(): boolean {
if (this.formState.mode === FormMode.Edit && this.initialState.userId) {
return this.usersResource.isOutdated(this.initialState.userId) || this.usersMetaParametersResource.isOutdated(this.initialState.userId);
@@ -32,7 +32,7 @@ import { parseJdbcUri } from '@dbeaver/jdbc-uri-parser';
import { getDefaultConfigurationType } from './getDefaultConfigurationType.js';
import { getConnectionName } from './getConnectionName.js';
import type { IConnectionFormOptionsState } from './IConnectionFormOptionsState.js';
import { CONNECTION_FORM_OPTIONS_SCHEMA, type IConnectionFormOptionsState } from './IConnectionFormOptionsState.js';
import type { IConnectionFormState } from '../IConnectionFormState.js';
import { ConnectionAuthenticationDialogLoader } from '../../ConnectionAuthentication/ConnectionAuthenticationDialogLoader.js';
@@ -70,7 +70,7 @@ export class ConnectionFormOptionsPart extends FormPart<IConnectionFormOptionsSt
private readonly commonDialogService: CommonDialogService,
private readonly notificationService: NotificationService,
) {
super(formState, defaultStateGetter(formState.state.connectionId));
super(formState, defaultStateGetter(formState.state.connectionId), CONNECTION_FORM_OPTIONS_SCHEMA);
this.onDriverIdChange = new Executor();
@@ -341,16 +341,11 @@ export class ConnectionFormOptionsPart extends FormPart<IConnectionFormOptionsSt
this.state.networkHandlersConfig = observable([]);
this.state.keepAliveInterval = this.state.keepAliveInterval ? Number(this.state.keepAliveInterval) : undefined;
this.state.name = this.state.name?.trim();
this.state.description = this.state.description?.trim();
if (!this.state.folder) {
delete this.state.folder;
}
if (this.state.configurationType === DriverConfigurationType.Url) {
this.state.url = this.state.url?.trim();
} else {
if (this.state.configurationType !== DriverConfigurationType.Url) {
// if manual type configuration set, it helps to keep host, port, etc. properties (not saved on backend)
delete this.state.url;
}
@@ -6,39 +6,39 @@
* you may not use this file except in compliance with the License.
*/
import { DriverConfigurationType } from '@cloudbeaver/core-sdk';
import { schema } from '@cloudbeaver/core-utils';
import { RecordKeySchema, schema } from '@cloudbeaver/core-utils';
import { CONNECTION_NETWORK_HANDLER_SCHEMA } from './IConnectionNetworkHanler.js';
export const CONNECTION_PROPERTIES_SCHEMA = schema.record(schema.string(), schema.any());
export const CONNECTION_PROPERTIES_SCHEMA = schema.record(RecordKeySchema, schema.any());
export const CONNECTION_CONFIG_SCHEMA = schema.object({
authModelId: schema.string().optional(),
autocommit: schema.boolean().optional(),
configurationType: schema.enum([DriverConfigurationType.Manual, DriverConfigurationType.Url]).optional(),
connectionId: schema.string().optional(),
credentials: schema.record(schema.string(), schema.any()).optional(),
credentials: schema.record(RecordKeySchema, schema.any()).optional(),
dataSourceId: schema.string().optional(),
databaseName: schema.string().optional(),
description: schema.string().optional(),
databaseName: schema.string().trim().optional(),
description: schema.string().trim().optional(),
driverId: schema.string().optional(),
folder: schema.string().optional(),
host: schema.string().optional(),
host: schema.string().trim().optional(),
keepAliveInterval: schema.number().optional(),
defaultCatalogName: schema.string().optional(),
defaultSchemaName: schema.string().optional(),
mainPropertyValues: schema.record(schema.string(), schema.any()).optional(),
name: schema.string().optional(),
mainPropertyValues: schema.record(RecordKeySchema, schema.any()).optional(),
name: schema.string().trim().optional(),
networkHandlersConfig: schema.array(CONNECTION_NETWORK_HANDLER_SCHEMA).optional(),
port: schema.string().optional(),
port: schema.string().trim().optional(),
properties: CONNECTION_PROPERTIES_SCHEMA.optional(),
providerProperties: schema.record(schema.string(), schema.any()).optional(),
providerProperties: schema.record(RecordKeySchema, schema.any()).optional(),
readOnly: schema.boolean().optional(),
saveCredentials: schema.boolean().optional(),
selectedSecretId: schema.string().optional(),
serverName: schema.string().optional(),
serverName: schema.string().trim().optional(),
sharedCredentials: schema.boolean().optional(),
url: schema.string().optional(),
userName: schema.string().optional(),
url: schema.string().trim().optional(),
userName: schema.string().trim().optional(),
userPassword: schema.string().optional(),
});