Skip to content

Optional value with pattern #812

Answered by jviotti
careri asked this question in Q&A
Oct 2, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Hi @careri , I think you can simplify your schema a lot by making use of anyOf instead of if/then/else. For example:

{
  "$id": "https://test.com/optional_value.json",
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "description": "Test",
  "properties": {
    "resource_url": {
      "description": "An optional string, should match the uri format if supplied",
      "anyOf": [
        { "type": "null" },
        { "const": "" },
        { "type": "string", "minLength": 1, "format": "uri" },
      ]
    }
  }
}

Though keep in mind that not all JSON Schema implementations validate format by default (some only report it as metadata, which we call "annotations" in JSON Schema),…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@careri
Comment options

Answer selected by careri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants