hug icon indicating copy to clipboard operation
hug copied to clipboard

body validation can not take effect when body is None

Open guozhihua12 opened this issue 5 years ago • 1 comments

from marshmallow import Schema, fields

class TestingSchema(Schema): a = fields.Integer(required=True) b = fields.Integer(required=True) c = fields.Integer()

@api.route.post('/test') def test_validation(body: TestingSchema()): return True, '', body

when body is None image

guozhihua12 avatar Nov 15 '19 09:11 guozhihua12

@guozhihua12 I'm having a little trouble reproducing this. Would you mind updating with the full code you're using for a repo? For instance, I'm not exactly sure how you are defining api and it doesn't seem to be something with direct documentation.

To try to reproduce, I used a file like this

from marshmallow import Schema, fields
import hug

class TestingSchema(Schema):
    a = fields.Integer(required=True)
    b = fields.Integer(required=True)
    c = fields.Integer()

@hug.route.post('/test')
def test_validation(body: TestingSchema()):
    return True, '', body

and then used POST requests like this

➜  hug-examples curl -X POST localhost:8000/test -d "a=1&b=2&c=3"
[true, "", {"b": 2, "a": 1, "c": 3}]
➜  hug-examples curl -X POST localhost:8000/test
{"errors": {"body": {"_schema": ["Invalid input type."]}}}

jay-tyler avatar Feb 09 '20 22:02 jay-tyler