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

Type error for valid validation rule and data #128

Open
karanAT opened this issue Oct 2, 2021 · 2 comments
Open

Type error for valid validation rule and data #128

karanAT opened this issue Oct 2, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@karanAT
Copy link

karanAT commented Oct 2, 2021

I am getting TypeError when I am trying to validate a dict with proper validation rule.

The validation_rule I am using : {'email': 'required|mail', 'password': 'required|min:6'}

The data I am trying to validate : {'deviceSerialNumber': 'dd2114d80437f4ba7aasdf3d6ef02932e2', 'firmwareVersion': '1.01', 'deviceType': 'daylite', 'deviceUuid': 'd7f4ba7a83d63ef02932', 'password': ''}

In this case, the validation should fail, providing validation errors. However, I am getting TypeError.

Traceback :

Traceback (most recent call last):
File "", line 1, in
File "./env/lib/python3.9/site-packages/validator/validator.py", line 82, in validate
result = val.validate()
File "./env/lib/python3.9/site-packages/validator/validator.py", line 31, in validate
rw.run()
File "./env/lib/python3.9/site-packages/validator/rules_wrapper.py", line 27, in run
rpv_result = rpv.execute()
File "./env/lib/python3.9/site-packages/validator/rule_pipe_validator.py", line 17, in execute
if not rule(self.data):
File "./env/lib/python3.9/site-packages/validator/rules_src/init.py", line 15, in call
result = self.check(arg)
File "./env/lib/python3.9/site-packages/validator/rules_src/mail.py", line 28, in check
if re.search(self.regex, arg) is not None:
File "/usr/lib/python3.9/re.py", line 201, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

How I am using validate :

validation_rule = {'email': 'required|mail', 'password': 'required|min:6'}
data = {'deviceSerialNumber': 'dd2114d80437f4ba7aasdf3d6ef02932e2', 'firmwareVersion': '1.01', 'deviceType': 'daylite', 'deviceUuid': 'd7f4ba7a83d63ef02932', 'password': ''}
from validator import validate
validate(data, validation_rule)

@karanAT karanAT added the bug Something isn't working label Oct 2, 2021
@karanAT
Copy link
Author

karanAT commented Oct 3, 2021

The 'mail' rule is not checking if the provided arg is of type(str), which is causing the issue. (In this case, it is of type(None)). I have implemented the check and the issue seems to be resolved. I have created a new branch and fixed the issue, but, I do not have the required permissions to push the commit.

This is the check method in 'mail' rule after fix :

def check(self, arg):
        if type(arg) == str and re.search(self.regex, arg) is not None:
            return True

        self.set_error(f"Expected a Mail, Got: {arg}")
        return False

Please accept the commit I am trying to push or add this to the code as a permanent or temp fix (if there is any other better solution) and create the package.

@karanAT
Copy link
Author

karanAT commented Oct 3, 2021

Another possible solution :

def check(self, arg):
        if type(arg) != str:
            if arg == None:
                return True
            raise TypeError(
                f"Value should be of type str. {arg} of type {type(arg)} passed."
            )

        if type(arg) == str and re.search(self.regex, arg) is not None:
            return True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant