在我的架构定义中,我有一个带有整数属性的类型,该属性应该是任何“固定”数字集。问题是这个“固定集”可能会经常改变。
"person": {
"type": "object",
"properties": {
"aproperty": {
"type": "integer",
"enum": [1, 12, 30 ... , 1000]
},
}
},
是否有任何方法可以从远程服务引用此数组(该服务将具有最新的集合)?
"person": {
"type": "object",
"properties": {
"aproperty": {
"type": "integer",
"$ref": "http://localhost:8080/fixed_set"
},
}
},
我尝试了 $ref,但没有成功。 如果“ref”是解决方案的一部分,那么后端应该返回什么?
{
"enum": [1, 12, 30 ... , 1000]
}
或
"enum": [1, 12, 30 ... , 1000]
或
[1, 12, 30 ... , 1000]
最佳答案
主要架构:
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"aproperty": {
"type": "integer",
"$ref": "http://localhost:8080/fixed_set"
},
}
}
子架构:
{
"$id": "http://localhost:8080/fixed_set",
"enum": [1, 12, 30 ... , 1000]
}
请注意,您必须使用支持 Draft2019-09 的评估器,才能将 $ref
识别为同级关键字。否则,您需要将其包装在 allOf 中:
{
"type": "object",
"properties": {
"aproperty": {
"type": "integer",
"allOf": [
{ "$ref": "http://localhost:8080/fixed_set" }
]
},
}
}
关于json - 从 json-schema 引用远程枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63252555/