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

Disallow Unknown Body Parameters? #146

Closed
rowanc1 opened this issue Nov 26, 2019 · 2 comments
Closed

Disallow Unknown Body Parameters? #146

rowanc1 opened this issue Nov 26, 2019 · 2 comments

Comments

@rowanc1
Copy link
Contributor

rowanc1 commented Nov 26, 2019

Awesome library, thanks for putting it together!

Working off of the example provided, I was expecting the validation to cover requestBody in the OpenAPI 3.0 schema. As a user of APIs I often make typos or misunderstand the documentation, being able to be alerted by the API would be nice.

A similar interface to the allowUnknownQueryParameters (see #133) would be ideal:

new OpenApiValidator({
    apiSpec: "./src/services/openapi.yaml",
    validateRequests: {
        allowUnknownQueryParameters: false,
        allowUnknownBodyParameters: false,
    }
}).install(app);

Right now adding a value to the JSON body of a request of the POST /v1/pets example results in:

curl -s --request POST \
  --url http://localhost:3000/v1/pets \
  --header 'content-type: application/json' \
  --header 'x-api-key: XXXX' \
  --data '{"name":"sparky", "random": "scaryValue"}'


>> {"id":3,"name":"sparky","random": "scaryValue"}

I would expect this to error using the same validations as if it were in the query parameters. I would be curious about your thoughts on this.

@rowanc1
Copy link
Contributor Author

rowanc1 commented Nov 26, 2019

I just found out how to do this by adding additionalProperties: false to the OpenAPI schema.

Perhaps it could be worth putting this in the example documentation?

@cdimascio
Copy link
Owner

Thanks, @rowanc1. You're correct, this can be accomplished with additionalProperties: false.
I also agree that we should get this into the README. Would you be willing to add an example to the FAQ section of the README?

rowanc1 added a commit to rowanc1/express-openapi-validator that referenced this issue Nov 27, 2019
Documentation to point users towards the Open API 3.0 spec to set
`additionalProperties` to false for validation of the body component
of the request.

See cdimascio#146
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants