Skip to content

HashType requires LENGTH #600

Open
Open
@bkreider

Description

@bkreider

I was using the wrong Type, but still HashType raises an error on to_native(), because it expects a Class attribute LENGTH. This works for SHA and the derived hashtypes because they have a length, but if you use HashType by itself, you also need to set LENGTH or it will die on to_native(). Note: it validates fine without LENGTH.

class HashType(StringType):

    MESSAGES = {
        'hash_length': _("Hash value is wrong length."),
        'hash_hex': _("Hash value is not hexadecimal."),
    }

    def _mock(self, context=None):
        return random_string(self.LENGTH, string.hexdigits)

    def to_native(self, value, context=None):
        value = super(HashType, self).to_native(value, context)

        if len(value) != self.LENGTH:
            raise ValidationError(self.messages['hash_length'])
        try:
            int(value, 16)
        except ValueError:
            raise ConversionError(self.messages['hash_hex'])
        return value

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions