0

我正在使用节点 9.2.0 和 ajv 6.0.0。

我有一个模式,我希望在其上使用否定的lookbehind,它的定义如下:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "release format",
    "description": "A Release Format",
    "type": "object",
    "properties": {
        "id": {"type": "integer"},
        "title": {
            "anyOf": [
                {
                    "type": "string",
                    "pattern": "^((?!itunes).)*$"
                },
                {
                    "type": "string",
                    "pattern": "^((?!exclusive).)*$"
                },
                {
                    "type": "string",
                    "pattern": "^((?!ringtone).)*$"
                }
            ]
        }
    }
}

但是,似乎当我尝试使用以下数据通过 AJV 验证这一点时:{"id": 123, "title": "world exclusive"}我没有收到验证错误。

编码:

const Ajv = require('ajv');
class Validator {
  constructor() {
    this.releaseFormatSchema = JSON.parse(fs.readFileSync('./schemas/release-format.json'));
    this.schemaValidator = new Ajv({allErrors: true});
  }

  validate(data) {
    let validate = this.schemaValidator.compile(this.releaseFormatSchema);
    let valid = validate(data);
    console.log(valid);
    console.log(validate);
  }
}

数据在哪里:{"id": 123, "title": "world exclusive"}. 我希望这会出错,但它目前告诉我数据是有效的。

4

1 回答 1

0

@sln 和 @ClasG 也找到了答案,任何标题模式之间的联合都可以匹配:“所有除了包含 iTunes 的字符串”联合“所有除了包含独占的字符串”联合“...”,这意味着所有不包含所有禁止的关键字。也可以固定

  • 使用allOf而不是anyOF

    "title": {
        "allOf": [
            {
                "type": "string",
                "pattern": "^((?!itunes).)*$"
            },
            {
                "type": "string",
                "pattern": "^((?!exclusive).)*$"
            },
            {
                "type": "string",
                "pattern": "^((?!ringtone).)*$"
            }
        ]
    }
    
  • 使用单一类型/模式:

    "title": {
        "type": "string",
        "pattern": "^((?!itunes|exclusive|ringtone).)*$"
    }
    
于 2018-01-24T08:19:32.753 回答