Make sure object field has value existing in another field in Json Schema

橙三吉。 提交于 2020-01-04 05:35:16

问题


I am trying to express objects and relationships between them. Every object has an ID and every relationship references 2 object ids. I'd like to make sure every relationship references existing object ids. Could you do this with Json Schema?

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "Objects": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer"
                    }
                }
            }
        },
        "Relations": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "objId1": {"type": "integer"}, // I'd like these
                    "objId2": {"type": "integer"}  // Two fields to reference an existing Objects.id
                }

            }
        },
    },
}

回答1:


No you cannot do this with JSON Schema. Sorry.

It sounds like this is a database. You should consider using database level constraints for this sort of validation.




回答2:


Within-document referential integrity constraints of the type required here are supported in JESS (JSON Extended Structural Schemas). See in particular https://bitbucket.org/pkoppstein/jess/wiki/Home#markdown-header-specifying-within-document-referential-integrity-constraints

Incidentally, between-document referential integrity constraints can also be handled, if somewhat clunkily, by combining the separate documents into a composite document.

Disclaimer: I am the author of the JESS specification and validation module.



来源:https://stackoverflow.com/questions/56429622/make-sure-object-field-has-value-existing-in-another-field-in-json-schema

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