Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference remote enum values from json-schema

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]
like image 292
Victor Marconi Avatar asked Mar 23 '26 12:03

Victor Marconi


1 Answers

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" }
          ]
        },
      }
    }
like image 183
Ether Avatar answered Mar 26 '26 02:03

Ether



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!