vininfo icon indicating copy to clipboard operation
vininfo copied to clipboard

Vin().validate() requires vin that is already given

Open NFSpeedy opened this issue 2 years ago • 4 comments

It seems irrational to be, but to make validate (classmethod) work, you have to instantiate the Vin with a VIN number and call validate again with a VIN number.

Vin('someVIN').validate('someVIN') is the only way that I managed to make it work. In my humble opinion, you should be able to check like so:

vin = 'SomeVIN'
vin_object = Vin(vin)
vin_object.validate() # This returns True or False

P.S.1: Or even better would be:

vin = 'SomeVIN'
vin_object = Vin(vin)
if vin_object.is_valid:  # This returns True or False
    print('Yay!')
vin_object.is_valid_with_exception()  # or something like that
>>> InvalidVIN: This VIN number is invalid.

P.S.2: I had an idea:

def validate(self, num: str = None, with_exception: bool = False) -> str:
        """Performs basic VIN validation and sanation.

        :param num: Other VIN to check
        :param with_exception: Flag to determine if the validate function should raise an exception or not.


        """
        if num is None:
                num = self.num
        num = num.strip().upper()

        if len(num) != 17:
                raise ValidationError(f'VIN number requires 17 chars ({num_len} given)')

        pattern = r"^[A-HJ-NPR-Z0-9]{17}$"
        if not re.match(pattern, num):
                if with_exception:
                        raise ValidationError(f"VIN number must only contain alphanumeric symbols except 'I', 'O', and 'Q' ")
                else:
                        return False
                return True

Cheers 🍺

NFSpeedy avatar Jul 31 '22 17:07 NFSpeedy

you have to instantiate the Vin with a VIN number and call validate again with a VIN number.

Do you mean Vin.validate('X') don't work for you?

If you need a property .is_valid pull request is welcome.

idlesign avatar Aug 01 '22 02:08 idlesign

Hi Igor,

No, it does not.

NFSpeedy avatar Aug 01 '22 06:08 NFSpeedy

Hi,

No, it does not.

validate() can be accessed explicitly (as a class method); otherwise it's called implicitly on object instantiation, so you do not need to call it again. And what is your use case?

idlesign avatar Aug 01 '22 07:08 idlesign

This should work for your use case:

vin = 'SomeVIN'
try:
    Vin(vin)
except Exception:
    return False
else:
    return True

gazlaws-dev avatar Sep 23 '22 07:09 gazlaws-dev