0
schema = { "type" : "object", 
              "properties" : 
                   { "price" : {"type" : "array", 
                      "items" : { "type" : "string" ,  "pattern": "^[A-Za-z0-9_]*$" }},
                     }, 
             }

from jsonschema import validate
validate(instance={"price" : ["test","34"]}, schema=schema)

上面的代码验证了具有非间隔字符串数组项的属性价格。即使我们不知道传入的属性名称,是否可以验证该属性?

可能像下面这样

schema = { "type" : "object", 
              "properties" : 
                   {  "type" : "array", 
                      "items" : { "type" : "string" ,  "pattern": "^[A-Za-z0-9_]*$" },
                   }, 
             }

from jsonschema import validate
validate(instance={"price" : ["test","34"]}, schema=schema)
validate(instance={"marks" : ["test","34"]}, schema=schema)
4

1 回答 1

0

是的,您可以使用它additionalProperties来提供所有对象都必须通过的通用模式,无论属性名称是什么。

您还可以使用模式限制属性名称 - 请参阅https://json-schema.org/understanding-json-schema/reference/object.html上的“patternProperties” 。

于 2020-10-13T22:38:08.533 回答