简体   繁体   English

如何格式化 JSON 模式以进行验证

[英]How to format JSON schema for validation

I am writing a program with Python/Flask and am using a package called flask-jsonschema-validator for JSON validation.我正在使用 Python/Flask 编写程序,并且正在使用名为 flask-jsonschema-validator 的 package 进行 JSON 验证。 When I validate my JSON I get the following error:当我验证我的 JSON 时,我收到以下错误:

jsonschema.exceptions.SchemaError: [{'fieldname': {'type': 'string'}, 'type': {'type': 'string'}, 'description': {'type': 'string'}, 'default-value': {'type': 'string'}, 'validation': [{'type': 'string'}]}, {'fieldname': {'type': 'string'}, 'type': {'type': 'string'}, 'description': {'type': 'string'}, 'default-value': {'type': 'string'}, 'validation': [{'valid-values': {'type': 'string'}}, {'invalid-values': {'type': 'string'}}, {'isinteger': {'type': 'string'}}, {'nullable': {'type': 'string'}}]}] is not of type 'object', 'boolean'

Here is the JSON schema I am using这是我正在使用的 JSON 架构

    {
    "validate": {
      "type": "object",
      "properties": {    
        "_id": { "type": "string", "minLength": 2, "maxLength": 100 },
        "name": { "type": "string", "minLength": 2, "maxLength": 100 },
        "type": { "type": "string", "minLength": 2, "maxLength": 100 },
        "subtype": { "type": "string", "minLength": 2, "maxLength": 100 },
        "domain-data-version": {"type": "string"},
        "description": { "type": "string", "minLength": 2, "maxLength": 100 },
        "created" : {"type": "string"},
        "owner-org": {"type": "string"},
        "domain-data":[
          {
           "fieldname": {"type": "string"},
            "type": {"type": "string"},
            "description": {"type": "string"},
            "default-value": {"type": "string"},
            "validation": [{"type": "string"}]},
          {"fieldname": {"type": "string"},
            "type": {"type": "string"},
            "description": {"type": "string"},
            "default-value": {"type": "string"},
            "validation": [ {"valid-values": {"type": "string"}},
                            {"invalid-values": {"type": "string"}},
                            {"isinteger": {"type": "string"}},
                            {"nullable": {"type": "string"}}]
                      }]
      },
      "required": []
    }
  }

Here is the JSON I would use to validate这是我用来验证的 JSON

{"name": "PHARMACY-CLAIM", "type": "Pharmacy", "subtype": "Prescription Filled", "domain-data-version": "1", "domain-data": [{"fieldname": "claim-id", "type": "string", "description": "The Insurance claim ID", "default-value": "null", "validation": [{"nullable": "false"}]}, {"fieldname": "member-gen-key", "type": "string", "description": "The unique insurance Member ID", "default-value": "null", "validation": [{"nullable": "false"}]}, {"fieldname": "ndc", "type": "string", "description": "The National Drug Code value for the medication prescription filled.", "default-value": "null", "validation": [{"nullable": "false"}]}]}

When I remove the "domain-data portion of the schema, the JSON will validate. I think the problem comes from the fact the domian-data is an array of objects but I am not sure what to do about it. Thanks for the help.当我删除模式的“域数据部分”时,JSON 将验证。我认为问题出在域数据是一个对象数组的事实,但我不知道该怎么做。感谢您的帮助.

The first hint here is that the error is a SchemaError , which means there's a problem with how you've written your schema, the validator hasn't even gotten around to looking at the instance value yet.这里的第一个提示是错误是SchemaError ,这意味着您编写架构的方式存在问题,验证器甚至还没有开始查看实例值。

The second hint is the error message lists the value of the "domain-data" property in your schema, then says ... is not of type 'object', 'boolean' .第二个提示是错误消息列出了架构中“域数据”属性的值,然后说... is not of type 'object', 'boolean' Whereas you specified an array, JSON Schema only allows schemas as values in a "properties" keyword — which requires an object (or a boolean).虽然您指定了一个数组,但 JSON 模式只允许模式作为“属性”关键字中的值——这需要一个对象(或布尔值)。

