Skip to content
Snippets Groups Projects
validators.py 1.89 KiB
Newer Older
  • Learn to ignore specific revisions
  • # Copyright 2019 PrivateStorage.io, LLC
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    """
    This module implements validators for ``attrs``-defined attributes.
    """
    
    
    Tom Prince's avatar
    Tom Prince committed
    from base64 import b64decode
    
    Tom Prince's avatar
    Tom Prince committed
    
    
    def is_base64_encoded(b64decode=b64decode):
        def validate_is_base64_encoded(inst, attr, value):
            try:
                b64decode(value.encode("ascii"))
            except TypeError:
                raise TypeError(
                    "{name!r} must be base64 encoded unicode, (got {value!r})".format(
                        name=attr.name,
                        value=value,
                    ),
                )
    
    Tom Prince's avatar
    Tom Prince committed
    
    
        return validate_is_base64_encoded
    
    
    Tom Prince's avatar
    Tom Prince committed
    
    
    def has_length(expected):
        def validate_has_length(inst, attr, value):
            if len(value) != expected:
                raise ValueError(
                    "{name!r} must have length {expected}, instead has length {actual}".format(
                        name=attr.name,
                        expected=expected,
                        actual=len(value),
                    ),
                )
    
    Tom Prince's avatar
    Tom Prince committed
    
    
    Tom Prince's avatar
    Tom Prince committed
    
    
    def greater_than(expected):
        def validate_relation(inst, attr, value):
            if value > expected:
                return None
    
            raise ValueError(
                "{name!r} must be greater than {expected}, instead it was {actual}".format(
                    name=attr.name,
                    expected=expected,
                    actual=value,
                ),
            )
    
    Tom Prince's avatar
    Tom Prince committed