How do I do a nested list (array) of schema references in json schema

xamox

I am trying to build a schema which has a list of items that I want to have an enforced schema.

Basically here is the data I want to validate against the schema:

data = {
    "VIN" : "123",
    "timestamp" : "xxxx",
    "model" : "jeep",
    "inspections": [
        { "door_badge" : 
             {
                "expected": "yes",
                "image": "/image/path/here",
                "state": 1
            },
        },
        {
            "rear_badge" :
            {
                "expected" : "yes",
                "image" : "/image/path/here",
                "state": 1
            }


        }
    ],
}

And my schema mapping as is such but seem to be getting errors when trying to validate:

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "inspection": {
            "type": "object",
            "properties": {
                "expected" : { "type": "string" },
                "found": { "type" : "string"},
                "state" : { "enum" : [0,1] },
                "image" : { "type" : "string"}
            },
            "required": ["state", "image"]
        },
        "inspections" : {
            "type" : "array",
            "items" : {
                "$ref" : "#/definitions/inspection"
            },
            "required" : ["items"]
        },

    },
    "type" : "object",
    "properties" : {
        "VIN" : { "type" : "string" },
        "timestamp" : { "type" : "string"},
        "model" : { "type" : "string"},
        "inspections" : { "$ref" : "#/definitions/inspections"}
    },
    "required": ["VIN", "timestamp", "model"]
}

I basically want sub list within the inspection list but also to validate based on that type of item.


SOLUTION: Thanks to jruizaranguren's help the solution was:

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "inspection": {
            "type": "object",
            "properties": {
                "expected" : { "type": "string" },
                "found": { "type" : "string"},
                "state" : { "enum" : [0,1] },
                "image" : { "type" : "string"}
            },
            "required": ["state", "image", "expected"]
        },
    },
    "type" : "object",
    "properties" : {
        "VIN" : { "type" : "string" },
        "timestamp" : { "type" : "string"},
        "model" : { "type" : "string"},
        "inspections" : { 
            "type" : "array",
            "items" : {
                "type" : "object",
                "maxProperties": 1,
                "minProperties": 1,
                "additionalProperties" : {
                    "$ref" : "#/definitions/inspection"
                }
            }
        }
    },
    "required": ["VIN", "timestamp", "model", "inspections"]
}
jruizaranguren

The problem you have is that you are constraining each item in the array to be of the form:

{
    "expected": "yes",
    "image": "/image/path/here",
    "state": 1
}

But your objects are of the form:

{ "door_badge" : 
    {
        "expected": "yes",
        "image": "/image/path/here",
         "state": 1
    },
}

One way to achieve this would be to use additionalProperties in the items clause:

"items" : 
    { 
            "type" : "object",
            "maxProperties": 1,
            "minProperties": 1,
            "additionalProperties" : {
                "$ref" : "#/definitions/inspection"
            }
    }

If you can enforce some rules on these properties keys (for instance, all must end with badge) then you may use patternProperties clause with a regular expression.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

how do I convert a double array in C to python list?

来自分类Dev

从POJO生成Json Schema

来自分类Dev

JSon schema and Inheritance

来自分类Dev

MongoDB schema for reservations, nested or referenced?

来自分类Dev

How to match a Unicode letter with a JSON Schema pattern (regular expression)

来自分类Dev

How do I parse JSON in Racket?

来自分类Dev

How do I get my nested if statement to work in jQuery

来自分类Dev

Failed validating 'type' json schema

来自分类Dev

oneOf对象的Json Schema示例

来自分类Dev

数字的MaxLength的JSON Schema属性

来自分类Dev

How can I make a field in a meteor Simple Schema equal to a js variable and still let the user fill out the rest of the simple schema?

来自分类Dev

how do you output the results of a collections.defaultdict(list) from a nested list into jinja?

来自分类Dev

从Json Schema生成样本Json输出

来自分类Dev

How do I give a list dynamically without hardcoding to MongoDB's $in?

来自分类Dev

How do I build a list with a dependently-typed length?

来自分类Dev

How do I effectively access list information in C++?

来自分类Dev

How do I deserialize an array with indexes using jackson

来自分类Dev

How do I convert a numpy array into a pandas dataframe?

来自分类Dev

how do I find the closest value to a given number in an array?

来自分类Dev

How do I get a value from an associative array using Swift

来自分类Dev

How do I convert a Zip into an array in rust 0.8?

来自分类Dev

Adding an array inside my Mongoose schema

来自分类Dev

使用 Mongoose $push into Array Schema 类型

来自分类Dev

基于属性值的条件Json Schema验证

来自分类Dev

测试schema.org ld + json内容

来自分类Dev

Json Schema-使用引用使用枚举

来自分类Dev

Json Schema,请考虑未指定的属性

来自分类Dev

用于验证测试的Json Schema数据集

来自分类Dev

验证Json Schema Draft V4

Related 相关文章

  1. 1

    how do I convert a double array in C to python list?

  2. 2

    从POJO生成Json Schema

  3. 3

    JSon schema and Inheritance

  4. 4

    MongoDB schema for reservations, nested or referenced?

  5. 5

    How to match a Unicode letter with a JSON Schema pattern (regular expression)

  6. 6

    How do I parse JSON in Racket?

  7. 7

    How do I get my nested if statement to work in jQuery

  8. 8

    Failed validating 'type' json schema

  9. 9

    oneOf对象的Json Schema示例

  10. 10

    数字的MaxLength的JSON Schema属性

  11. 11

    How can I make a field in a meteor Simple Schema equal to a js variable and still let the user fill out the rest of the simple schema?

  12. 12

    how do you output the results of a collections.defaultdict(list) from a nested list into jinja?

  13. 13

    从Json Schema生成样本Json输出

  14. 14

    How do I give a list dynamically without hardcoding to MongoDB's $in?

  15. 15

    How do I build a list with a dependently-typed length?

  16. 16

    How do I effectively access list information in C++?

  17. 17

    How do I deserialize an array with indexes using jackson

  18. 18

    How do I convert a numpy array into a pandas dataframe?

  19. 19

    how do I find the closest value to a given number in an array?

  20. 20

    How do I get a value from an associative array using Swift

  21. 21

    How do I convert a Zip into an array in rust 0.8?

  22. 22

    Adding an array inside my Mongoose schema

  23. 23

    使用 Mongoose $push into Array Schema 类型

  24. 24

    基于属性值的条件Json Schema验证

  25. 25

    测试schema.org ld + json内容

  26. 26

    Json Schema-使用引用使用枚举

  27. 27

    Json Schema,请考虑未指定的属性

  28. 28

    用于验证测试的Json Schema数据集

  29. 29

    验证Json Schema Draft V4

热门标签

归档