Description
While testing a schema with oneOf
that has 2 schemas, the validation library returns all of the errors across the two schemas if none of them match; which is the expected behavior. However, if I add the discriminator field (Redocly docs) the validator still fails for the same issues across both schemas, but I would expect the validator to only fail against the schema it was mapped to. After looking more into this, I thought this was an issue with the JSONSchema validator library you're using under the hood, but it turns out the discriminator
field isn't JSONSchema, it's a feature of OpenAPI.
Point being that I'd like to open a feature request to support discriminators as a field. Pulling from the OpenAPI documentation, part of their description is:
Here the discriminator value of dog will map to the schema #/components/schemas/Dog, rather than the default (implicit) value of Dog. If the discriminator value does not match an implicit or explicit mapping, no schema can be determined and validation SHOULD fail. Mapping keys MUST be string values, but tooling MAY convert response values to strings for comparison.
When used in conjunction with the anyOf construct, the use of the discriminator can avoid ambiguity where multiple schemas may satisfy a single payload.
In both the oneOf and anyOf use cases, all possible schemas MUST be listed explicitly. To avoid redundancy, the discriminator MAY be added to a parent schema definition, and all schemas comprising the parent schema in an allOf construct may be used as an alternate schema.
Looking at the responses/validate_response code, I imagine you could add an additional "discriminator resolver" method here right before passing it to the JSONSchema library? However, I'm not familiar enough with this code base to know whether that will break the "stack trace" of where the invalid schema was raised from (e.g. keeping the oneOf
in the location object). Alternatively, you could run the validation as-is then filter out the "invalid errors" before returning it to the user. For example, if the mapping resulted in a match with oneOf[0]
then ignore anything in oneOf[1+]
.
Anyways, you may have better ideas than this, just asking if we can get support for discriminator