How to use the masonite.validation.Validator.BaseValidation function in masonite

To help you get started, we’ve selected a few masonite examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
return '{} must not be a valid email address'.format(attribute)


class exists(BaseValidation):

    def passes(self, attribute, key, dictionary):
        return key in dictionary

    def message(self, attribute):
        return '{} must exist'.format(attribute)

    def negated_message(self, attribute):
        return '{} must not exist'.format(attribute)


class active_domain(BaseValidation):

    def passes(self, attribute, key, dictionary):
        import socket
        try:
            if '@' in attribute:
                # validation is for an email address
                return socket.gethostbyname(
                    attribute.split('@')[1]
                )

            return socket.gethostbyname(
                attribute.replace('https://', '').replace('http://', '').replace('www.', '')
            )
        except socket.gaierror:
            return False
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
def __init__(self, validations, value='', messages={}, raises={}):
        super().__init__(validations, messages=messages, raises=raises)
        self.value = value

    def passes(self, attribute, key, dictionary):
        return attribute in self.value

    def message(self, attribute):
        return '{} must contain an element in {}'.format(attribute, self.value)

    def negated_message(self, attribute):
        return '{} must not contain an element in {}'.format(attribute, self.value)


class greater_than(BaseValidation):

    def __init__(self, validations, value='', messages={}, raises={}):
        super().__init__(validations, messages=messages, raises=raises)
        self.value = value

    def passes(self, attribute, key, dictionary):
        return attribute > self.value

    def message(self, attribute):
        return '{} must be greater than {}'.format(attribute, self.value)

    def negated_message(self, attribute):
        return '{} must be greater than {}'.format(attribute, self.value)


class less_than(BaseValidation):
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
def __init__(self, validations, min=1, max=255, messages={}, raises={}):
        super().__init__(validations, messages=messages, raises=raises)
        self.min = min
        self.max = max

    def passes(self, attribute, key, dictionary):
        return attribute >= self.min and attribute <= self.max

    def message(self, attribute):
        return '{} must be between {} and {}'.format(attribute, self.min, self.max)

    def negated_message(self, attribute):
        return '{} must not be between {} and {}'.format(attribute, self.min, self.max)


class equals(BaseValidation):

    def __init__(self, validations, value='', messages={}, raises={}):
        super().__init__(validations, messages=messages, raises=raises)
        self.value = value

    def passes(self, attribute, key, dictionary):
        return attribute == self.value

    def message(self, attribute):
        return '{} must be equal to {}'.format(attribute, self.value)

    def negated_message(self, attribute):
        return '{} must not be equal to {}'.format(attribute, self.value)


class contains(BaseValidation):
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
def passes(self, attribute, key, dictionary):
        import pendulum
        try:
            return pendulum.parse(attribute, tz=self.tz) <= pendulum.yesterday()
        except pendulum.parsing.exceptions.ParserError:
            return False

    def message(self, attribute):
        return '{} must be a date before today'.format(attribute)

    def negated_message(self, attribute):
        return '{} must not be a date before today'.format(attribute)


class after_today(BaseValidation):

    def __init__(self, validations, tz='Universal', messages={}, raises={}):
        super().__init__(validations, messages=messages, raises=raises)
        self.tz = tz

    def passes(self, attribute, key, dictionary):
        import pendulum
        try:
            return pendulum.parse(attribute, tz=self.tz) >= pendulum.yesterday()
        except pendulum.parsing.exceptions.ParserError:
            return False

    def message(self, attribute):
        return '{} must be a date after today'.format(attribute)

    def negated_message(self, attribute):
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
def __init__(self, validations, value='', messages={}, raises={}):
        super().__init__(validations, messages=messages, raises=raises)
        self.value = value

    def passes(self, attribute, key, dictionary):
        return self.value in attribute

    def message(self, attribute):
        return '{} must contain {}'.format(attribute, self.value)

    def negated_message(self, attribute):
        return '{} must not contain {}'.format(attribute, self.value)


class is_in(BaseValidation):

    def __init__(self, validations, value='', messages={}, raises={}):
        super().__init__(validations, messages=messages, raises=raises)
        self.value = value

    def passes(self, attribute, key, dictionary):
        return attribute in self.value

    def message(self, attribute):
        return '{} must contain an element in {}'.format(attribute, self.value)

    def negated_message(self, attribute):
        return '{} must not contain an element in {}'.format(attribute, self.value)


