def _is_valid_query(params, expected_params):
"""
Function to validate get request params.
"""
# If expected params, check them. If not, pass.
if expected_params:
for param in expected_params:
# If the expected param is in the query.
if param in params:
for check, rule in expected_params[param].__dict__.items():
if rule is not None:
error_message = "QueryParam [%s] failed validation check [%s]:[%s]" % (param, check, rule)
if check == "minLength":
if len(params.get(param)) < rule:
raise ValidationError(error_message)
elif check == "maxLength":
if len(params.get(param)) > rule:
raise ValidationError(error_message)
# Isn't in the query but it is required, throw a validation exception.
elif expected_params[param].required is True:
raise ValidationError("QueryParam [%s] failed validation check [Required]:[True]" % param)
# TODO Add more checks here.
return True
评论列表
文章目录