验证 JSON 架构中的强制字符串值

Validating Mandatory String values in JSON Schema

我有以下示例架构。我需要确保在 json 文件

中至少出现一次 "name": "This is Mandatory"

有可能实现吗?请帮忙。

   "SchemaList": {
      "type": "array",      
      "additionalItems": false,
      "items": { "$ref": "#/definitions/Schema1" }          
        },
   "Schema1": {
      "type": "object",         
      "properties": {
        "description": { "type": "string" },
        "name": { "type": "string" }        
            }
        }

请参阅此link了解更多详情:http://json-schema.org/example1.html

{
"SchemaList": {
    "type": "array",
    "additionalItems": false,
    "items": {
        "$ref": "#/definitions/Schema1"
    }
},
"Schema1": {
    "type": "object",
    "properties": {
        "description": {
            "type": "string"
        },
        "name": {
            "type": "string"
        }
    }
},
"required": ["name"]
}

最新的草案(草案 6)引入了一个新的关键字 "contains" ,您可以用它来表示给定的模式应该至少匹配数组中的一个元素。你可以这样使用它:

{
    "SchemaList": {
        "additionalItems": false,
        "contains": {
            "properties": {
                "name": {
                    "const": "This is Mandatory"
                }
            },
            "required": [
                "name"
            ]
        },
        "items": {
            "$ref": "#/definitions/Schema1"
        },
        "type": "array"
    }
}

但请记住 the latest draft version of json-schema has only a very few implementations ,因此根据您使用的库,"contains" 关键字可能不起作用。