Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validating an enum array does not seem to work #203

Closed
Tracked by #318
jamesraby opened this issue Apr 20, 2020 · 5 comments
Closed
Tracked by #318

Validating an enum array does not seem to work #203

jamesraby opened this issue Apr 20, 2020 · 5 comments

Comments

@jamesraby
Copy link

jamesraby commented Apr 20, 2020

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:

func (schema *Schema) visitSetOperations(value interface{}, fast bool) (err error) {
if enum := schema.Enum; len(enum) != 0 {
for _, v := range enum {
if value == v {
return
}
}
if fast {
return errSchema
}
return &SchemaError{
Value: value,
Schema: schema,
SchemaField: "enum",
Reason: "JSON value is not one of the allowed values",
}

@fenollp
Copy link
Collaborator

fenollp commented Apr 21, 2020

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 OAI/OpenAPI-Specification#1977

If you want to take this on I'll gladly help!

@jamesraby
Copy link
Author

Would not mind contributing - but this may be too big a fish to swallow! Let me investigate

@iron-s
Copy link

iron-s commented Jul 22, 2020

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" ]

@fenollp
Copy link
Collaborator

fenollp commented Oct 24, 2022

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 #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)

@fenollp
Copy link
Collaborator

fenollp commented Oct 24, 2022

Closing this as this was identified as not being this lib's issue.

@fenollp fenollp closed this as completed Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants