예제 #1
0
 def __init__(self, rules=None, request=None, custom_handlers=None):
     self._parser = Parser()
     self._translator = Translator()
     self._processor = RulesProcessor()
     self._rules = rules or {}
     self._custom_handlers = custom_handlers or {}
     self._request = self._parse_request(request or {})
예제 #2
0
class TestParser(unittest.TestCase):
    def setUp(self):
        self._parser = Parser()

    def test_parses_rules_correctly(self):
        rules = {
            'name': ['required', 'string', 'min:6'],
            'email': ['required', 'email', 'in:[email protected],[email protected]'],
            'password': ['required', 'min:8', 'confirmed']
        }
        self._parser.set_rules(rules)
        parsed_rules = self._parser.parsed_rules()
        self.assertDictEqual(
            parsed_rules, {
                'name': [{
                    'name': 'required',
                    'params': []
                }, {
                    'name': 'string',
                    'params': []
                }, {
                    'name': 'min',
                    'params': ['6']
                }],
                'email': [
                    {
                        'name': 'required',
                        'params': []
                    },
                    {
                        'name': 'email',
                        'params': []
                    },
                    {
                        'name': 'in',
                        'params': ['*****@*****.**', '*****@*****.**']
                    },
                ],
                'password': [
                    {
                        'name': 'required',
                        'params': []
                    },
                    {
                        'name': 'min',
                        'params': ['8']
                    },
                    {
                        'name': 'confirmed',
                        'params': []
                    },
                ]
            })
예제 #3
0
 def setUp(self):
     self.parser = Parser()
     self.processor = RulesProcessor()
     self.stream = open('tests/files/image.png')
     self.image_file = FileStorage(
         stream=self.stream,
         filename='tests/files/image.png',
         content_type='image/png',
         name='image'
     )
     self.invalid_file = FileStorage(
         stream=self.stream,
         filename='invalid.png'
     )
예제 #4
0
 def setUp(self):
     self.parser = Parser()
     self.processor = RulesProcessor()
     self.stream = open('tests/files/image.png', 'rb')
     self.image_file = FileStorage(
         stream=self.stream,
         filename='image.png',
         content_type='image/png',
         name='image'
     )
     self.invalid_file = FileStorage(
         stream=FakeFileStream(bytearray([0xFE, 0x58, 0x8F, 0x00, 0x08])),
         filename='invalid.png'
     )
예제 #5
0
class Validator:
    def __init__(self, rules=None, request=None, custom_handlers=None):
        self._parser = Parser()
        self._translator = Translator()
        self._processor = RulesProcessor()
        self._rules = rules or {}
        self._custom_handlers = custom_handlers or {}
        self._request = self._parse_request(request or {})

    def set_rules(self, rules):
        self._rules = rules

    def set_request(self, request):
        self._request = self._parse_request(request or {})

    def set_custom_messages(self, messages):
        self._translator.set_custom_messages(messages)

    def set_custom_handlers(self, handlers):
        for handler in handlers:
            self.register_rule_handler(**handler)

    def register_rule_handler(self, handler, message, params_count=0):
        if not handler.__name__.startswith('validate_'):
            raise ValueError(
                'Rule handlers must start with "validate_" name, %s provided' %
                (handler.__name__))
        self._processor.register_rule_handler(handler=handler,
                                              message=message,
                                              params_count=params_count)
        handler_messages = {}
        for handler_name, handler_dict in self._processor.custom_handlers(
        ).items():
            handler_messages[
                handler_name[len('validate_'):]] = handler_dict['message']
        self._translator.set_handler_messages(handler_messages)

    def fails(self):
        return not self.passes()

    def passes(self):
        self._parser.set_rules(self._rules)
        self._processor.set_rules(self._parser.parsed_rules())
        self._processor.set_request(self._request)
        return self._processor.passes()

    def messages(self):
        self._translator.set_validations(self._processor.validations())
        return self._translator.translated_errors()

    @staticmethod
    def _parse_request(request):
        if isinstance(request, dict):
            return request
        # instance of flask.request ...
        if request.is_json:
            return request.json
        dict_request = {}
        dict_request.update(request.form.to_dict(flat=True))
        dict_request.update(request.files.to_dict(flat=True))
        return dict_request
예제 #6
0
 def setUp(self):
     self._parser = Parser()