Release NapCat / validate-version (push) Canceled after 0s
Release NapCat / Build-Framework (push) Canceled after 0s
Release NapCat / Build-Shell (push) Canceled after 0s
Release NapCat / Download-QNX64 (push) Canceled after 0s
Release NapCat / release-napcat (push) Canceled after 0s
Migrate schema validation from AJV to @sinclair/typebox across the monorepo. Key changes: - Core validation: ConfigBase now uses TypeCompiler/Value (TypeBox) for compile/defaults/coercion and replaces Ajv usage. - OneBot & Protocol: loadConfig functions and related config loaders updated to use Value.Default/Convert and TypeCompiler.Compile with improved error reporting. - OneBot action validation: OneBotAction now compiles TypeBox schemas, applies defaults/conversions and returns TypeBox errors. - Web UI backend: use TypeBox Value/TypeCompiler to fetch and validate defaults for Napcat/Web UI configs. - Types/scripts: removed AJV type shims and adjusted post-build script to stop special-casing AJV types. - Dependencies: removed ajv from several package.json files and added/normalized @sinclair/typebox usage; updated pnpm-lock accordingly. - Tests/tools: added schema validation test utilities and tests (packages/napcat-onebot/test_schema.ts and packages/napcat-test/schema.test.ts) to validate schema compilation, coercion, and defaults. This change standardizes on TypeBox for runtime schema compilation, default population and type coercion, simplifying validation logic and improving default handling across packages.
112 lines
4.8 KiB
TypeScript
112 lines
4.8 KiB
TypeScript
import { Type, Static } from '@sinclair/typebox';
|
|
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
import { Value } from '@sinclair/typebox/value';
|
|
const HttpServerConfigSchema = Type.Object({
|
|
name: Type.String({ default: 'http-server' }),
|
|
enable: Type.Boolean({ default: false }),
|
|
port: Type.Number({ default: 3000 }),
|
|
host: Type.String({ default: '127.0.0.1' }),
|
|
enableCors: Type.Boolean({ default: true }),
|
|
enableWebsocket: Type.Boolean({ default: false }),
|
|
messagePostFormat: Type.String({ default: 'array' }),
|
|
token: Type.String({ default: '' }),
|
|
debug: Type.Boolean({ default: false }),
|
|
});
|
|
|
|
const HttpSseServerConfigSchema = Type.Object({
|
|
name: Type.String({ default: 'http-sse-server' }),
|
|
enable: Type.Boolean({ default: false }),
|
|
port: Type.Number({ default: 3000 }),
|
|
host: Type.String({ default: '127.0.0.1' }),
|
|
enableCors: Type.Boolean({ default: true }),
|
|
enableWebsocket: Type.Boolean({ default: false }),
|
|
messagePostFormat: Type.String({ default: 'array' }),
|
|
token: Type.String({ default: '' }),
|
|
debug: Type.Boolean({ default: false }),
|
|
reportSelfMessage: Type.Boolean({ default: false }),
|
|
});
|
|
|
|
const HttpClientConfigSchema = Type.Object({
|
|
name: Type.String({ default: 'http-client' }),
|
|
enable: Type.Boolean({ default: false }),
|
|
url: Type.String({ default: 'http://localhost:8080' }),
|
|
messagePostFormat: Type.String({ default: 'array' }),
|
|
reportSelfMessage: Type.Boolean({ default: false }),
|
|
token: Type.String({ default: '' }),
|
|
debug: Type.Boolean({ default: false }),
|
|
});
|
|
|
|
const WebsocketServerConfigSchema = Type.Object({
|
|
name: Type.String({ default: 'websocket-server' }),
|
|
enable: Type.Boolean({ default: false }),
|
|
host: Type.String({ default: '127.0.0.1' }),
|
|
port: Type.Number({ default: 3001 }),
|
|
messagePostFormat: Type.String({ default: 'array' }),
|
|
reportSelfMessage: Type.Boolean({ default: false }),
|
|
token: Type.String({ default: '' }),
|
|
enableForcePushEvent: Type.Boolean({ default: true }),
|
|
debug: Type.Boolean({ default: false }),
|
|
heartInterval: Type.Number({ default: 30000 }),
|
|
});
|
|
|
|
const WebsocketClientConfigSchema = Type.Object({
|
|
name: Type.String({ default: 'websocket-client' }),
|
|
enable: Type.Boolean({ default: false }),
|
|
url: Type.String({ default: 'ws://localhost:8082' }),
|
|
messagePostFormat: Type.String({ default: 'array' }),
|
|
reportSelfMessage: Type.Boolean({ default: false }),
|
|
reconnectInterval: Type.Number({ default: 5000 }),
|
|
token: Type.String({ default: '' }),
|
|
debug: Type.Boolean({ default: false }),
|
|
heartInterval: Type.Number({ default: 30000 }),
|
|
});
|
|
|
|
const PluginConfigSchema = Type.Object({
|
|
name: Type.String({ default: 'plugin' }),
|
|
enable: Type.Boolean({ default: false }),
|
|
messagePostFormat: Type.String({ default: 'array' }),
|
|
reportSelfMessage: Type.Boolean({ default: false }),
|
|
debug: Type.Boolean({ default: false }),
|
|
});
|
|
|
|
const NetworkConfigSchema = Type.Object({
|
|
httpServers: Type.Array(HttpServerConfigSchema, { default: [] }),
|
|
httpSseServers: Type.Array(HttpSseServerConfigSchema, { default: [] }),
|
|
httpClients: Type.Array(HttpClientConfigSchema, { default: [] }),
|
|
websocketServers: Type.Array(WebsocketServerConfigSchema, { default: [] }),
|
|
websocketClients: Type.Array(WebsocketClientConfigSchema, { default: [] }),
|
|
plugins: Type.Array(PluginConfigSchema, { default: [] }),
|
|
}, { default: {} });
|
|
|
|
export const OneBotConfigSchema = Type.Object({
|
|
network: NetworkConfigSchema,
|
|
musicSignUrl: Type.String({ default: '' }),
|
|
enableLocalFile2Url: Type.Boolean({ default: false }),
|
|
parseMultMsg: Type.Boolean({ default: false }),
|
|
imageDownloadProxy: Type.String({ default: '' }),
|
|
});
|
|
|
|
export type OneBotConfig = Static<typeof OneBotConfigSchema>;
|
|
export type HttpServerConfig = Static<typeof HttpServerConfigSchema>;
|
|
export type HttpSseServerConfig = Static<typeof HttpSseServerConfigSchema>;
|
|
export type HttpClientConfig = Static<typeof HttpClientConfigSchema>;
|
|
export type WebsocketServerConfig = Static<typeof WebsocketServerConfigSchema>;
|
|
export type WebsocketClientConfig = Static<typeof WebsocketClientConfigSchema>;
|
|
export type PluginConfig = Static<typeof PluginConfigSchema>;
|
|
|
|
export type NetworkAdapterConfig = HttpServerConfig | HttpSseServerConfig | HttpClientConfig | WebsocketServerConfig | WebsocketClientConfig | PluginConfig;
|
|
export type NetworkConfigKey = keyof OneBotConfig['network'];
|
|
|
|
export function loadConfig (config: Partial<OneBotConfig>): OneBotConfig {
|
|
let data = config;
|
|
data = Value.Default(OneBotConfigSchema, data) as Partial<OneBotConfig>;
|
|
data = Value.Convert(OneBotConfigSchema, data) as Partial<OneBotConfig>;
|
|
const validate = TypeCompiler.Compile(OneBotConfigSchema);
|
|
const valid = validate.Check(data);
|
|
if (!valid) {
|
|
const errorMsg = [...validate.Errors(data)].map(e => e.message).join(', ');
|
|
throw new Error(errorMsg);
|
|
}
|
|
return data as OneBotConfig;
|
|
}
|