Giter VIP home page Giter VIP logo

Comments (5)

dod38fr avatar dod38fr commented on May 17, 2024 1

Yes ! this resolves my problem.

On the other hand, you might want to check why specifying enum with properties leads to weird results. I would have expected a consistent behavior or an error.

Feel free to leave this bug open to track this issue or to close it.

Thanks for the help ! :-)

All the best

from express-openapi-validator.

cdimascio avatar cdimascio commented on May 17, 2024

i'm a bit confused by what you are trying to express.

It seems you have an enum of type object with values of a different type (string). im not sure think it’s correct . Also it has a property foo where that key is the enum value. Also a bit puzzling.

    EnumBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              enum:   # <-- hmm
                - foo
              required: 
                - foo
              properties:
                foo:
                  type: string

Perhaps you want something like the following:
an object with property foo which is an enum of type string with acceptable values bar and baz

    EnumBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - foo
              properties:
                foo:
                  type: string      
                  enum:
                    - bar
                    - baz

Can you describe what you are trying to express, perhaps I can help

from express-openapi-validator.

dod38fr avatar dod38fr commented on May 17, 2024

@cdimascio We want to validate that the body contains an object with only allowed keys. In this case, I've boiled down the issue to only one property.

In other word, I want the validation to allow a body containing { foo: "bar" } and fail a body containing {foo: "baz", bogus: "baz"}.

The second example should fail because bogus is not in the list of allowed keys (as specified in the enum).

Sorry for not being clear in my original request.

from express-openapi-validator.

cdimascio avatar cdimascio commented on May 17, 2024

No problem. Have you tried additionalProperties: false.

See example here:
The spec:
https://github.com/cdimascio/express-openapi-validator/blob/master/test/resources/additional.properties.yaml#L52

The test:
https://github.com/cdimascio/express-openapi-validator/blob/master/test/additional.props.spec.ts#L37

This test ensures that only explicit props are allowed. In this case, name (required) and tags (optional). Any other property will result in a validation error.

from express-openapi-validator.

cdimascio avatar cdimascio commented on May 17, 2024

Woop! Excellent news! I'm glad it worked out.

The validator is properly handling the enum cases. There are examples of them working in the tests. I'm fairly certain that the specification you provided above is not behaviorally valid, despite it passing validation. Enums describe values and not keys. The OpenAPI spec allows one to define an enum property as a type object, however, the value should also be an object.

I'll keep this open while I validate this is indeed the case.

from express-openapi-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.