[英]get required fields from json schema
I'm trying to test a lot of json documents against a schema, and I use an object with all the required field names to keep how many errors each has. 我正在尝试针对一个架构测试很多json文档,并且我使用一个具有所有必需字段名称的对象来保持每个对象有多少错误。
Is there a function in any python libraries that creates a sample object with boolean values for whether a particular field is required. 是否有任何python库中的函数可创建带有布尔值的示例对象,用于确定是否需要特定字段。 ie From this schema: 即从这个模式:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"type": {
"type": "string"
},
"position": {
"type": "array"
},
"content": {
"type": "object"
}
},
"additionalProperties": false,
"required": [
"type",
"content"
]
}
I need to get something like: 我需要得到类似的东西:
{
"type" : True,
"position" : False,
"content" : True
}
I need it to support references to definitions as well 我也需要它来支持对定义的引用
I don't know of a library that will do this, but this simple function uses a dict comprehension to get the desired result. 我不知道会执行此操作的库,但是这个简单的函数使用dict理解来获得所需的结果。
def required_dict(schema):
return {
key: key in schema['required']
for key in schema['properties']
}
print(required_dict(schema))
Example output from your provided schema 您提供的模式的示例输出
{'content': True, 'position': False, 'type': True}
Edit: link to repl.it example 编辑: 链接到repl.it示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.