validators are now decorators instead
This commit is contained in:
+71
-17
@@ -1,4 +1,6 @@
|
||||
# coding=UTF-8
|
||||
from functools import wraps
|
||||
from flask import request
|
||||
import jsonschema
|
||||
from api.models.market import iter_markets
|
||||
|
||||
|
||||
@@ -9,14 +11,12 @@ class ValidationError(Exception):
|
||||
self.message = message
|
||||
|
||||
|
||||
def validate_keys_exists(obj, *keys):
|
||||
for key in keys:
|
||||
if not key in obj:
|
||||
raise ValidationError('No {} is specified'.format(key))
|
||||
return True
|
||||
def _check_parameter_exists(param):
|
||||
if not param in request.args:
|
||||
raise ValidationError('No {} is specified'.format(param))
|
||||
|
||||
|
||||
def is_number(s):
|
||||
def _is_number(s):
|
||||
try:
|
||||
float(s)
|
||||
return True
|
||||
@@ -24,15 +24,69 @@ def is_number(s):
|
||||
return False
|
||||
|
||||
|
||||
def validate_number(*values):
|
||||
for val in values:
|
||||
if not is_number(val):
|
||||
raise ValidationError('{} is not a number'.format(val))
|
||||
return True
|
||||
def validate_jsonschema(schema):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
if not request.json:
|
||||
raise ValidationError('Payload is not valid json')
|
||||
try:
|
||||
j = request.json
|
||||
jsonschema.validate(j, schema)
|
||||
except jsonschema.ValidationError as e:
|
||||
raise ValidationError(e.message)
|
||||
except jsonschema.SchemaError as e:
|
||||
raise ValidationError(e.message)
|
||||
return f(*args, **kwargs)
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
def validate_territory(value):
|
||||
for market in iter_markets():
|
||||
if market.territory == value:
|
||||
return True
|
||||
raise ValidationError('{} is not a valid territory'.format(value))
|
||||
def validate_exists(*keys):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
for key in keys:
|
||||
_check_parameter_exists(key)
|
||||
if not key in request.args:
|
||||
raise ValidationError('No {} is specified'.format(key))
|
||||
return f(*args, **kwargs)
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
def validate_number(*keys):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
for key in keys:
|
||||
_check_parameter_exists(key)
|
||||
value = request.args.get(key)
|
||||
if not _is_number(value):
|
||||
raise ValidationError('{} is not a number'.format(key))
|
||||
return f(*args, **kwargs)
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
def validate_territory(*keys):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
for key in keys:
|
||||
if not key in request.args:
|
||||
raise ValidationError('No {} is specified'.format(key))
|
||||
value = request.args.get(key)
|
||||
valid = False
|
||||
for market in iter_markets():
|
||||
if market.territory == value:
|
||||
valid = True
|
||||
break
|
||||
|
||||
if not valid:
|
||||
raise ValidationError(
|
||||
'{} is not a valid territory'.format(value))
|
||||
|
||||
return f(*args, **kwargs)
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
Reference in New Issue
Block a user