在 jsonschema 中使用负后视
Using negative lookbehind with jsonschema
我正在使用 Node 9.2.0 和 ajv 6.0.0。
我有一个模式,我希望在其上使用负面回顾,它的定义如下:
{
"$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"}
。我希望这会出错,但它目前告诉我数据有效。
@sln 和@ClasG 也找到了答案,anyOf does a union between titles patterns can match : "all except strings which contains itunes" union "all except strings which contains exclusive" union "...", which means all which not包含所有禁止的关键字。也可以修复
使用 allOf
而不是 anyOF
"title": {
"allOf": [
{
"type": "string",
"pattern": "^((?!itunes).)*$"
},
{
"type": "string",
"pattern": "^((?!exclusive).)*$"
},
{
"type": "string",
"pattern": "^((?!ringtone).)*$"
}
]
}
使用单个 type/pattern :
"title": {
"type": "string",
"pattern": "^((?!itunes|exclusive|ringtone).)*$"
}
我正在使用 Node 9.2.0 和 ajv 6.0.0。
我有一个模式,我希望在其上使用负面回顾,它的定义如下:
{
"$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"}
。我希望这会出错,但它目前告诉我数据有效。
@sln 和@ClasG 也找到了答案,anyOf does a union between titles patterns can match : "all except strings which contains itunes" union "all except strings which contains exclusive" union "...", which means all which not包含所有禁止的关键字。也可以修复
使用
allOf
而不是anyOF
"title": { "allOf": [ { "type": "string", "pattern": "^((?!itunes).)*$" }, { "type": "string", "pattern": "^((?!exclusive).)*$" }, { "type": "string", "pattern": "^((?!ringtone).)*$" } ] }
使用单个 type/pattern :
"title": { "type": "string", "pattern": "^((?!itunes|exclusive|ringtone).)*$" }