是否可以在JSON模式中定义两个属性之间的约束 [英] Is it possible in json schema to define a constraint between two properties

查看:269
本文介绍了是否可以在JSON模式中定义两个属性之间的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的架构中有两个字段-一个是称为"name"的必填属性,另一个是名为"nameSort"的可选属性(用于定义排序属性),我想表示

I have two fields in my schema - one is a required property called "name" and the other is optional (used to define a sorting property) called "nameSort" and I want to express

如果定义了"nameSort"字段,则"name"字段也应定义为相同的值.

If the "nameSort" field is defined, the "name" field should also be defined as the same value.

是否可以用JSON模式表达这样的元素间"约束?我对JSON Schema的粗读在这里 http://json-schema.org/latest/json-schema-validation.html 说不.

Is it possible to express such an "inter-element" constraint with JSON schema? My cursory read of JSON Schema here http://json-schema.org/latest/json-schema-validation.html says no.

推荐答案

当存在另一个属性时,您可以表示一个属性必须是定义的,例如:

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?

这篇关于是否可以在JSON模式中定义两个属性之间的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