CSenshi / Validator

Easy-to-use, Highly Configurable Python Data Validator. Inspired by Laravel Validator
https://pypi.org/project/validator/
MIT License
46 stars 23 forks source link

Type error for valid validation rule and data #128

Open karanAT opened 3 years ago

karanAT commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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