问题
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"
},
}
},
I tried $ref, but no luck. If "ref" is part of the solution, what should de backend return?
{
"enum": [1, 12, 30 ... , 1000]
}
or
"enum": [1, 12, 30 ... , 1000]
or
[1, 12, 30 ... , 1000]
回答1:
main schema:
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"aproperty": {
"type": "integer",
"$ref": "http://localhost:8080/fixed_set"
},
}
}
subschema:
{
"$id": "http://localhost:8080/fixed_set",
"enum": [1, 12, 30 ... , 1000]
}
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" }
]
},
}
}
来源:https://stackoverflow.com/questions/63252555/reference-remote-enum-values-from-json-schema