class greater_than(BaseValidation):
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
def negated_message(self, attribute):
        return '{} must not be less than {}'.format(attribute, self.value)


class isnt(BaseValidation):

    def __init__(self, *rules, messages={}, raises={}):
        super().__init__(rules)

    def handle(self, dictionary):
        for rule in self.validations:
            rule.negate().handle(dictionary)
            self.errors.update(rule.errors)


class when(BaseValidation):

    def __init__(self, *rules, messages={}, raises={}):
        super().__init__(rules)
        self.should_run_then = True

    def handle(self, dictionary):
        self.dictionary = dictionary
        for rule in self.validations:
            if not rule.handle(dictionary):
                self.errors.update(rule.errors)

        if not self.errors:
            for rule in self.then_rules:
                if not rule.handle(dictionary):
                    self.errors.update(rule.errors)
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
def negated_message(self, key):
        """A message to show when this rule is negated using a negation rule like 'isnt()'

        For example if you have a message that says 'this is required' you may have a negated statement
        that says 'this is not required'.

        Arguments:
            key {string} -- The key used to search the dictionary

        Returns:
            string
        """
        return '{} is not required'.format(key)


class timezone(BaseValidation):

    def passes(self, attribute, key, dictionary):
        import pytz
        return attribute in pytz.all_timezones

    def message(self, attribute):
        return '{} must be a valid timezone'.format(attribute)

    def negated_message(self, attribute):
        return '{} must not be a valid timezone'.format(attribute)


class accepted(BaseValidation):

    def passes(self, attribute, key, dictionary):
        return attribute is True or attribute == 'on' or attribute == 'yes' or attribute == '1' or attribute == 1
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
except AttributeError:
            JsonParseException = ValueError

        try:
            return json_module.loads(str(attribute))
        except (TypeError, JsonParseException):
            return False

    def message(self, attribute):
        return '{} must be json'.format(attribute)

    def negated_message(self, attribute):
        return '{} must not be json'.format(attribute)


class phone(BaseValidation):

    def __init__(self, *rules, pattern="123-456-7890", messages={}, raises={}):
        super().__init__(rules, messages={}, raises={})
        # 123-456-7890
        # (123)456-7890
        self.pattern = pattern

    def passes(self, attribute, key, dictionary):
        if self.pattern == '(123)456-7890':
            return re.compile(r"^\(\w{3}\)\w{3}\-\w{4}$").match(attribute)
        elif self.pattern == '123-456-7890':
            return re.compile(r"^\w{3}\-\w{3}\-\w{4}$").match(attribute)

    def message(self, attribute):
        if self.pattern == '(123)456-7890':
            return '{} must be in the format (XXX)XXX-XXXX'.format(attribute)
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
class timezone(BaseValidation):

    def passes(self, attribute, key, dictionary):
        import pytz
        return attribute in pytz.all_timezones

    def message(self, attribute):
        return '{} must be a valid timezone'.format(attribute)

    def negated_message(self, attribute):
        return '{} must not be a valid timezone'.format(attribute)


class accepted(BaseValidation):

    def passes(self, attribute, key, dictionary):
        return attribute is True or attribute == 'on' or attribute == 'yes' or attribute == '1' or attribute == 1

    def message(self, attribute):
        return '{} must be yes, on, 1 or true'.format(attribute)

    def negated_message(self, attribute):
        return '{} must not be yes, on, 1 or true'.format(attribute)


class ip(BaseValidation):

    def passes(self, attribute, key, dictionary):
        import socket
github MasoniteFramework / masonite / masonite / validation / Validator.py View on Github external
self.error(key, self.negated_message(key))
                    else:
                        self.error(key, self.message(key))

                continue
            if not self.passes(self.find(key, dictionary), key, dictionary):
                boolean = False
                self.error(key, self.message(key))

            if self.errors and self.raises:
                return self.raise_exception(key)

        return boolean


class required(BaseValidation):

    def passes(self, attribute, key, dictionary):
        """The passing criteria for this rule.

        This should return a True boolean value.

        Arguments:
            attribute {mixed} -- The value found within the dictionary
            key {string} -- The key in the dictionary being searched for.
                            This key may or may not exist in the dictionary.
            dictionary {dict} -- The dictionary being searched

        Returns:
            bool
        """
        return attribute