Skip to content

Commit

Permalink
CB-4629 fix: zod validation and localization
Browse files Browse the repository at this point in the history
  • Loading branch information
Wroud committed Apr 15, 2024
1 parent c45c4de commit d28715c
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 18 deletions.
11 changes: 9 additions & 2 deletions webapp/packages/core-cli/configs/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
const { resolve } = require('path');
const PnpWebpackPlugin = require('pnp-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
Expand Down Expand Up @@ -150,9 +157,9 @@ module.exports = (env, argv) => {
pathinfo: false,
},
resolve: {
extensions: ['.ts', '.tsx', '.wasm', '.mjs', '.js', '.jsx', '.json'],
extensions: ['.ts', '.tsx', '.wasm', '.mjs', '.js', '.jsx', '.json', '.json5'],
modules: nodeModules,
plugins: [PnpWebpackPlugin, new SourceAssetsResolver(['.json5', '.css', '.scss'])],
plugins: [PnpWebpackPlugin, new SourceAssetsResolver(['.json5', '.css', '.scss', '.json'])],
},
resolveLoader: {
modules: nodeModules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ export class ConnectionInfoResource extends CachedMapResource<IConnectionInfoPar
protected validateKey(key: IConnectionInfoParams): boolean {
const parse = CONNECTION_INFO_PARAM_SCHEMA.safeParse(toJS(key));
if (!parse.success) {
this.logger.warn(`Invalid resource key ${schemaValidationError(parse.error).toString()}`);
this.logger.warn(`Invalid resource key ${(schemaValidationError(parse.error).toString(), { prefix: null })}`);
}
return parse.success;
}
Expand Down
1 change: 0 additions & 1 deletion webapp/packages/core-product/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"dependencies": {
"@cloudbeaver/core-di": "~0.1.0",
"@cloudbeaver/core-settings": "~0.1.0",
"@cloudbeaver/core-utils": "~0.1.0",
"mobx": "^6.12.0"
},
"peerDependencies": {},
Expand Down
3 changes: 0 additions & 3 deletions webapp/packages/core-product/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
},
{
"path": "../core-settings/tsconfig.json"
},
{
"path": "../core-utils/tsconfig.json"
}
],
"include": [
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
"@types/whatwg-mimetype": "^3.0.2",
"clsx": "^2.1.0",
"fast-deep-equal": "^3.1.3",
"i18next": "^23.11.2",
"md5": "^2.3.0",
"mobx": "^6.12.0",
"underscore": "^1.13.6",
"uuid": "^9.0.1",
"whatwg-mimetype": "^4.0.0",
"zod": "^3.22.4",
"zod-i18n-map": "^2.27.0",
"zod-validation-error": "^3.0.3"
},
"peerDependencies": {},
Expand Down
32 changes: 32 additions & 0 deletions webapp/packages/core-utils/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,43 @@
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import i18next from 'i18next';
import * as z from 'zod';
import { zodI18nMap } from 'zod-i18n-map';

i18next.init({
fallbackLng: 'en',
resources: {},
});
z.setErrorMap(zodI18nMap);

export const schemaExtra = {
stringedBoolean() {
return z.union([z.enum(['false', '0']).transform(() => false), z.boolean(), z.string(), z.number()]).pipe(z.coerce.boolean());
},

async loadLocale(code: string) {
let translation = {};
switch (code) {
case 'ru':
translation = await import('zod-i18n-map/locales/ru/zod.json');
break;
case 'en':
translation = await import('zod-i18n-map/locales/en/zod.json');
break;
case 'it':
translation = await import('zod-i18n-map/locales/it/zod.json');
break;
case 'zh':
translation = await import('zod-i18n-map/locales/zh-CN/zod.json');
break;
}

i18next.addResourceBundle(code, 'zod', translation, true);
},

async setLocale(code: string) {
i18next.changeLanguage(code);
},
};
export { z as schema };
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import { schema, schemaExtra } from '@cloudbeaver/core-utils';

import { DATA_EDITOR_SETTINGS_GROUP } from './DATA_EDITOR_SETTINGS_GROUP';

const FETCH_MIN = 10;
const FETCH_MAX = 5000;
const DEFAULT_FETCH_SIZE = 200;

const defaultSettings = schema.object({
'plugin.data-viewer.disableEdit': schemaExtra.stringedBoolean().default(false),
'plugin.data-viewer.disableCopyData': schemaExtra.stringedBoolean().default(false),
'plugin.data-viewer.fetchMin': schema.coerce.number().min(10).default(100),
'plugin.data-viewer.fetchMax': schema.coerce.number().min(10).default(5000),
'resultset.maxrows': schema.coerce.number().min(10).default(200),
'plugin.data-viewer.fetchMin': schema.coerce.number().min(FETCH_MIN).default(DEFAULT_FETCH_SIZE),
'plugin.data-viewer.fetchMax': schema.coerce.number().min(FETCH_MIN).default(FETCH_MAX),
'resultset.maxrows': schema.coerce.number().min(FETCH_MIN).max(FETCH_MAX).default(DEFAULT_FETCH_SIZE),
});

