Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge 1870f22 into 20cf794
Browse files Browse the repository at this point in the history
  • Loading branch information
rambobinator committed Aug 27, 2018
2 parents 20cf794 + 1870f22 commit 964a098
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions flask_stupe/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@


if marshmallow:
def schema_required(schema):
"""Validate body of the request against the schema.
Abort with a status code 400 if the schema yields errors."""
def __inner(f):
@functools.wraps(f)
def __inner(*args, **kwargs):
json = request.get_json(force=True)
results = schema.load(json)
if results.errors:
abort(400, results.errors)
request.schema = results.data
return f(*args, **kwargs)
if marshmallow.__version__.startswith('3'): # pragma: no cover
def schema_required(schema):
"""Validate body of the request against the schema.
Abort with a status code 400 if the schema yields errors."""
def __inner(f):
@functools.wraps(f)
def __inner(*args, **kwargs):
json = request.get_json(force=True)
try:
request.schema = schema.load(json)
except marshmallow.exceptions.ValidationError as e:
abort(400, e.messages)
return f(*args, **kwargs)
return __inner
return __inner
else:
def schema_required(schema):
"""Validate body of the request against the schema.
Abort with a status code 400 if the schema yields errors."""
def __inner(f):
@functools.wraps(f)
def __inner(*args, **kwargs):
json = request.get_json(force=True)
results = schema.load(json)
if results.errors:
abort(400, results.errors)
request.schema = results.data
return f(*args, **kwargs)
return __inner
return __inner
return __inner

__all__.extend(["schema_required"])

0 comments on commit 964a098

Please sign in to comment.