问题描述
在我的模式定义中,我有一个类型,具有一个整数属性,该属性应该是固定"属性中的任何一个.一组数字.问题在于,该固定集"不正确.可能会经常更改.
In my schema definitions, I have a type, with an integer property which should be any of a "fixed" set of numbers. The problem is that this "fixed set" may be changed often.
"person": {
"type": "object",
"properties": {
"aproperty": {
"type": "integer",
"enum": [1, 12, 30 ... , 1000]
},
}
},
是否有任何方法可以从远程服务(该服务具有最新设置)引用此数组?
Is there any way to reference this array from a remote service (which will have the most updated set)?
"person": {
"type": "object",
"properties": {
"aproperty": {
"type": "integer",
"$ref": "http://localhost:8080/fixed_set"
},
}
},
我尝试了$ ref,但是没有运气.如果"ref"是解决方案的一部分,后端后端应该返回什么?
I tried $ref, but no luck.If "ref" is part of the solution, what should de backend return?
{
"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中:
Note that you must be using an evaluator that supports draft2019-09 for the $ref
to be recognized as a sibling keyword. Otherwise, you need to wrap it in an allOf:
{
"type": "object",
"properties": {
"aproperty": {
"type": "integer",
"allOf": [
{ "$ref": "http://localhost:8080/fixed_set" }
]
},
}
}
这篇关于从json-schema引用远程枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!