在JSON模式中验证强制字符串值

时间:2017-09-01 09:14:19

标签: json jsonschema json-schema-validator

我有以下示例架构。我需要确保至少应该出现一次" name":"这是强制性的"在json文件中

有可能实现这一目标吗?请帮助。

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

2 个答案:

答案 0 :(得分:0)

请参阅此链接了解详情:http://json-schema.org/example1.html

    {
  "aggregateBy": [{
    "dataTypeName": "com.google.step_count.delta",
    "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
  }],
  "bucketByTime": { "durationMillis": 86400000 }, // This is 24 hours
  "startTimeMillis": 1504137600000, //start time
  "endTimeMillis": 1504310400000 // End Time
}

答案 1 :(得分:0)

最新草稿(草案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"关键字可能无效。