在oneOf之前是否可以具有公共属性?

编程入门 行业动态 更新时间:2024-10-21 09:13:18
本文介绍了在oneOf之前是否可以具有公共属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个json模式,该模式具有一个包含多个对象的数组,并且每个对象根据某些模式与其他对象略有不同.

I have this json schema that has an array that contains multiple object, and each object differs slightly from the others based on certain patterns.

示例.

[ { "ID": "pgID", "Name": "John", "Surname": "Doe", "ProjectsInvolved": [ "My Project", "My Project 2" ] }, { "ID": "jtID", "Name": "John", "Surname": "Doe", "WorksOn": [ "Monday", "Thursday" ] } ]

其json模式为:

{ "$schema": "json-schema/draft-04/schema", "type": "array", "items": { "oneOf": [ { "type": "object", "properties": { "ID": { "type": "string", "pattern": "^(pg)\\w*$" }, "Name": { "type": "string" }, "Surname": { "type": "string" }, "ProjectsInvolved": { "type": "array", "items": { "type": "string" } } } }, { "type": "object", "properties": { "ID": { "type": "string", "pattern": "^(jt)\\w*$" }, "Name": { "type": "string" }, "Surname": { "type": "string" }, "WorksOn": { "type": "array", "items": { "type": "string" } } } } ] }, "additionalProperties": false }

我的问题是,尽管真正的json很相似,但它有更多的项,并且随着时间的流逝,它会变得越来越大.因此,我必须问,该模式是否可以将相同的元素Name和Surname分组,并且在oneOf中仅包含ID和数组?

My issue is that although the real json is similar, it has many more items, and it is poised to grow larger as more time passes. Therefore I must ask, is it possible for the schema to group the identical elements Name, and Surname, and only have the ID and the arrays in the oneOf?

建议架构的示例:

{ "$schema": "json-schema/draft-04/schema", "type": "array", "items": { "type": "object", "properties": { "Name": { "type": "string" }, "Surname": { "type": "string" }, "oneOf": [ { "ID": { "type": "string", "pattern": "^(pg)\\w*$" }, "ProjectsInvolved": { "type": "array", "items": { "type": "string" } } }, { "ID": { "type": "string", "pattern": "^(jt)\\w*$" }, "WorksOn": { "type": "array", "items": { "type": "string" } } } ] } }, "additionalProperties": false }

推荐答案

通常,您想在前面定义常见的东西,之后再定义特殊的条件.这使架构更易于阅读,并产生更好的错误消息.

Generally, you want to define the common stuff upfront and the special conditions after. This makes the schema easier to read and results in better error messages.

在此示例中,如果存在"ProjectsInvolved",则"ID"必须以"pg"开头,并且不能存在"WorksOn".并且,如果存在"WorksOn",则"ID"必须以"jt"开头,并且"ProjectsInvolved"不能存在.

In this example, if "ProjectsInvolved" is present, then "ID" must start with "pg" and "WorksOn" can not be present. And, if "WorksOn" is present, then "ID" must start with "jt" and "ProjectsInvolved" can not be present.

使用oneOf或anyOf也可以进行类似的操作,但是通常使用dependencies可以获得更好的错误消息.

It is possible to something like this with oneOf or anyOf as well, but you generally get better error messaging with dependencies.

{ "$schema": "json-schema/draft-04/schema", "type": "array", "items": { "type": "object", "properties": { "ID": { "type": "string" }, "Name": { "type": "string" }, "Surname": { "type": "string" }, "ProjectsInvolved": { "type": "array", "items": { "type": "string" } }, "WorksOn": { "type": "array", "items": { "type": "string" } } }, "dependencies": { "ProjectsInvolved": { "properties": { "ID": { "pattern": "^(pg)\\w*$" } }, "not": { "required": ["WorksOn"] } }, "WorksOn": { "properties": { "ID": { "pattern": "^(jt)\\w*$" } }, "not": { "required": ["ProjectsInvolved"] } } } }, "additionalProperties": false }

更多推荐

在oneOf之前是否可以具有公共属性?

本文发布于:2023-11-14 00:11:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   oneOf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!