3

I have base json schema base.schema.json

{
  "$id": "base.schema.json",
  "type": "object",
  "properties": {
    "remote_os": {
      "default": "Windows",
      "enum": [
        "Linux",
        "Windows" ]
     }
  },
  "required": ["remote_os"]
}

Now referenced the schema definition in another json

{
  "$id": "update.schema.json",
  "properties": {
    "common_data": {
      "$ref": "base.schema.json"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "common_data": {
            "remote_os": {
              "const": "Windows"
            }
          }
        }
      },
      "then": {
        "properties": {
          "file": {
            "pattern": "^(.*.)(exe)$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "common_data": {
            "remote_os": {
              "const": "Linux",
              "required": [
                "remote_os"
              ]
            }
          }
        }
      },
      "then": {
        "properties": {
          "file": {
            "pattern": "^(.*.)(bin)$"
          }
        }
      }
    }
  ]
}

Basically adding the if-else logic to make sure for remote_os=Linux file should ended up with .bin and remote_os=Windows file should ended up with .exe
Now I am trying to validate against below data

{
  "common_data": {
    "remote_os": "Linux"
  },
  "file": "abc.bin"
}

[<ValidationError: "'abc.bin' does not match '^(.*.)(exe)$'">]. Not sure what's wrong here

curiousguy
  • 3,212
  • 8
  • 39
  • 71
  • What's hard to understand with *"'abc.bin' does not match '^(.\*.)(exe)$'"*? – Tomalak May 06 '21 at 07:42
  • For validation data `remote_os` = `Linux` and as per `if-else` in JSON Schema `update.schema.json` `file` should ended up with `bin` extension. So my validation data was correct only, it shouldn't raise the `ValidationError` – curiousguy May 06 '21 at 08:14
  • Ah, I see. I suggest a better question title: *"How to check nested properties with JSON Schema if else"* instead of "not working". – Tomalak May 06 '21 at 08:49
  • Thanks for the feedback, I have changed the title now. – curiousguy May 06 '21 at 09:26
  • In the example the reference schema definition is part of same JSON, I hope it will also work for local file reference also. Still didn't get where the `if-else` should go.Some example will really help – curiousguy May 06 '21 at 13:13

1 Answers1

0

I think you are over complicating it - surly just if/then/else?

{
  "if": {
    "properties": { "common_data": "properties": { "remote_os": { "const": "Windows" } } }
  },
  "then": {
    "properties": { "file": { "pattern": "^(.*.)(exe)$" } }
  },
  "else": {
    "properties": { "file": { "pattern": "^(.*.)(bin)$" } }
  }
}
Fraser
  • 15,275
  • 8
  • 53
  • 104
  • Tried that actually , but its not working. Its always looking for `"pattern": "^(.*.)(exe)$"` pattern , irrespective of `remote_os` is `Windows or Linux` – curiousguy May 06 '21 at 15:26
  • 2
    You have an error in your schema (both the question and the answer): you are missing an extra `"properties: { ... ` betwheen "common_data" and "remote_os". Schemas that have no recognized keywords are always true, which is why the "then" path is always evaluating. – Ether May 08 '21 at 03:32