-
-
Notifications
You must be signed in to change notification settings - Fork 753
/
validation.py
335 lines (272 loc) · 12.7 KB
/
validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import collections
import copy
import functools
import logging
import sys
import six
from jsonschema import (Draft4Validator, ValidationError,
draft4_format_checker, validators)
from werkzeug import FileStorage
from ..exceptions import ExtraParameterProblem
from ..problem import problem
from ..utils import all_json, boolean, is_json_mimetype, is_null, is_nullable
logger = logging.getLogger('connexion.decorators.validation')
TYPE_MAP = {
'integer': int,
'number': float,
'boolean': boolean
}
def make_type(value, type_literal):
type_func = TYPE_MAP.get(type_literal)
return type_func(value)
class TypeValidationError(Exception):
def __init__(self, schema_type, parameter_type, parameter_name):
"""
Exception raise when type validation fails
:type schema_type: str
:type parameter_type: str
:type parameter_name: str
:return:
"""
self.schema_type = schema_type
self.parameter_type = parameter_type
self.parameter_name = parameter_name
def __str__(self):
msg = "Wrong type, expected '{schema_type}' for {parameter_type} parameter '{parameter_name}'"
return msg.format(**vars(self))
def validate_type(param, value, parameter_type, parameter_name=None):
param_type = param.get('type')
parameter_name = parameter_name if parameter_name else param['name']
if param_type == "array":
converted_params = []
for v in value:
try:
converted = make_type(v, param["items"]["type"])
except (ValueError, TypeError):
converted = v
converted_params.append(converted)
return converted_params
else:
try:
return make_type(value, param_type)
except ValueError:
raise TypeValidationError(param_type, parameter_type, parameter_name)
except TypeError:
return value
def validate_parameter_list(request_params, spec_params):
request_params = set(request_params)
spec_params = set(spec_params)
return request_params.difference(spec_params)
def extend_with_nullable_support(validator_class):
"""Add support for null values in body.
It adds property validator to given validator_class.
:param validator_class: validator to add nullable support
:type validator_class: jsonschema.IValidator
:return: new validator with added nullable support in properties
:rtype: jsonschema.IValidator
"""
validate_properties = validator_class.VALIDATORS['properties']
def nullable_support(validator, properties, instance, schema):
null_properties = {}
for property_, subschema in six.iteritems(properties):
if isinstance(instance, collections.Iterable) and \
property_ in instance and \
instance[property_] is None and \
subschema.get('x-nullable') is True:
# exclude from following validation
null_properties[property_] = instance.pop(property_)
for error in validate_properties(validator, properties, instance, schema):
yield error
# add null properties back
if null_properties:
instance.update(null_properties)
return validators.extend(validator_class, {'properties': nullable_support})
Draft4ValidatorSupportNullable = extend_with_nullable_support(Draft4Validator)
class RequestBodyValidator(object):
def __init__(self, schema, consumes, api, is_null_value_valid=False, validator=None):
"""
:param schema: The schema of the request body
:param consumes: The list of content types the operation consumes
:param is_null_value_valid: Flag to indicate if null is accepted as valid value.
:param validator: Validator class that should be used to validate passed data
against API schema. Default is jsonschema.Draft4Validator.
:type validator: jsonschema.IValidator
"""
self.consumes = consumes
self.has_default = schema.get('default', False)
self.is_null_value_valid = is_null_value_valid
validatorClass = validator or Draft4ValidatorSupportNullable
self.validator = validatorClass(schema, format_checker=draft4_format_checker)
self.api = api
def __call__(self, function):
"""
:type function: types.FunctionType
:rtype: types.FunctionType
"""
@functools.wraps(function)
def wrapper(request):
if all_json(self.consumes):
data = request.json
if data is None and len(request.body) > 0 and not self.is_null_value_valid:
try:
ctype_is_json = is_json_mimetype(request.headers.get("Content-Type", ""))
except ValueError:
ctype_is_json = False
if ctype_is_json:
# Content-Type is json but actual body was not parsed
return problem(400,
"Bad Request",
"Request body is not valid JSON"
)
else:
# the body has contents that were not parsed as JSON
return problem(415,
"Unsupported Media Type",
"Invalid Content-type ({content_type}), expected JSON data".format(
content_type=request.headers.get("Content-Type", "")
))
logger.debug("%s validating schema...", request.url)
error = self.validate_schema(data, request.url)
if error and not self.has_default:
return error
response = function(request)
return response
return wrapper
def validate_schema(self, data, url):
# type: (dict, AnyStr) -> Union[ConnexionResponse, None]
if self.is_null_value_valid and is_null(data):
return None
try:
self.validator.validate(data)
except ValidationError as exception:
logger.error("{url} validation error: {error}".format(url=url,
error=exception.message))
return problem(400, 'Bad Request', str(exception.message))
return None
class ResponseBodyValidator(object):
def __init__(self, schema, validator=None):
"""
:param schema: The schema of the response body
:param validator: Validator class that should be used to validate passed data
against API schema. Default is jsonschema.Draft4Validator.
:type validator: jsonschema.IValidator
"""
ValidatorClass = validator or Draft4ValidatorSupportNullable
self.validator = ValidatorClass(schema, format_checker=draft4_format_checker)
def validate_schema(self, data, url):
# type: (dict, AnyStr) -> Union[ConnexionResponse, None]
try:
self.validator.validate(data)
except ValidationError as exception:
logger.error("{url} validation error: {error}".format(url=url,
error=exception))
six.reraise(*sys.exc_info())
return None
class ParameterValidator(object):
def __init__(self, parameters, api, strict_validation=False):
"""
:param parameters: List of request parameter dictionaries
:param strict_validation: Flag indicating if parameters not in spec are allowed
"""
self.parameters = collections.defaultdict(list)
for p in parameters:
self.parameters[p['in']].append(p)
self.api = api
self.strict_validation = strict_validation
@staticmethod
def validate_parameter(parameter_type, value, param):
if value is not None:
if is_nullable(param) and is_null(value):
return
try:
converted_value = validate_type(param, value, parameter_type)
except TypeValidationError as e:
return str(e)
param = copy.deepcopy(param)
if 'required' in param:
del param['required']
try:
if parameter_type == 'formdata' and param.get('type') == 'file':
Draft4Validator(
param,
format_checker=draft4_format_checker,
types={'file': FileStorage}).validate(converted_value)
else:
Draft4Validator(
param, format_checker=draft4_format_checker).validate(converted_value)
except ValidationError as exception:
debug_msg = 'Error while converting value {converted_value} from param ' \
'{type_converted_value} of type real type {param_type} to the declared type {param}'
fmt_params = dict(
converted_value=str(converted_value),
type_converted_value=type(converted_value),
param_type=param.get('type'),
param=param
)
logger.info(debug_msg.format(**fmt_params))
return str(exception)
elif param.get('required'):
return "Missing {parameter_type} parameter '{param[name]}'".format(**locals())
def validate_query_parameter_list(self, request):
request_params = request.query.keys()
spec_params = [x['name'] for x in self.parameters.get('query', [])]
return validate_parameter_list(request_params, spec_params)
def validate_formdata_parameter_list(self, request):
request_params = request.form.keys()
spec_params = [x['name'] for x in self.parameters.get('formData', [])]
return validate_parameter_list(request_params, spec_params)
def validate_query_parameter(self, param, request):
"""
Validate a single query parameter (request.args in Flask)
:type param: dict
:rtype: str
"""
val = request.query.get(param['name'])
return self.validate_parameter('query', val, param)
def validate_path_parameter(self, param, request):
val = request.path_params.get(param['name'].replace('-', '_'))
return self.validate_parameter('path', val, param)
def validate_header_parameter(self, param, request):
val = request.headers.get(param['name'])
return self.validate_parameter('header', val, param)
def validate_formdata_parameter(self, param, request):
if param.get('type') == 'file':
val = request.files.get(param['name'])
else:
val = request.form.get(param['name'])
return self.validate_parameter('formdata', val, param)
def __call__(self, function):
"""
:type function: types.FunctionType
:rtype: types.FunctionType
"""
@functools.wraps(function)
def wrapper(request):
logger.debug("%s validating parameters...", request.url)
if self.strict_validation:
query_errors = self.validate_query_parameter_list(request)
formdata_errors = self.validate_formdata_parameter_list(request)
if formdata_errors or query_errors:
raise ExtraParameterProblem(formdata_errors, query_errors)
for param in self.parameters.get('query', []):
error = self.validate_query_parameter(param, request)
if error:
response = problem(400, 'Bad Request', error)
return self.api.get_response(response)
for param in self.parameters.get('path', []):
error = self.validate_path_parameter(param, request)
if error:
response = problem(400, 'Bad Request', error)
return self.api.get_response(response)
for param in self.parameters.get('header', []):
error = self.validate_header_parameter(param, request)
if error:
response = problem(400, 'Bad Request', error)
return self.api.get_response(response)
for param in self.parameters.get('formData', []):
error = self.validate_formdata_parameter(param, request)
if error:
response = problem(400, 'Bad Request', error)
return self.api.get_response(response)
return function(request)
return wrapper