kin-openapi
kin-openapi copied to clipboard
Validating an enum array does not seem to work
I have the following schema:
items:
type: string
enum:
- Admin
- Prescriber
- Clinician
When I try to validate the array field with input ["Admin"] - I get the following error:
code=400, message=Request body has an error: doesn't match the schema: Error at \"/permissions\":JSON value is not one of the allowed values, internal=Request body has an error: doesn't match the schema: Error at \"/permissions\":JSON value is not one of the allowed values\nSchema:\n {\n \"enum\": [\n \"Prescriber\",\n \"Admin\",\n \"Clinician\"\n ],\n \"items\": {\n \"type\": \"string\"\n },\n \"type\": \"array\"\n }\n\nValue:\n [\n \"Admin\"\n ]\n"
It seems like it is comparing the array ["Admin"] vs iterating through the array on line:
https://github.com/getkin/kin-openapi/blob/989d00f2a3e831bd8fa14a4c2abd17a37d684f50/openapi3/schema.go#L641-L656
Indeed this VisitJSON implementation seems wrong. It's using value to check the schema instead of the other way around.
It's probably time to replace it with https://github.com/xeipuuv/gojsonschema It would have to transform the OpenAPI schema to JSON Schema first (see: https://github.com/mikunn/openapi-schema-to-json-schema/blob/master/lib/converters/schema.js ) This transformation would not be needed for specs >= 3.1 though as per https://github.com/OAI/OpenAPI-Specification/pull/1977
If you want to take this on I'll gladly help!
Would not mind contributing - but this may be too big a fish to swallow! Let me investigate
I've cleaned up error message, and it seems like you have enum on the array level, not item.
Schema: {"enum": ["Prescriber", "Admin", "Clinician" ], "items": { "type": "string" }, "type": "array" }
Value: [ "Admin" ]
Indeed the cited YAML and the error message don't match. Thanks @iron-s !
Looks like schema should instead be either of:
type: array
items:
type: string
enum:
- Admin
- Prescriber
- Clinician
or
type: array
items:
type: string
enum:
- [Admin]
- [Prescriber]
- [Clinician]
Note, that last schema will cause a panic due to https://github.com/getkin/kin-openapi/issues/646
The issue you're having is because you schema is
type: array
items:
type: string
enum:
- Admin
- Prescriber
- Clinician
(note how the enum values do not match the surrounding schema)
Closing this as this was identified as not being this lib's issue.