maximdanilchenko / aiohttp-apispec

Build and document REST APIs with aiohttp and apispec
https://aiohttp-apispec.rtfd.io
MIT License
219 stars 58 forks source link

Schema for single value? #107

Closed proximous closed 3 years ago

proximous commented 3 years ago

If I want to accept or return a single value and have it validated and display in Swagger how would I do that?

For a dict, I create a Schema() class describing the dict. If I simply want to accept or return something like a Float, or an Integer, or a Boolean, is there a way to have the data type validated and documented in Swagger?

for example: curl -H "Content-type: application/json" http://localhost/path/endpoint -X POST -d '{"value": 15.6}'

this can be validated with a schema like:

MyValue(Schema):
    value = fields.Float()

how would I do the same for: curl -H "Content-type: application/json" http://localhost/path/endpoint -X POST -d '15.6'

maximdanilchenko commented 3 years ago

'15.6' is incorrect json body. So you cannot do this with Schema validation.

You can use text/plain content type and just read the request body as string/bytes and convert it to needed value (and them if you want validate it with marshmallow schema), but my recommendation is to use correct json body right away.