确保对象字段的值存在于 Json 架构中的另一个字段中
Make sure object field has value existing in another field in Json Schema
我试图表达对象和它们之间的关系。
每个对象都有一个 ID,每个关系都引用 2 个对象 ID。
我想确保每个关系都引用现有的对象 ID。你能用 Json 架构来做到这一点吗?
{
"$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
}
}
},
},
}
不,您不能使用 JSON 模式执行此操作。对不起。
听起来这是一个数据库。
您应该考虑使用数据库级别的约束来进行此类验证。
Within-document JESS(JSON 扩展结构模式)支持此处所需类型的引用完整性约束。
具体参见 https://bitbucket.org/pkoppstein/jess/wiki/Home#markdown-header-specifying-within-document-referential-integrity-constraints
顺便说一下,between-document 引用完整性约束也可以通过将单独的文档组合成一个复合文档来处理,如果有点笨拙的话。
免责声明:我是 JESS 规范和验证模块的作者。
我试图表达对象和它们之间的关系。 每个对象都有一个 ID,每个关系都引用 2 个对象 ID。 我想确保每个关系都引用现有的对象 ID。你能用 Json 架构来做到这一点吗?
{
"$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
}
}
},
},
}
不,您不能使用 JSON 模式执行此操作。对不起。
听起来这是一个数据库。 您应该考虑使用数据库级别的约束来进行此类验证。
Within-document JESS(JSON 扩展结构模式)支持此处所需类型的引用完整性约束。 具体参见 https://bitbucket.org/pkoppstein/jess/wiki/Home#markdown-header-specifying-within-document-referential-integrity-constraints
顺便说一下,between-document 引用完整性约束也可以通过将单独的文档组合成一个复合文档来处理,如果有点笨拙的话。
免责声明:我是 JESS 规范和验证模块的作者。