The solution is to fix the "domain-data" property in your schema, to be a valid subschema.解决方案是修复架构中的“域数据”属性,使其成为有效的子架构。

If you intend for the property to validate arrays where all the items follow the same schema, use:如果您打算让该属性验证所有项目都遵循相同架构的数组,请使用:

{ "type": "array", "items": {...} }

If you want the array to be a tuple, where different positions in the array take different schemas, use:如果您希望数组是一个元组,其中数组中的不同位置采用不同的模式,请使用:

{ "type": "array", "items": [ {first}, {second}, ... ] }

Consult the JSON Schema documentation on the difference between "items" and "additionalItems" if you do this.如果您这样做,请查阅 JSON Schema 文档,了解“items”和“additionalItems”之间的区别。

I had the same problem as you.我和你有同样的问题。 Although it seems to be a schema error, it was a problem with JSON manipulation for me.虽然这似乎是一个架构错误,但对我来说这是一个 JSON 操作的问题。 I was working with a string content and not with a JSON object.我正在处理字符串内容而不是 JSON 对象。 I've built your schema and it succeeded.我已经建立了你的架构并且它成功了。 I put your schema in a file called teste.json.我将您的架构放在一个名为 teste.json 的文件中。

>>> from jsonschema import Draft7Validator
>>> import json
>>> Draft7Validator.check_schema(json.load(open('teste.json')))

Probably you are not loading this content as a JSON object ( json.load() ).可能您没有将此内容作为 JSON 对象 ( json.load() ) 加载。 FYI, Draft7Validator is an implementation class for validating 7th version of JSON schema.仅供参考, Draft7Validator是用于验证第 7 版 JSON 模式的实现类。 Use the one that suits your needs.使用适合您需求的那一种。

About your schema, I suggest you to follow JSON Schema documentation.关于您的架构,我建议您遵循 JSON Schema 文档。 I couldn't validate your schema and instance without using definitions .如果不使用定义,我无法验证您的架构和实例。 It seems that you have two kinds of validation for your domain data.您的域数据似乎有两种验证方式。 Then using extension will avoid code repetition, as illustrated here .然后使用扩展将避免重复的代码,如所示在这里 I refactored your schema and it seems to work as expected.我重构了您的架构,它似乎按预期工作。

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Validate",
    "definitions": {
        "Validate": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "_id": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 100
                },
                "name": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 100
                },
                "type": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 100
                },
                "subtype": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 100
                },
                "domain-data-version": {
                    "type": "string"
                },
                "description": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 100
                },
                "created": {
                    "type": "string"
                },
                "owner-org": {
                    "type": "string"
                },
                "domain-data": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Domain-data"
                    }
                }
            }
        },
        "Domain-data": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "fieldname": {
                    "type": "string"
                },
                "type": {
                    "type": "string"
                },
                "description": {
                    "type": "string"
                },
                "default-value": {
                    "type": "string"
                },
                "validation": {
                    "oneOf": [
                        { "$ref": "#/definitions/DomainDataV1" },
                        { "$ref": "#/definitions/DomainDataV2" }
                    ]
                }
            }
        },
        "DomainDataV1": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "DomainDataV2": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "valid-values": {
                        "type": "string"
                    },
                    "invalid-values": {
                        "type": "string"
                    },
                    "isinteger": {
                        "type": "string"
                    },
                    "nullable": {
                            "type": "string"
                    }
                }
            }            
        }
    }
}

DomainDataV1 and DomainDataV2 are the two kind of validation I regarded. DomainDataV1DomainDataV2是我认为的两种验证。 When additionalProperties is set to false, any additional property is prohibited.additionalProperties设置为 false 时,禁止任何附加属性。 I hope it helps!我希望它有帮助! ;) ;)

for anyone else reading this, and you are using array, try "prefixItems" instead of "items".对于其他阅读本文的人,并且您正在使用数组,请尝试“prefixItems”而不是“items”。 Check out this link to get more of a feel.查看此链接以获得更多感觉。 https://json-schema.org/understanding-json-schema/reference/array.html https://json-schema.org/understanding-json-schema/reference/array.html

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

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