我正在尝试验证一个包含两个键的模式tags
和parameters
,它们旨在成为任意键值对的数组。但是,由于某种原因,我无法为这两个键指定任何验证失败(我正在使用 nodejs 库ajv
)。
这是架构定义:
var cfStackSchema = {
name: { type: "string" },
application: { type: "string" },
account: { type: "string" },
environment: { type: "string" },
tags: {
type: "array",
items: {
type: "object",
patternProperties: {
"^[a-zA-z0-9]$": { type: "string" }
},
additionalProperties: false
},
additionalItems: false
},
parameters: {
type: "array",
items: {
type: "object",
patternProperties: {
"^[a-zA-z0-9]$": { type: "string" }
},
additionalProperties: false
},
additionalItems: false
},
deps: { type: "array", items: { type: "string" } },
required: ["name", "account", "environment", "parameters", "application"]
};
这是一个测试对象。我在parameters
这里作为一个简单的字符串传递,打算让它验证失败,但它实际上通过了:
var spec = { name: "test",
account: "test",
environment: "test",
parameters: "test",
application: "test"
};
这是我用来验证的代码:
var ajv = new Ajv({ useDefaults: true });
var validate = ajv.compile(cfStackSchema);
if (!validate(spec)) {
throw new Error('Stack does not match schema!')
}