Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I must admit I'm a noob in this world so maybe I'm going about it the wrong way, but I used JSON Schema's oneOf[1] along with required[2] for this:

    "properties": {
      "cat": {
        "$ref": "#/$defs/catType"
      }
      "dog": {
        "$ref": "#/$defs/dogType"
      }
    },
    "oneOf": [
      {
        "required": ["cat"]
      },
      {
        "required": ["dog"]
      },
    ]
Usage would then look like:

    {
      "dog": {
        "name": "Charlie"
      }
    }
At least it's explicit and can be easily checked for validity.

[1]: https://json-schema.org/understanding-json-schema/reference/...

[2]: https://json-schema.org/understanding-json-schema/reference/...






Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: