JSON schema deeper object uniqueness

痴心易碎 提交于 2020-03-23 06:18:17

问题


I'm trying to get into JSON schema definitions and wanted to find out, how to achieve a deeper object uniqueness in the schema definition. Please look at the following example definition, in this case a simple IO of a module.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "object",
    "required": ["modulIOs"],
    "properties": {
        "modulIOs": {
            "type": "array",
            "uniqueItems": true,
            "items": {
                "allOf": [
                    {
                    "type": "object",
                    "required": ["ioPosition","ioType","ioFunction"],
                    "additionalProperties": false,
                    "properties": {
                        "ioPosition": {
                            "type": "integer"
                        },
                        "ioType": {
                            "type":"string",
                            "enum": ["in","out"]
                        },   
                        "ioFunction": {
                            "type":"string"
                        }
                    }
                }
            ]
        }
        }
    }
}

When I validate the following with i.E. draft-06 I get a positive validation.

{"modulIOs":
    [
          {
            "ioPosition":1,
            "ioType":"in",
            "ioFunction":"240 V AC in"
        },
        {
            "ioPosition":1,
            "ioType":"in",
            "ioFunction":"24 V DC in"
        }
    ]
} 

I'm aware that the validation is successfull because the validator does what he's intended to - it checks the structure of a JSON-object, but is there a possibility to validate object value data in deeper objects or do i need to perform the check elsewhere?


回答1:


This is not currently possible with JSON Schema (at draft-7).

There is an issue raised on the official spec repo github for this: https://github.com/json-schema-org/json-schema-spec/issues/538

If you (or anyone reading this) really wants this, please thumbsup the first issue comment.

It's currently unlikely to make it into the next draft, and even if it did, time to impleemntations picking it up may be slow.

You'll need to do this validation after your JSON Schema validation process.




回答2:


You can validate data value of your object fields by using JSON schema validation. For example, if you need to check if ioPosition is between 0 and 100 you can use:

"ioPosition": {
            "type": "integer",
            "minimum": 0,
            "maximum": 100
          }

If you need to validate ioFunction field you can use regualr expression such as:

 "ioFunction": {
            "type": "string",
            "pattern": "^[0-9]+ V [A,D]C"
          }

Take a look at json-schema-validation.



来源:https://stackoverflow.com/questions/50640628/json-schema-deeper-object-uniqueness

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!