Example #1
0
def datatype_address(field, value):
    valid_keys = (
        'extension',
        'street',
        'postalCode',
        'city',
        'county',
        'state',
        'country',
        'countryCode',
        'district',
        'buildingName',
        'buildingFloor',
        'buildingRoom',
        'buildingZone',
        'crossing1',
        'crossing2',
    )

    if not isinstance(value, dict):
        raise InvalidDatatypeException(field, dict, type(value))

    # Make sure the user didn't submit an unexpected key.
    for key in value:
        if key not in valid_keys:
            raise ExtraDataSubmittedException(field, key)

    # Make sure all values are str.
    for k, v in value.items():
        if v is not None and not isinstance(v, str):
            raise InvalidDatatypeException('{}[{}]'.format(
                field, v), str, type(v))
Example #2
0
def datatype_birthday(field, value):
    try:
        assert isinstance(value, dict)
    except AssertionError:
        raise InvalidDatatypeException(field, dict, type(value))

    required_keys = ('day', 'month', 'year')

    for key in value:
        if key not in required_keys:
            raise ValueError(
                'Invalid key of {} ... must be one of day|month|year.'.format(
                    key))

        try:
            assert isinstance(value[key], int)
        except AssertionError:
            raise InvalidDatatypeException(
                '{}[{}]'.format(field, key), int, type(value[key]))

        keyval = value[key]

        if key == 'month' and not (1 <= keyval <= 12):
            raise InvalidValueException(
                '{}[{}]'.format(field, key), keyval, '1-12')
        elif key == 'day' and not (1 <= keyval <= 31):
            raise InvalidValueException(
                '{}[{}]'.format(field, key), keyval, '1-31')
        elif key == 'year' and not (1800 <= keyval <= 2100):
            raise InvalidValueException(
                '{}[{}]'.format(field, key), keyval, '1800-2100')
Example #3
0
def datatype_list_of_str(field, value):
    """Assert that the value is of type list containing str."""
    try:
        assert isinstance(value, (list, tuple))
        for i, val in enumerate(value):
            try:
                assert isinstance(val, str)
            except AssertionError:
                raise InvalidDatatypeException(
                    '{}[{}]'.format(field, i), str, type(val))
    except AssertionError:
        raise InvalidDatatypeException(field, (list, tuple), type(value))
Example #4
0
def datatype_dict_of_str(field, value):
    """Assert that the value is a dict of str's."""
    datatype_dict(field, value)
    for k, v in value.items():
        try:
            assert isinstance(k, str)
        except:
            raise InvalidDatatypeException(
                    '{}[{}]'.format(field, k), str, type(k))
        try:
            assert isinstance(v, str)
        except:
            raise InvalidDatatypeException(
                    '{}[{}].value'.format(field, k), str, type(v))
Example #5
0
def datatype_url(field, value):
    """Assert that the value is a URL."""
    try:
        result = urlparse(value)
        assert result.scheme and result.netloc
    except (AssertionError, AttributeError):
        raise InvalidDatatypeException(field, str, type(value))
Example #6
0
def datatype_json(field, value):
    """
    Evrythng parses the value into its native JSON datatype, so the
    value must be JSONifiable.
    """
    try:
        json.dumps(value)
    except (ValueError, TypeError):
        raise InvalidDatatypeException(field, json, type(value))
Example #7
0
def datatype_dict(field, value):
    """Assert that the value is of type dict."""
    try:
        assert isinstance(value, dict)
    except AssertionError:
        raise InvalidDatatypeException(field, dict, type(value))
Example #8
0
def datatype_time(field, value):
    """Assert that the value is of type int."""
    try:
        assert isinstance(value, int)
    except AssertionError:
        raise InvalidDatatypeException(field, int, type(value))
Example #9
0
def datatype_str(field, value):
    """Assert that the value is of type str."""
    try:
        assert isinstance(value, str)
    except AssertionError:
        raise InvalidDatatypeException(field, str, type(value))
Example #10
0
def datatype_bool(field, value):
    try:
        assert isinstance(value, bool)
    except AssertionError:
        raise InvalidDatatypeException(field, bool, type(value))
Example #11
0
def datatype_number(field, value):
    """Assert that the value is a number (int or float)."""
    try:
        float(value)
    except (TypeError, ValueError):
        raise InvalidDatatypeException(field, (int, float), type(value))