Is it possible in json schema to define a constraint between two properties

此生再无相见时 提交于 2019-12-04 11:35:39

You can express one property must be defined when another is present, e.g.:

{
    "type": "object",
    "dependencies": {
        "nameSort": ["name"]
    }
}

However, you cannot specify that two properties must have equal values.

Also, why do you have a separate property at all, if it's always going to be equal? And if it's always equal, could you just have a boolean flag to reduce redundancy?

Old question, but this can now be done with json schema v5/v6 using a combination of the constant and $data (JSON pointer or relative JSON pointer) keywords.

Example:

"properties": {
    "password": { "type": "string" },
    "password_confirmation": { "const": { "$data": "1/password" } }
}

Where "1/password" is a relative JSON pointer saying "go up one level, then look up the key password".

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