def generate_path_validator(api_path, path_definition, parameters, context, **kwargs): """ Generates a callable for validating the parameters in a response object. """ path_level_parameters = dereference_parameter_list( path_definition.get('parameters', []), context, ) operation_level_parameters = dereference_parameter_list( parameters, context, ) all_parameters = merge_parameter_lists( path_level_parameters, operation_level_parameters, ) # PATH in_path_parameters = filter_parameters(all_parameters, in_=PATH) return chain_reduce_partial( attrgetter('path'), generate_path_parameters_validator(api_path, in_path_parameters, context), )
def generate_response_header_validator(headers, context, **kwargs): validators = ValidationDict() for key, header_definition in headers.items(): # generate a function that will attempt to cast the header to the # appropriate type. header_processor = generate_value_processor( context=context, **header_definition ) # generate a function that will validate the header. header_validator = functools.partial( validate_object, field_validators=construct_header_validators(header_definition, context=context), ) # Chain the type casting function, the individual header validation # function with a methodcaller that will fetch the header with # `response.headers.get(header_name, EMPTY)` and then feed that into # the type casting function and then into the validation function. validators.add_validator(key, chain_reduce_partial( methodcaller('get', key, EMPTY), header_processor, header_validator, )) return chain_reduce_partial( attrgetter('headers'), functools.partial(validate_object, field_validators=validators), )
def generate_response_body_validator(schema, context, **kwargs): return chain_reduce_partial( attrgetter('data'), functools.partial( validate_object, schema=schema, context=context, ), )
def generate_parameters_validator(api_path, path_definition, parameters, context, **kwargs): """ Generates a validator function to validate. - request.path against the path parameters. - request.query against the query parameters. - request.headers against the header parameters. - TODO: request.body against the body parameters. - TODO: request.formData against any form data. """ # TODO: figure out how to merge this with the same code in response # validation. validators = ValidationDict() path_level_parameters = dereference_parameter_list( path_definition.get('parameters', []), context, ) operation_level_parameters = dereference_parameter_list( parameters, context, ) all_parameters = merge_parameter_lists( path_level_parameters, operation_level_parameters, ) # PATH in_path_parameters = filter_parameters(all_parameters, in_=PATH) validators.add_validator( 'path', chain_reduce_partial( attrgetter('path'), generate_path_parameters_validator(api_path, in_path_parameters, context), ), ) # QUERY in_query_parameters = filter_parameters(all_parameters, in_=QUERY) validators.add_validator( 'query', chain_reduce_partial( attrgetter('query_data'), functools.partial( validate_query_parameters, query_parameters=in_query_parameters, context=context, ), ), ) # HEADERS in_header_parameters = filter_parameters(all_parameters, in_=HEADER) validators.add_validator( 'headers', chain_reduce_partial( attrgetter('headers'), generate_header_validator(in_header_parameters, context), ), ) # FORM_DATA # in_form_data_parameters = filter_parameters(all_parameters, in_=FORM_DATA) # validators.add_validator( # 'form_data', # chain_reduce_partial( # attrgetter('data'), # generate_form_data_validator(in_form_data_parameters, context), # ) # ) # REQUEST_BODY in_request_body_parameters = filter_parameters(all_parameters, in_=BODY) validators.add_validator( 'request_body', chain_reduce_partial( attrgetter('data'), generate_request_body_validator(in_request_body_parameters, context), ) ) return generate_object_validator(field_validators=validators)