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

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

Open
NFSpeedy opened this issue Jul 31, 2022 · 4 comments
Open

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

NFSpeedy opened this issue Jul 31, 2022 · 4 comments
Labels
question Further information is requested

Comments

@NFSpeedy
Copy link

NFSpeedy commented Jul 31, 2022

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 🍺

@idlesign
Copy link
Owner

idlesign commented Aug 1, 2022

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 idlesign added the question Further information is requested label Aug 1, 2022
@NFSpeedy
Copy link
Author

NFSpeedy commented Aug 1, 2022

Hi Igor,

No, it does not.

@idlesign
Copy link
Owner

idlesign commented Aug 1, 2022

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?

@gazlaws-dev
Copy link

This should work for your use case:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants