csk*_*k87 4 json jsonschema openapi
我将我的 JSON 模式分成两个文件。
person-input.json (将由输入设置的所有属性。)
person.json (持有对 person-input.json 的引用,但也有 dateUpdate、dateCreated 和 DateDeleted)。
这将输入与自动生成的日期属性分开。
我不希望任何帖子能够向我的数据添加不需要的属性,所以我想我会使用"additionalProperties": false的问题是,如果我在person-input.json文件中使用它,它将不接受文件中的“日期”属性person.json。如果我把它放在 person.json 文件中,它不会阻止添加随机属性。这个问题有方法解决吗?
所以下面这个不起作用,我放错地方了"additionalProperties": false吗?
person.json
{
"allOf": [
{
"$ref": "./person-input.json"
},
{
"type": "object",
"properties": {
"dateCreated": {
"name": "dateCreated",
"type": "string",
"description": "date created",
"example": "2019-09-02T11:17:41.783Z"
},
"dateUpdated": {
"type": "string",
"nullable": true,
"description": "date updated",
"example": "2019-09-02T11:17:41.783Z"
},
"dateDeleted": {
"type": "string",
"nullable": true,
"description": "date deleted",
"example": "2019-09-02T11:17:41.783Z"
}
},
"additionalProperties": false
}
]
}
Run Code Online (Sandbox Code Playgroud)
additionalProperties不能“透视”涂抹器allOf,也不能“透视”使用$ref.
为了解决这个问题,您必须在最外层/最顶层架构中对架构进行一些复制,并additionalProperties: false从任何子架构中删除该要求。
additionalProperties: false通过应用false(这是一个有效的架构,返回验证失败)到基于properties或patternProperties在 SAME 架构对象内与键不匹配的值。
使用“additionalProperties”的验证仅适用于
不匹配“properties”中的任何名称且不匹配“patternProperties”中的任何正则表达式的实例名称的子值。
https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5.6 (draft-7)
因此,如果您需要将所需的所有属性复制到顶级架构。是的,这不好!
您可以通过观察properties对象的值是模式这一事实来使它更好一点,因此可能只是true允许子模式稍后实际进行验证。
这是我将在即将进行的演讲中使用的示例:
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "MatchMakerExchange format for queries",
"definitions": {
"phenotypicFeatures": {
"type": [
"array"
]
},
"genomicFeatures": {
"type": [
"array"
]
},
"geneticsPatient": {
"properties": {
"phenotypicFeatures": {
"$ref": "#/definitions/phenotypicFeatures"
},
"genomicFeatures": {
"$ref": "#/definitions/genomicFeatures"
}
},
"anyOf": [
{
"required": [
"phenotypicFeatures"
]
},
{
"required": [
"genomicFeatures"
]
}
]
},
"regularPatient": {
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": [
"string"
]
}
}
}
},
"properties": {
"patient": {
"additionalProperties": false,
"properties": {
"name": true,
"phenotypicFeatures": true,
"genomicFeatures": true
},
"allOf": [
{
"$ref": "#/definitions/regularPatient"
},
{
"$ref": "#/definitions/geneticsPatient"
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
你可能会问......“好吧,这太疯狂了。你能解决这个问题吗?” - 我们做到了。它叫做 Draft 2019-09,它最近才发布,所以你必须等待实现的赶上。
新关键字unevaluatedProperties取决于注释结果,但您仍需additionalProperties: false要从子模式中删除。
| 归档时间: |
|
| 查看次数: |
609 次 |
| 最近记录: |