Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashType requires LENGTH #600

Open
bkreider opened this issue Oct 10, 2019 · 1 comment
Open

HashType requires LENGTH #600

bkreider opened this issue Oct 10, 2019 · 1 comment

Comments

@bkreider
Copy link

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
@lkraider
Copy link
Contributor

HashType is used as an abstract type, but I agree it should have some more obvious way to signal that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants