Python - Validating Schemas, trying to require a field

everyone! I appreciate your help. This is my first time creating a schema and it involves flight data, but I'm getting multiple errors because in some of the records, it doesn't involve a specific field (in this case, the dst_airport).

Here's what I included:

        "dst_airport": {
            "type": "object",
            "properties": {
                 "airport_id": {
                     "type": "number"
                 },
                 "name": {
                     "type": "string"
                 },
                 "city": {
                     "type": "string"
                 },
                 "country": {
                     "type": "string"
                 },
                 "iata": {
                     "type": "string"
                 },
                 "iaco": {
                     "type": "string"
                 },
                 "latitude": {
                     "type": "number"
                 },
                  "longitude": {
                     "type": "number"
                 },
                  "altitude": {
                     "type": "number"
                  },
                  "timezone": {
                     "type": "number"
                  },
                  "dst": {
                     "type": "string"
                  },
                  "tz_id": {
                     "type": "string"
                  },
                  "type": {
                     "type": "string"
                  },
                  "source": {
                     "type": "string"
                  },
                "anyOf": [
                    {
                        "required": [
                            "dst_airport"
                            ]
                    }
                    ]
                }
        }

I added the anyof part that I found online trying to fix it, but I don't think it actually did anything.

Is anyone able to lend a hand? Thanks!


The required should be at the same level than the properties where they are defined. Example :

import jsonschema

schema = {
    "type": "object",
    "properties": {
        "src_airport": {
            "type": "object",
            "properties": {
                "airport_id": {"type": "number"}
            }
        },
        "dst_airport": {
            "type": "object",
            "properties": {
                "airport_id": {"type": "number"}
            }
        },
    },
    "required": [
        "src_airport",
        # do not put `dst_airport` here, meaning it is optional
    ]
}

example1 = {
    "src_airport": {"airport_id": 1},
    "dst_airport": {"airport_id": 2}
}
example2 = {
    "src_airport": {"airport_id": 1},
    # "dst_airport": {"airport_id": 2}
}
example3 = {
    # "src_airport": {"airport_id": 1},
    "dst_airport": {"airport_id": 2}
}

try:
    jsonschema.validate(example1, schema=schema)
except jsonschema.exceptions.ValidationError:
    print("example1: fail")
else:
    print("example1: pass")

try:
    jsonschema.validate(example2, schema=schema)
except jsonschema.exceptions.ValidationError:
    print("example2: fail")
else:
    print("example2: pass")

try:
    jsonschema.validate(example3, schema=schema)
except jsonschema.exceptions.ValidationError:
    print("example3: fail")
else:
    print("example3: pass")

I too get confused by the jsonschema syntax.