python-openapi/openapi-schema-validator

Add support for discriminator

gberaudo opened this issue · 1 comments

It would be nice to have support for the discriminator object so that anyOf or oneOf schemas can return good validation errors.

Exemple schema:

{
    "$ref": "#/$defs/Route",
    "$defs": {
        "MountainHiking": {
            "type": "object",
            "properties": {
                "discipline": {
                    "type": "string",
                    "enum": ["mountain_hiking"]
                },
                "length": {
                    "type": "integer",
                }
            },
            "required": ["discipline", "length"]
        },
        "AlpineClimbing": {
            "type": "object",
            "properties": {
                "discipline": {
                    "type": "string",
                    "enum": ["alpine_climbing"]
                },
                "height": {
                    "type": "integer",
                },
            },
            "required": ["discipline", "height"]
        },
        "Route": {
          "discriminator": {
              "propertyName": "discipline",
              "mapping": {
                  "mountain_hiking": {"$ref": "#/$defs/MountainHiking"},
                  "alpine_climbing": {"$ref": "#/$defs/AlpineClimbing"},
              }
          },
            "oneOf": [
                {"$ref": "#/$defs/MountainHiking"},
                {"$ref": "#/$defs/AlpineClimbing"},
            ]
        }
    }
}

Example instance:

{
    "discipline": "mountain_hiking",
    "length": "bad_string"
}

Current error:

....is not valid under any of the given schemas...

Expected error:

...'bad_string' is not of type integer...

Fixed with #30.