arrays - JsonSchema 如何对数组使用只读
问题描述
我阅读了规范。但是,我没有找到任何关于如何使用readOnly
Array 的信息。并且单个readonly
属性不足以涵盖不同的场景。
一个示例数组模式,如下所示:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/product.schema.json",
"title": "Products",
"type": "array",
"items": {
"type": "object",
"properties": {
"productId": {
"description": "The unique identifier for a product",
"type": "integer"
},
"productName": {
"description": "Name of the product",
"type": "string"
},
"price": {
"description": "The price of the product",
"type": "number"
}
}
}
}
是否有可能实现以下目标?
- 数组不允许添加/删除数组的任何项。但是,允许更新数组的任何项目。
- 数组不允许更新数组的任何项。但是,允许添加/删除数组的项目。一旦一个项目被添加到数组中,我唯一能做的就是“删除”。
解决方案
readOnly
描述这些限制的工具太生硬了。但是,只需稍作改动,您就可以使用 JSON Hyper-Schema 来表达它。您必须分别对单个产品和产品列表进行建模。不像readOnly
说你不能做的,links
说你能做的。以下是您确定的每个案例的情况。
- 数组不允许添加/删除数组的任何项。但是,允许更新数组的任何项目。
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "http://example.com/product-list.schema.json",
"type": "array",
"items": { "$ref": "product.schema.json" }
}
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "http://example.com/product.schema.json",
"type": "object",
"properties": {
"productId": { "type": "integer" },
"productName": { "type": "string" },
"price": { "type": "number" }
},
"links": [
{ "rel": "edit", "href": "/product/{productId}" }
]
}
- 数组不允许更新数组的任何项。但是,允许添加/删除数组的项目。一旦一个项目被添加到数组中,我唯一能做的就是“删除”。
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "http://example.com/product-list.schema.json",
"type": "array",
"items": { "$ref": "product.schema.json" },
"links": [
{
"rel": "create",
"href": "/products",
"schema": { "$ref": "product-create.schema.json" }
}
]
}
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "http://example.com/product.schema.json",
"type": "object",
"properties": {
"productId": { "type": "integer" },
"productName": { "type": "string" },
"price": { "type": "number" }
},
"links": [
{ "rel": "delete", "href": "/product/{productId}" }
]
}
推荐阅读
- json - Dart/Flutter:build_value vs json_serializable
- javascript - three.js 相机通过触摸事件和设备方向事件旋转
- unix - Sed/Awk:如何在一行中以系列模式插入文本
- java - 开始:eclipse中小程序未初始化错误
- android - Android ndk clang++.exe:错误:生成多个输出文件时无法指定-o
- php - 使用 htaccess 从具有多个参数的 codeigniter 路由重写 URL
- wordpress - 强制插件脚本加载到头部
- rxjs - 如何调用两种方法,第一个返回 void observable,第二个返回 observable?
- reactjs - Jest TypeError: e.preventDefault 不是函数
- assembly - 在 Linux 上使用 mingw 交叉编译 MASM 语法程序集