export type DataViewerSettings = schema.infer<typeof defaultSettings>;
Expand Down
5 changes: 4 additions & 1 deletion webapp/packages/plugin-localization/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"update-ts-references": "rimraf --glob dist && typescript-resolve-references"
},
"dependencies": {
"@cloudbeaver/core-di": "~0.1.0"
"@cloudbeaver/core-di": "~0.1.0",
"@cloudbeaver/core-localization": "~0.1.0",
"@cloudbeaver/core-utils": "~0.1.0",
"mobx": "^6.12.0"
},
"peerDependencies": {},
"devDependencies": {
Expand Down
20 changes: 19 additions & 1 deletion webapp/packages/plugin-localization/src/PluginBootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { autorun, type IReactionDisposer } from 'mobx';

import { Bootstrap, injectable } from '@cloudbeaver/core-di';
import { LocalizationService } from '@cloudbeaver/core-localization';
import { schemaExtra } from '@cloudbeaver/core-utils';

@injectable()
export class PluginBootstrap extends Bootstrap {
constructor() {
private reactionDisposer: IReactionDisposer | null;
constructor(private readonly localizationService: LocalizationService) {
super();
this.reactionDisposer = null;
}

register(): void {
this.reactionDisposer = autorun(() => schemaExtra.setLocale(this.localizationService.currentLanguage));
this.localizationService.addProvider(async locale => {
await schemaExtra.loadLocale(locale);
return [];
});
}

dispose(): void {
this.reactionDisposer?.();
}
}
6 changes: 6 additions & 0 deletions webapp/packages/plugin-localization/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"references": [
{
"path": "../core-di/tsconfig.json"
},
{
"path": "../core-localization/tsconfig.json"
},
{
"path": "../core-utils/tsconfig.json"
}
],
"include": [
Expand Down
1 change: 0 additions & 1 deletion webapp/packages/plugin-object-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@cloudbeaver/core-connections": "~0.1.0",
"@cloudbeaver/core-data-context": "~0.1.0",
"@cloudbeaver/core-di": "~0.1.0",
"@cloudbeaver/core-dialogs": "~0.1.0",
"@cloudbeaver/core-events": "~0.1.0",
"@cloudbeaver/core-executor": "~0.1.0",
"@cloudbeaver/core-localization": "~0.1.0",
Expand Down
3 changes: 0 additions & 3 deletions webapp/packages/plugin-object-viewer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
{
"path": "../core-di/tsconfig.json"
},
{
"path": "../core-dialogs/tsconfig.json"
},
{
"path": "../core-events/tsconfig.json"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const Setting = observer<Props>(function Setting({ source, setting }) {
return null;
}

return schemaValidationError(result.error).toString();
return schemaValidationError(result.error, { prefix: null }).toString();
});

function handleChange(value: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class UserProfileFormAuthenticationPart extends FormPart<IUserProfileForm
const validation = contexts.getContext(formValidationContext);

if (!state.success) {
validation.error(schemaValidationError(state.error).toString());
validation.error(schemaValidationError(state.error, { prefix: null }).toString());
return;
}

Expand Down
1 change: 1 addition & 0 deletions webapp/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"strictNullChecks": true,
"isolatedModules": true,
"allowArbitraryExtensions": true,
"resolveJsonModule": true,

"useDefineForClassFields": false,
"esModuleInterop": true,
Expand Down
12 changes: 12 additions & 0 deletions webapp/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10527,6 +10527,13 @@ husky@^9.0.11:
resolved "https://registry.yarnpkg.com/husky/-/husky-9.0.11.tgz#fc91df4c756050de41b3e478b2158b87c1e79af9"
integrity sha512-AB6lFlbwwyIqMdHYhwPe+kjOC3Oc5P3nThEoW/AaO2BX3vJDjWPFxYLxokUZOo6RNX20He3AaT8sESs9NJcmEw==

i18next@^23.11.2:
version "23.11.2"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-23.11.2.tgz#4c0e8192a9ba230fe7dc68b76459816ab601826e"
integrity sha512-qMBm7+qT8jdpmmDw/kQD16VpmkL9BdL+XNAK5MNbNFaf1iQQq35ZbPrSlqmnNPOSUY4m342+c0t0evinF5l7sA==
dependencies:
"@babel/runtime" "^7.23.2"

[email protected], iconv-lite@^0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down Expand Up @@ -19096,6 +19103,11 @@ yocto-queue@^1.0.0:
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==

zod-i18n-map@^2.27.0:
version "2.27.0"
resolved "https://registry.yarnpkg.com/zod-i18n-map/-/zod-i18n-map-2.27.0.tgz#ad4e7f702de4d1f49e3edc79c1ac147a56f17152"
integrity sha512-ORu9XpiVh3WDiEUs5Cr9siGgnpeODoBsTIgSD8sQCH9B//f9KowlzqHUEdPYb3vFonaSH8yPvPCOFM4niwp3Sg==

zod-validation-error@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/zod-validation-error/-/zod-validation-error-3.0.3.tgz#d2e0bbb1bb5fcf0ae406095180dc3c31d8145b8e"
Expand Down

0 comments on commit d28715c

Please sign in to comment.