Files
NapCatQQ/packages/napcat-onebot/test_schema.ts
T
手瓜一十雪 36ae7bf923
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
Replace AJV with TypeBox validation
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.
2026-03-06 09:46:12 +08:00

62 lines
1.9 KiB
TypeScript

import { TypeCompiler } from '@sinclair/typebox/compiler';
import { Value } from '@sinclair/typebox/value';
import { OB11MessageDataSchema, OB11MessageSchema, OB11PostSendMsgSchema } from './types/message';
import { OB11MessageSchema as OB11ActionMessageSchema } from './action/schemas';
try {
TypeCompiler.Compile(OB11MessageDataSchema);
console.log("Compile OB11MessageDataSchema OK");
} catch (e: any) {
console.error("Error in OB11MessageDataSchema:", e.message);
}
try {
TypeCompiler.Compile(OB11MessageSchema);
console.log("Compile OB11MessageSchema OK");
} catch (e: any) {
console.error("Error in OB11MessageSchema:", e.message);
}
try {
TypeCompiler.Compile(OB11ActionMessageSchema);
console.log("Compile OB11ActionMessageSchema OK");
} catch (e: any) {
console.error("Error in OB11ActionMessageSchema:", e.message);
}
import { Type } from '@sinclair/typebox';
try {
const QA = Type.Object({ message: OB11PostSendMsgSchema, quick_action: OB11ActionMessageSchema });
TypeCompiler.Compile(QA);
console.log("Compile Quick Action OK");
} catch (e: any) {
console.error("Error in Quick Action:", e.message);
}
// Test validation
const testObj = {
message_type: 'private',
user_id: 12345,
message: [
{ type: 'text', data: { text: 'hello' } }
]
};
try {
let data = structuredClone(testObj);
data = Value.Default(OB11PostSendMsgSchema, data) as any;
data = Value.Convert(OB11PostSendMsgSchema, data) as any;
const compiler = TypeCompiler.Compile(OB11PostSendMsgSchema);
const isValid = compiler.Check(data);
console.log("Validation result:", isValid);
if (!isValid) {
const errors = [...compiler.Errors(data)];
errors.forEach(e => console.error(e.path, e.message));
} else {
console.log("Data after Convert and Check:", JSON.stringify(data, null, 2));
}
} catch (e: any) {
console.error("Test validation error:", e.message);
}