예제 #1
0
def colander_querystring_validator(request,
                                   schema=None,
                                   deserializer=None,
                                   **kwargs):
    """
    Validate the querystring against the schema defined on the service.

    The content of the querystring is deserialized, validated and stored in the
    ``request.validated`` attribute.

    .. note::

        If no schema is defined, this validator does nothing.

    :param request: Current request
    :type request: :class:`~pyramid:pyramid.request.Request`

    :param schema: The Colander schema
    :param deserializer: Optional deserializer, defaults to
        :func:`cornice.validators.extract_cstruct`
    """
    class RequestSchema(colander.MappingSchema):
        querystring = schema

    colander_validator(request, RequestSchema(), deserializer, **kwargs)
    request.validated = request.validated.get('querystring', {})
예제 #2
0
    def test_schema_class_deprecated(self):
        class RequestSchema(colander.MappingSchema):
            body = colander.MappingSchema()

        request = DummyRequest()
        request.validated = {}
        with warnings.catch_warnings(record=True) as w:
            warnings.resetwarnings()
            colander_validator(request, schema=RequestSchema)
        self.assertEqual(len(w), 1)
        self.assertIs(w[0].category, DeprecationWarning)
예제 #3
0
    def test_schema_class_deprecated(self):
        class RequestSchema(colander.MappingSchema):
            body = colander.MappingSchema()

        request = DummyRequest()
        request.validated = {}
        with warnings.catch_warnings(record=True) as w:
            warnings.resetwarnings()
            colander_validator(request, schema=RequestSchema)
        self.assertEqual(len(w), 1)
        self.assertIs(w[0].category, DeprecationWarning)
예제 #4
0
 def test_no_schema(self):
     request = DummyRequest()
     request.validated = mock.sentinel.validated
     colander_validator(request)
     self.assertEqual(request.validated, mock.sentinel.validated)
     self.assertEqual(len(request.errors), 0)
예제 #5
0
 def test_no_schema(self):
     request = DummyRequest()
     request.validated = mock.sentinel.validated
     colander_validator(request)
     self.assertEqual(request.validated, mock.sentinel.validated)
     self.assertEqual(len(request.errors), 0)