mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
- 实现 parseSchemaContent 函数解析对象和数组枚举元数据 - 添加 validateParsedConfig 验证对象值是否在枚举声明范围内 - 支持数组枚举候选项的顺序敏感比较 - 优化诊断信息避免父对象枚举不匹配的重复报告 - 添加测试用例验证枚举对象和数组的解析与验证功能 - 实现可编辑字段收集功能支持批量编辑器更新 - 添加 YAML 解析和注释提取功能用于表单预览 - 实现配置验证诊断生成功能支持本地化消息 - 添加格式化和规范化函数支持不同数据类型的处理
164 lines
4.6 KiB
JavaScript
164 lines
4.6 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const {
|
|
parseSchemaContent,
|
|
parseTopLevelYaml,
|
|
validateParsedConfig
|
|
} = require("../src/configValidation");
|
|
|
|
test("parseSchemaContent should capture object and array enum comparable metadata", () => {
|
|
const schema = parseSchemaContent(`
|
|
{
|
|
"type": "object",
|
|
"properties": {
|
|
"reward": {
|
|
"type": "object",
|
|
"properties": {
|
|
"gold": { "type": "integer" },
|
|
"itemId": { "type": "string" }
|
|
},
|
|
"enum": [
|
|
{ "gold": 10, "itemId": "potion" }
|
|
]
|
|
},
|
|
"dropItemIds": {
|
|
"type": "array",
|
|
"items": { "type": "string" },
|
|
"enum": [
|
|
["fire", "ice"]
|
|
]
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
|
|
assert.deepEqual(schema.properties.reward.enumDisplayValues, ["{\"gold\":10,\"itemId\":\"potion\"}"]);
|
|
assert.match(schema.properties.reward.enumComparableValues[0], /^4:gold=/u);
|
|
assert.deepEqual(schema.properties.dropItemIds.enumDisplayValues, ["[\"fire\",\"ice\"]"]);
|
|
assert.equal(schema.properties.dropItemIds.enumComparableValues[0], "[13:string:4:fire,12:string:3:ice]");
|
|
});
|
|
|
|
test("validateParsedConfig should reject object values not declared in object enum", () => {
|
|
const schema = parseSchemaContent(`
|
|
{
|
|
"type": "object",
|
|
"required": ["reward"],
|
|
"properties": {
|
|
"reward": {
|
|
"type": "object",
|
|
"required": ["gold", "itemId"],
|
|
"properties": {
|
|
"gold": { "type": "integer" },
|
|
"itemId": { "type": "string" }
|
|
},
|
|
"enum": [
|
|
{ "gold": 10, "itemId": "potion" }
|
|
]
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
const yaml = parseTopLevelYaml(`
|
|
reward:
|
|
gold: 10
|
|
itemId: elixir
|
|
`);
|
|
|
|
const diagnostics = validateParsedConfig(schema, yaml);
|
|
|
|
assert.equal(diagnostics.length, 1);
|
|
assert.match(diagnostics[0].message, /reward/u);
|
|
assert.match(diagnostics[0].message, /"itemId":"potion"/u);
|
|
});
|
|
|
|
test("validateParsedConfig should treat array enum candidates as order-sensitive", () => {
|
|
const schema = parseSchemaContent(`
|
|
{
|
|
"type": "object",
|
|
"required": ["dropItemIds"],
|
|
"properties": {
|
|
"dropItemIds": {
|
|
"type": "array",
|
|
"items": { "type": "string" },
|
|
"enum": [
|
|
["fire", "ice"]
|
|
]
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
const yaml = parseTopLevelYaml(`
|
|
dropItemIds:
|
|
- ice
|
|
- fire
|
|
`);
|
|
|
|
const diagnostics = validateParsedConfig(schema, yaml);
|
|
|
|
assert.equal(diagnostics.length, 1);
|
|
assert.match(diagnostics[0].message, /dropItemIds/u);
|
|
assert.match(diagnostics[0].message, /\["fire","ice"\]/u);
|
|
});
|
|
|
|
test("validateParsedConfig should not add parent object enumMismatch after child diagnostics", () => {
|
|
const schema = parseSchemaContent(`
|
|
{
|
|
"type": "object",
|
|
"required": ["reward"],
|
|
"properties": {
|
|
"reward": {
|
|
"type": "object",
|
|
"required": ["gold", "itemId"],
|
|
"properties": {
|
|
"gold": { "type": "integer" },
|
|
"itemId": { "type": "string" }
|
|
},
|
|
"enum": [
|
|
{ "gold": 10, "itemId": "potion" }
|
|
]
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
const yaml = parseTopLevelYaml(`
|
|
reward:
|
|
gold: wrong
|
|
itemId: potion
|
|
`);
|
|
|
|
const diagnostics = validateParsedConfig(schema, yaml);
|
|
|
|
assert.equal(diagnostics.length, 1);
|
|
assert.match(diagnostics[0].message, /reward\.gold/u);
|
|
assert.doesNotMatch(diagnostics[0].message, /must be one of|必须是以下值之一/u);
|
|
});
|
|
|
|
test("validateParsedConfig should not add parent array enumMismatch after item diagnostics", () => {
|
|
const schema = parseSchemaContent(`
|
|
{
|
|
"type": "object",
|
|
"required": ["dropLevels"],
|
|
"properties": {
|
|
"dropLevels": {
|
|
"type": "array",
|
|
"items": { "type": "integer" },
|
|
"enum": [
|
|
[1, 2]
|
|
]
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
const yaml = parseTopLevelYaml(`
|
|
dropLevels:
|
|
- 1
|
|
- two
|
|
`);
|
|
|
|
const diagnostics = validateParsedConfig(schema, yaml);
|
|
|
|
assert.equal(diagnostics.length, 1);
|
|
assert.match(diagnostics[0].message, /dropLevels\[1\]/u);
|
|
assert.doesNotMatch(diagnostics[0].message, /must be one of|必须是以下值之一/u);
|
|
});
|