예제 #1
0
def validate_truthy(value: Any) -> None:
    try:
        formatted_value = format_boolean(value)
    except ValueError:
        raise ValidationError(
            f"Passed value {value} is not valid truthy expression.")

    if formatted_value is True:
        return

    raise ValidationError(
        f"Passed value {value} is not valid truthy expression.")
예제 #2
0
def validate_email(value: str) -> None:
    """
    Keep in mind this validator willfully violates RFC 5322, the best way to invalidate email address is to send
    a message and receive confirmation from the recipient.

    :param str value:
    :return None:
    """
    if not _EMAIL_REGEX.match(value):
        raise ValidationError(
            f"Passed value {value} is not valid email address.")
    if ".." in value:
        raise ValidationError(
            f"Passed value {value} is not valid email address.")
예제 #3
0
    def validate(self, value: Any) -> None:
        super().validate(value)

        if value not in self.allowed_values:
            raise ValidationError(
                f"Passed value `{value}` is not within allowed values `{self.allowed_values}`."
            )

        return value
예제 #4
0
    def validate(self, value: dict) -> None:
        super().validate(value)
        for prop in self.required:
            if prop not in value:
                raise ValidationError(
                    f"Missing required property `{prop}` in passed dataset `{value}`"
                )

        for key, prop in self.properties.items():
            if key not in value:
                continue
            validate(value[key], prop)
예제 #5
0
def validate_date(
    value: Union[str, date],
    minimum: Optional[date] = None,
    maximum: Optional[date] = None,
) -> None:
    try:
        date_value = format_date(value)
    except ValueError:
        raise ValidationError(f"Passed value {value} is not valid ISO 8601 date.")

    if not date_value:
        raise ValidationError(f"Passed value {value} is not valid ISO 8601 date.")

    if minimum and date_value < minimum:
        raise NotWithinMinimumBoundaryError(
            f"Passed date `{date_value}` is lower than set minimum value `{minimum}`."
        )

    if maximum and date_value > maximum:
        raise NotWithinMaximumBoundaryError(
            f"Passed date `{date_value}` is greater than set maximum value `{maximum}`."
        )
예제 #6
0
    def validate(self, value: Union[list, tuple]) -> None:
        super().validate(value)

        if self.unique_items and not len(set(value)) == len(value):
            raise ValidationError(
                "Items in the array should be unique, passed array contains duplicates."
            )

        if isinstance(self.items, Type):
            for item in value:
                self.items.validate(item)

        if self.min_length or self.max_length:
            validators.validate_length(value, self.min_length, self.max_length)
예제 #7
0
def validate_time(value: str,
                  minimum: Optional[time] = None,
                  maximum: Optional[time] = None) -> None:
    """
    Validates time in iso standard
    :: _ISO Standard: http://tools.ietf.org/html/rfc3339#section-5.6
    """
    try:
        parsed_time = format_time(value)
    except ValueError:
        raise ValidationError(
            f"Passed value {value} is not compatible ISO time")

    if minimum and parsed_time < minimum:
        raise NotWithinMinimumBoundaryError(
            f"Passed time {value} is not within minimum boundary {minimum}")

    if maximum and parsed_time > maximum:
        raise NotWithinMaximumBoundaryError(
            f"Passed time {value} is not within maximum boundary {maximum}")
예제 #8
0
def validate_url(value: str) -> None:
    if not _URL_REGEX.match(value):
        raise ValidationError(f"Passed value {value} is not valid url.")
예제 #9
0
def validate_ipv6(value: str) -> None:
    if not _IPV6_REGEX.match(value):
        raise ValidationError(
            f"Passed value {value} is not valid ipv6 address.")
예제 #10
0
def validate_base64(value: str) -> None:
    try:
        base64.b64decode(value)
    except binascii.Error:
        raise ValidationError(
            "Passed value is not valid base64 encoded string.")
예제 #11
0
def validate_semver(value: str) -> None:
    if not _SEMVER_REGEX.match(value):
        raise ValidationError(
            f"Passed value {value} is not valid semantic version number.")
예제 #12
0
def validate_hostname(value: str) -> None:
    if not _HOSTNAME_REGEX.match(value):
        raise ValidationError(f"Passed value {value} is not valid hostname.")
예제 #13
0
def validate_uuid(value: str) -> None:
    if not _UUID_REGEX.match(value):
        raise ValidationError(f"Passed value {value} is not valid uuid.")
예제 #14
0
def validate_multiple_of(value: float, multiple_of: float) -> None:
    if not value % multiple_of == 0:
        raise ValidationError(
            f"Passed value `{value}` must be multiplication of {multiple_of}."
        )