chore(server): validate function not actually used (#11263)

This commit is contained in:
forehalo
2025-03-28 12:51:19 +00:00
parent 387f7211bf
commit 64c7fb1d66
3 changed files with 23 additions and 8 deletions

View File

@@ -37,7 +37,7 @@ export type ConfigDescriptor<T> = {
type ConfigDefineDescriptor<T> = {
desc: string;
default: T;
validate?: (value: T) => boolean;
validate?: (value: T) => z.SafeParseReturnType<T, T>;
shape?: z.ZodType<T>;
env?: string | [string, EnvConfigType];
link?: string;
@@ -158,7 +158,7 @@ function standardizeDescriptor<T>(
default: desc.default,
type,
validate: (value: T) => {
return shape.safeParse(value);
return desc.validate ? desc.validate(value) : shape.safeParse(value);
},
env,
link: desc.link,
@@ -257,7 +257,15 @@ export function getDefaultConfig(): AppConfigSchema {
const { success, error } = desc.validate(defaultValue);
if (!success) {
throw error;
throw new Error(
error.issues
.map(issue => {
return `Invalid config for module [${module}] with key [${key}]
Value: ${JSON.stringify(defaultValue)}
Error: ${issue.message}`;
})
.join('\n')
);
}
set(modulizedConfig, key, defaultValue);

View File

@@ -23,7 +23,7 @@ defineModuleConfig('redis', {
desc: 'The database index of redis server to be used(Must be less than 10).',
default: 0,
env: ['REDIS_SERVER_DATABASE', 'integer'],
validate: val => val >= 0 && val < 10,
shape: z.number().int().nonnegative().max(10),
},
host: {
desc: 'The host of the redis server.',

View File

@@ -9,12 +9,12 @@ export interface ServerFlags {
declare global {
interface AppConfigSchema {
server: {
externalUrl: string;
externalUrl?: string;
https: boolean;
host: string;
port: number;
path: string;
name: string | undefined;
name?: string;
};
flags: ServerFlags;
}
@@ -29,9 +29,16 @@ defineModuleConfig('server', {
desc: `Base url of AFFiNE server, used for generating external urls.
Default to be \`[server.protocol]://[server.host][:server.port]\` if not specified.
`,
default: 'http://localhost:3010',
default: '',
env: 'AFFINE_SERVER_EXTERNAL_URL',
shape: z.string().url(),
validate: val => {
// allow to be nullable and empty string
if (!val) {
return { success: true, data: val };
}
return z.string().url().safeParse(val);
},
},
https: {
desc: 'Whether the server is hosted on a ssl enabled domain (https://).',