对于 Flask 1.1.2、marshmallow 3.6.1 和 webargs 6.1.0,我所有的论点总是missing
.
架构:
class ExportSearchSchema(Schema):
limit = fields.Integer(required=False, allow_none=False, default=10, missing=10)
offset = fields.Integer(required=False, allow_none=False, default=0, missing=0)
status = fields.Str(required=False)
class Meta:
unknown = RAISE
@validates('status')
def validate_status(self, value):
if value and value not in ['complete', 'pending', 'failed']:
raise ValidationError('Invalid status: {}'.format(value))
@validates('limit')
def validate_limit(self, value):
if value > 100:
raise ValidationError('Max limit is 100')
if value < 1:
raise ValidationError('Limit must be a positive number and less than 100')
@validates('offset')
def validate_offset(self, value):
if value < 0:
raise ValidationError('Offset must be equal to, or greater than 0')
蓝图.py:
from flask import jsonify, Response
from flask import Blueprint
from marshmallow import Schema, fields, validates, ValidationError, RAISE
from webargs.flaskparser import use_args
exports = Blueprint('exports', __name__)
@exports.route('exports/',
methods=['GET'], strict_slashes=False)
@use_args(ExportSearchSchema(unknown=RAISE))
def get_export_list(qparams):
log.info("qparams {}".format(qparams)
response = jsonify({'data': 'export_list'})
response.mimetype = 'application/json'
return response
当我 curl 任何值时,limit
它offset
总是使用该default
值。
curl http://localhost:8000/exports?limit=5930
log: "qparams {'limit': 10, 'offset': 0}"}
我希望ValidationError
提高 a ,因为限制应该 > 100。
当我卷曲一个未知参数时,我希望 aValidationError
被提高,因为它是一个未知参数。这也无法按预期工作。
curl http://localhost:8000/exports?lkfjdskl=fkjdsl
返回 200 并且没有qparams
.
我在结合webargs
,Flask
和时做错了marshmallow
什么?