def colander_bound_repository_body_validator(request, schema=None, deserializer=None, validator=colander_body_validator, **kwargs): if schema: schema = schema.bind(repository=request.repository) for method in kwargs.get('response_schemas', {}): kwargs['response_schemas'][method] = kwargs['response_schemas'][ method].bind(repository=request.repository) return colander_body_validator(request, schema=schema, **kwargs)
def colander_validator(request, **kwargs): if 'klass' in kwargs: # Instantiate the class params = dict(request=request) params['context'] = request.context ob = kwargs['klass'](**params) if getattr(ob, 'schema', None) is not None: # Get the schema from the object kwargs['schema'] = ob.schema return colander_body_validator(request, **kwargs) return True
def _run_validators(self, request: Request, **kwargs): """Run all validators for the current http method. :param request: request object """ validate_jwt_token(request) request_method = request.method validators = self.validators.get(request_method, []) for item in validators: try: validator = item if isinstance(item, str): validator = getattr(self, item) except AttributeError as e: raise AttributeError( f'Validator "{item}" specified not found.') else: validator(request) # Only validate body if we expect a body in the method if request_method in ('PATCH', 'POST', 'PUT'): colander_body_validator(request, self.schema)
def test_no_body_schema(self): request = DummyRequest() request.validated = mock.sentinel.validated colander_body_validator(request) self.assertEqual(request.validated, mock.sentinel.validated) self.assertEqual(len(request.errors), 0)
def rebinding_validator(request, **kwargs): kwargs['schema'] = NeedsBindingSchema().bind() return colander_body_validator(request, **kwargs)
def test_no_body_schema(self): request = DummyRequest() request.validated = mock.sentinel.validated colander_body_validator(request) self.assertEqual(request.validated, mock.sentinel.validated) self.assertEqual(len(request.errors), 0)
def body_validator(request, **kwargs): schema = FruitSchema() kwargs['schema'] = schema.bind(request=request) return colander_body_validator(request, **kwargs)