Closed superstas closed 2 months ago
@superstas
Do you have any objection against adding this logic to Huma? ( I can send a patch )
This seems like it would be great to add to support better validation error messages when possible. Hopefully this doesn't complicate the code too much :smile:
Maybe there is a way to get what I want without changing the source code?
No, the support for oneOf
is fairly basic at this point. I would love some help making the error messages it generates better. This seems like kind of a complex problem unfortunately. At least the discriminator mapping helps simplify it.
This seems like kind of a complex problem unfortunately. At least the discriminator mapping helps simplify it.
Agree.
Overall, sounds great! Thank you.
I'll send a patch when it's ready.
Hi there,
I have a question about validating a request by a spec with
discriminator and mapping
.In this example I follow to Mapping Type Names from here: https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
This is my code:
This is the spec I get:
spec
```yaml components: schemas: EqualName: additionalProperties: false description: An EQUAL comparison with main.Name. properties: operator: description: The operator to use for the comparison. enum: - EQUAL type: string value: description: The value to compare against. maxLength: 100 minLength: 1 type: string required: - operator - value type: object ErrorDetail: additionalProperties: false properties: location: description: Where the error occurred, e.g. 'body.items[3].tags' or 'path.thing-id' type: string message: description: Error message text type: string value: description: The value at the given location type: object ErrorModel: additionalProperties: false properties: $schema: description: A URL to the JSON Schema for this object. examples: - https://example.com/schemas/ErrorModel.json format: uri readOnly: true type: string detail: description: A human-readable explanation specific to this occurrence of the problem. examples: - Property foo is required but is missing. type: string errors: description: Optional list of individual error details items: $ref: "#/components/schemas/ErrorDetail" type: - array - "null" instance: description: A URI reference that identifies the specific occurrence of the problem. examples: - https://example.com/error-log/abc123 format: uri type: string status: description: HTTP status code examples: - 400 format: int64 type: integer title: description: A short, human-readable summary of the problem type. This value should not change between occurrences of the error. examples: - Bad Request type: string type: default: about:blank description: A URI reference to human-readable documentation for the error. examples: - https://example.com/errors/example format: uri type: string type: object InName: additionalProperties: false description: An IN comparison with []main.Name. properties: operator: description: The operator to use for the comparison. enum: - IN type: string values: description: The values to compare against. items: description: A name with value . maxLength: 100 minLength: 1 type: string minItems: 1 type: - array - "null" required: - operator - values type: object InputBody: additionalProperties: false properties: $schema: description: A URL to the JSON Schema for this object. examples: - https://example.com/schemas/InputBody.json format: uri readOnly: true type: string name: description: The name to compare. discriminator: mapping: EQUAL: "#/components/schemas/EqualName" IN: "#/components/schemas/InName" propertyName: operator oneOf: - $ref: "#/components/schemas/EqualName" - $ref: "#/components/schemas/InName" type: object required: - name type: object OutputBody: additionalProperties: false properties: $schema: description: A URL to the JSON Schema for this object. examples: - https://example.com/schemas/OutputBody.json format: uri readOnly: true type: string message: type: string required: - message type: object info: title: My API version: 1.0.0 openapi: 3.1.0 paths: /input: post: operationId: post-input requestBody: content: application/json: schema: $ref: "#/components/schemas/InputBody" required: true responses: "200": content: application/json: schema: $ref: "#/components/schemas/OutputBody" description: OK default: content: application/problem+json: schema: $ref: "#/components/schemas/ErrorModel" description: Error summary: Post input ```All looks good.
The problem
When I send an invalid request, for example
I get a pretty common error message:
From the source code, this is how Huma currently validates with
oneOf
.But in this scenario, we've got a mapping to target schemas by
propertyName
.So, it's possible to validate the input against only the target schema if a
discriminator.mapping
is provided.Ideal state Ideally, in the above scenario, I'd want to get smth like
instead of
This would help the end user understand why the request is invalid faster.
Questions
Thank you in advance, and have a great weekend.