将必填字段应用于引用的JSON数据模式 [英] Apply required field to referenced JSON data schema
问题描述
我有以下使用JSON模式解决的用例.
我有一个通用的JSON数据架构,例如,一个用户.这是user.schema.json文件的示例.
{
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"email": {
"type": "string",
"minLength": 1
},
"locale": {
"type": "string",
"minLength": 1
},
"active": {
"type": "boolean",
"default": true
},
"password": {
"type": "string",
"minLength": 8
},
"roles": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
}
}
}
现在我有2种不同的请求: -POST:添加用户 -补丁:更新用户数据.
在1种情况下,我可以发送此数据结构,其中包含3个必填字段,而在打补丁的情况下,每个字段都是可选的. 所以我得到了发布请求文件:post-user.schema.json:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "user.schema.json",
"required": [
"name",
"password",
"email"
]
}
对于我的补丁(path-user.schema.json:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "user.schema.json"
}
现在我遇到的问题是我的POST模式也将用户标记为:
{
"name": "NoPassword",
"email": "nopassword@moba.nl",
"roles": []
}
缺少一个有效的JSON模式的必填密码字段.
显然,这不是将必填字段分配给引用的数据结构的方法.我尝试使用google来搜索有关此主题的内容,例如: [如何将必填字段分配给引用的架构] 我试图从文档中获取此信息.
我没有运气.
我现在的问题是: 答:是否可以将必需的字段分配给$ referenced json模式数据对象. B.如果可能的话,该怎么办 C.如果这不可能,那么什么是解决这个问题的好方法.
非常感谢您的帮助.
使用$ref
会导致对象中的所有其他属性被忽略,因此您需要包装对$ref
的使用.
让我们看一下规格:
具有"$ ref"属性的对象模式必须解释为
"$ ref"参考. "$ ref"属性的值必须是URI
参考.根据当前URI基础进行解析,它标识了
要使用的架构的URI. "$ ref"对象中的所有其他属性必须 被忽略.
https://tools.ietf.org /html/draft-handrews-json-schema-01#section-8.3
然后考虑您包含在问题中的架构:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "user.schema.json",
"required": [
"name",
"password",
"email"
]
}
通过阅读规范,您可以了解为什么required
将被忽略.
最初,$ref
仅用于替换整个对象,而不能添加该对象的条件.
您想要的是将多个架构应用于实例.为此,请使用allOf
.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"allOf": [
{
"$ref": "user.schema.json"
},
{
"required": [
"name",
"password",
"email"
]
}
]
}
我在 https://jsonschema.dev -尽管它尚不支持引用,所以我将其排除在外,但验证将起作用.
从草案8开始,$ref
会像您期望的那样运行,因为它成为应用程序关键字,而不是具有特殊行为的关键字,这意味着同一对象中的其他关键字将不需要忽略.
I have the following use-case I try to solve with JSON schemas.
I have a generic JSON data schema for, for example, a user. Here is an example of the user.schema.json file.
{
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"email": {
"type": "string",
"minLength": 1
},
"locale": {
"type": "string",
"minLength": 1
},
"active": {
"type": "boolean",
"default": true
},
"password": {
"type": "string",
"minLength": 8
},
"roles": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
}
}
}
Now I have 2 different kinds of requests: - POST: Add a user - PATCH: Update user data.
In 1 case, I can send this data structure, with 3 required fields, while in case of a patch each field is optional. So I get the post request file: post-user.schema.json:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "user.schema.json",
"required": [
"name",
"password",
"email"
]
}
And for my patch (path-user.schema.json:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "user.schema.json"
}
Now the issue that I am having is that my POST schema also marks a user like:
{
"name": "NoPassword",
"email": "nopassword@moba.nl",
"roles": []
}
Which is missing the required password field, as a valid JSON schema.
Apparently, this is not the way to assign required fields to a referenced data structure. I have tried to use google to see what I can find on the subject regarding this using searches like: [ how to assign required field to referenced schema's ] and I tried to obtain this info from the documentation.
I have no luck.
My questions now are: A. Is it possible to assign required fields to a $referenced json schema data object. B. If this is possible how to do it C. If this is not possible, what would be a good way to approach this.
Any help is much appreciated.
Using $ref
results in all other properties in the object being ignored, so you need to wrap your use of $ref
.
Let's take a look at the spec:
An object schema with a "$ref" property MUST be interpreted as a
"$ref" reference. The value of the "$ref" property MUST be a URI
Reference. Resolved against the current URI base, it identifies the
URI of a schema to use. All other properties in a "$ref" object MUST be ignored.
https://tools.ietf.org/html/draft-handrews-json-schema-01#section-8.3
Then consider the schema you included in your question:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "user.schema.json",
"required": [
"name",
"password",
"email"
]
}
Reading the spec, you can see why required
will be ignored.
Originally $ref
was only designed to replace a WHOLE object, not ADD to the conditions for the object.
What you want is for multiple schemas to be applied to the instance. To do this, you use allOf
.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"allOf": [
{
"$ref": "user.schema.json"
},
{
"required": [
"name",
"password",
"email"
]
}
]
}
I loaded this schema into a demo for you to test at https://jsonschema.dev - although it doesn't support references yet, so I transcluded the reference, but the validation will work the same.
From draft-8 onwards, $ref
will behave as you expect, as it becomes an applicator keyword rather than a keyword with special behaviours, meaning other keywords in the same object will not need to be ignored.
这篇关于将必填字段应用于引用的JSON数据模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!