Пример #1
0
 def validate_attributes(self, attrs, source):
     if source in attrs:
         value = attrs[source]
         if value is None:
             bad_param = BadParameterValueException('null')
             raise ValidationError(bad_param.details)
     return attrs
Пример #2
0
 def validate_bindings(self, attrs, source):
     # To simplify, only size of one is allowed in bindings
     value = attrs.get(source, ())
     try:
         [ObjectId(instance) for instance in value]
     except (InvalidId, TypeError):
         bad_param = BadParameterValueException(instance)
         raise ValidationError(bad_param.details)
     return attrs
Пример #3
0
 def validate_origin(self, attrs, source):
     """
     Check that origin:default in lowercase only
     """
     origin = attrs[source]
     if origin.lower() == 'default' and origin != 'default':
         bad_param = BadParameterValueException(origin)
         raise ValidationError(bad_param.details)
     return attrs
Пример #4
0
    def create_dynamic_fields(self, obj):
        if not getattr(self, '_errors', None):
            self._errors = {}
        field_mapping = {
            str: CharRestrictField,  # Only string is allowed as dynamic fields
            unicode: CharRestrictField
        }
        if not isinstance(obj, dict):
            bad_param = BadParameterValueException(obj)
            raise ValidationError(bad_param.details)
        # Min_size and max_size validations
        if len(obj) < self.min_size or len(obj) > self.max_size:
            bad_param = BadParameterValueException(obj)
            raise ValidationError(bad_param.details)

        for (key, value) in obj.items():
            # Key length validations (value is done as field...)
            if len(key) < self.min_key_length or len(
                    key) > self.max_key_length:
                bad_param = BadParameterValueException(key)
                raise ValidationError(bad_param.details)

            if key not in self.fields:
                try:
                    if not match(
                            '^{0}$'.format(settings.ATTTRIBUTES_KEYS_REGEX),
                            key):
                        bad_param = BadParameterValueException(key)
                        raise ValidationError(bad_param.details)
                    else:
                        field = field_mapping[type(value)](
                            min_length=self.min_value_length,
                            max_length=self.max_value_length)
                        field.initialize(parent=self, field_name=key)
                        self.fields[key] = field
                except KeyError:
                    bad_param = BadParameterValueException(value)
                    raise ValidationError(bad_param.details)
            else:
                # fields already defined -> doesn't need to be created
                continue
Пример #5
0
        def wrapper(*args, **kwargs):
            called_view = args[0]
            request = args[1]
            # include fields not coming in request for validation
            # request.DATA: contains the query parameters + the body
            compose_data = request.DATA.copy()
            # kwargs. contains the url parameters
            compose_data.update(kwargs)
            # Get serializer and check data is valid
            serializer = called_view.get_serializer(data=compose_data,
                                                    partial=partial)
            if validate and not serializer.is_valid():
                if '_schema_' in serializer.errors:
                    # Schema Validation error
                    raise BadParameterValueException(
                        serializer.errors['_schema_'][0])

                # check for nested serializer errors and look inside errors array
                (key, value, text) = get_first_error(serializer.errors,
                                                     compose_data)
                if error_messages['required'] in text:
                    raise MissingMandatoryParameterException(key)
                elif error_messages['invalid'] in text:
                    raise BadParameterValueException(value)
                elif match(
                        NotAllowedParameterValueException.get_regex_unica_code(
                        ), text):
                    raise NotAllowedParameterValueException(None,
                                                            None,
                                                            details=text)
                elif match(BadParameterValueException.get_regex_unica_code(),
                           text):
                    raise BadParameterValueException(None, details=text)
                else:
                    raise BadRequestException(text)
            # update request.DATA, request. and call the view
            kwargs['deserialized_object'] = serializer.object
            return f(called_view, request, **kwargs)
Пример #6
0
    def validate_value(self, attrs, source):
        # Validation of values depending on rules
        value = attrs[source]
        operation = attrs.get('operation', None)
        if operation == 'range':
            if len(value) != 2 or type(value[0]) != type(value[1]) or value[0] == value[1] or value[0] > value[1]:
                bad_param = BadParameterValueException(value)
                raise ValidationError(bad_param.details)
        elif operation == 'in':
            first_type_in = type(value[0])
            for elem in value[1:]:
                if type(elem) != first_type_in:
                    bad_param = BadParameterValueException(elem)
                    raise ValidationError(bad_param.details)
            try:
                unrepeated_set = set(value)
                if len(unrepeated_set) < len(value):
                    bad_param = BadParameterValueException(value)
                    raise ValidationError(bad_param.details)
            except TypeError:
                bad_param = BadParameterValueException(value)
                raise ValidationError(bad_param.details)
        elif operation == 'regex':
            bad_param = BadParameterValueException(value)
            if len(value) > 1:
                raise ValidationError(bad_param.details)
            try:
                compile(value[0])
            except CompileError:
                raise ValidationError(bad_param.details)
        elif operation == 'eq':
            if len(value) > 1:
                bad_param = BadParameterValueException(value)
                raise ValidationError(bad_param.details)

        return attrs
Пример #7
0
 def wrapper(*args, **kwargs):
     request = args[1]
     if any(imap(lambda x: x == '', request.QUERY_PARAMS.values())):
         raise BadParameterValueException('empty-query-parameter')
     return f(*args, **kwargs)
Пример #8
0
 def validate_input_context_param(self, attrs, source):
     value = attrs[source]
     if not match(settings.INPUT_CONTEXT_KEYS_CREATE_REGEX, value):
         bad_param = BadParameterValueException(value)
         raise ValidationError(bad_param.details)
     return attrs