From 24cc2d49dfca5d46b98fe7f6a04d5f60b618f008 Mon Sep 17 00:00:00 2001 From: Chareice Date: Fri, 24 Jan 2025 21:21:11 +0800 Subject: [PATCH] chore(enviroment-variables): validateTexts api --- .../src/server/plugin.ts | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-environment-variables/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-environment-variables/src/server/plugin.ts index 070d008ad6..2ba414ebc8 100644 --- a/packages/plugins/@nocobase/plugin-environment-variables/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-environment-variables/src/server/plugin.ts @@ -51,7 +51,53 @@ export class PluginEnvironmentVariablesServer extends Plugin { return items.map(({ name, type }) => ({ name, type })); } + public validateTexts(texts: Array<{ text: string; secret: boolean }>) { + if (!Array.isArray(texts)) { + throw new Error('texts parameter must be an array'); + } + + for (const item of texts) { + if (!item || typeof item !== 'object') { + throw new Error('Each item in texts must be an object'); + } + + if (typeof item.text !== 'string' || !item.text.trim()) { + throw new Error('text property must be a non-empty string'); + } + + if (typeof item.secret !== 'boolean') { + throw new Error('secret property must be a boolean'); + } + + // Check each line for empty values + const lines = item.text + .split('\n') + .map((line) => line.trim()) + .filter((line) => line && !line.startsWith('#')); + + for (const line of lines) { + const equalIndex = line.indexOf('='); + if (equalIndex === -1) { + throw new Error(`Invalid environment variable format: ${line}`); + } + + const key = line.slice(0, equalIndex).trim(); + const value = line.slice(equalIndex + 1).trim(); + + if (!key) { + throw new Error(`Environment variable name cannot be empty`); + } + + if (!value) { + throw new Error(`Environment variable "${key}" must have a value`); + } + } + } + } + async setEnvironmentVariablesByText(texts: Array<{ text: string; secret: boolean }>) { + this.validateTexts(texts); + /* text format: KEY1=VALUE1 @@ -79,11 +125,15 @@ export class PluginEnvironmentVariablesServer extends Plugin { const key = line.slice(0, equalIndex).trim(); const value = line.slice(equalIndex + 1).trim(); - if (!key || !value) { - this.app.log.warn(`Empty key or value found: ${line}`); + if (!key) { + this.app.log.warn(`Empty key found: ${line}`); continue; } + if (!value) { + throw new Error(`Empty value is not allowed for key: ${key}`); + } + await repository.create({ values: { name: key,