Json Schema:validation数组

我试图validation包含两个键, tagsparameters的模式,这些键是用于任意键值对的数组。 出于某种原因,但我无法得到任何我指定这两个键validation失败(我使用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"] }; 

这里是一个testing对象。 我在这里传递parameters作为一个简单的string,意图它validation失败,但它实际上传递:

 var spec = { name: "test", account: "test", environment: "test", parameters: "test", application: "test" }; 

这里是我用来validation的代码:

  var ajv = new Ajv({ useDefaults: true }); var validate = ajv.compile(cfStackSchema); if (!validate(spec)) { throw new Error('Stack does not match schema!') } 

你只需要把属性放在一个properties对象中

 var cfStackSchema = { properties: { 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"] };