AVJ 不验证枚举类型

AVJ not validating enum types

如果有人问过这个问题,我深表歉意,但我找不到有效的答案

我在使用 enum 类型和 AVJ

验证 JSON 架构时遇到问题

我希望下面的代码 return 为假,因为给定的值没有出现在 enum 类型

var Ajv = require('ajv');
var ajv = new Ajv();

var schema = {
  gender: {
    enum: [
      'male',
      'female',
      'other'
    ]
  }
};
ajv.validate(schema, { gender: 'test' });
// returns true

你能告诉我如何解决这个问题吗

在 JSON 架构中,架构中的所有属性都是名为 keywords 的指令。忽略未知关键字。

在您的架构中,"gender" 不是已知的 JSON 架构关键字,因此将被忽略。您可能正在寻找 "properties" 关键字:

{
  properties: {
    "gender": {
      enum: ["male", "female", "other"]
    }
  }
}