简体   繁体   English

使用任意键进行JSON模式验证

[英]JSON schema validation with arbitrary keys

I am using validictory for validating the attached JSON data and schema. 我正在使用validictory来验证附加的JSON数据和架构。 Working so far. 到目前为止工作。

However the data dictionary can have arbitrary string keys (others than 'bp' but). 但是,数据字典可以具有任意字符串键(除了“ bp”以外)。 The key 'bp' in the schema here is hard-coded...it can be a string from a given list (enum of string). 模式中的键“ bp”是硬编码的,它可以是给定列表中的字符串(字符串枚举)。 How do I add the enum definition here for the "first level" of the dict. 如何在此处为字典的“第一级”添加枚举定义。

import json
import validictory

data = {'bp': [{'category': 'bp',
         'created': '2013-03-08T09:14:48.148000',
         'day': '2013-03-11T00:00:00',
         'id': 'dc049c0e-d19a-4e3e-93ea-66438a239712',
         'unit': 'mmHg',
         'value': 147.0,
         'value2': 43.0}]}


schema = {
    "type":"object",
    "properties":{
        "bp": {
            "type":"array",
            "required":False,
            "items":
                {
                    "type":"object",
                    "required":False,
                    "properties":{
                        "category": {
                            "type":"string",
                            "default": "bp",
                            "required":False
                        },
                        "created": {
                            "type":"string",
                            "default": "2013-03-08T09:14:48.148000",
                            "required":False
                        },
                        "day": {
                            "type":"string",
                            "default": "2013-03-11T00:00:00",
                            "required":False
                        },
                        "id": {
                            "type":"string",
                            "default": "dc049c0e-d19a-4e3e-93ea-66438a239712",
                            "required":False
                        },
                        "unit": {
                            "type":"string",
                            "default": "mmHg",
                            "required":False
                        },
                        "value2": {
                            "type":"number",
                            "default":43,
                            "required":False
                        },
                        "value": {
                            "type":"number",
                            "default":147,
                            "required":False
                        }
                    }
                }


        }
    }
}

validictory.validate(data,schema)

It depends on exactly what you're trying to do. 这完全取决于您要执行的操作。

If you want the same specification, but for a range of properties, you can abstract out the definition: 如果您希望使用相同的规范,但需要一系列属性,则可以抽象出定义:

{
    "type": "object",
    "properties": {
        "bp": {"$ref": "#/definitions/categoryList"},
        "foo": {"$ref": "#/definitions/categoryList"},
        "bar": {"$ref": "#/definitions/categoryList"}
    },
    "definitions": {
        "categoryList": {...}
    }
}

If you want any properties to follow that schema, you can use additionalProperties : 如果您希望任何属性遵循该架构,则可以使用additionalProperties

{
    "type": "object",
    "additionalProperties": {...}
}

Or a range of properties (matched by a pattern) - for instance, anything lower-case: 或一系列属性(由模式匹配)-例如,任何小写字母:

{
    "type": "object",
    "patternProperties": {
        "^[a-z]+$": {...}
    }
}

If you want to limit the number of properties that can be defined, then you can use "maxProperties" (v4 of the standard only): 如果要限制可以定义的属性的数量,则可以使用“ maxProperties”(仅标准版v4):

{
    "type": "object",
    "additionalProperties": {...},
    "maxProperties": 1
}

PS - in v4 of the standard, "required" is an array. PS-在标准的v4中,“必需”是一个数组。 In fact, even in v3, "required" defaults to false , so your example doesn't need it at all 实际上,即使在v3中,“ required”也默认为false ,因此您的示例根本不需要它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM