PaferaPy Async 0.1
ASGI framework focused on simplicity and efficiency
Loading...
Searching...
No Matches
validators.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#
4# This file contains validator objects designed to be called as
5# functions in order to validate values. They are useful for
6# simple and quick validation schemes where you don't need
7# advanced handling.
8#
9# To use them, simply create an instance of a class and then call it
10# as in
11#
12# validator = RangeValidator(0, 100)
13# validator(200)
14#
15# The validator will throw an exception if the value is not correct.
16
17import dateutil.parser
18
19# *********************************************************************
20class BaseValidator(object):
21 """Base class that doesn't do anything yet, but is a convenient
22 place to add any functionality that is common to all validators.
23 """
24 pass
25
26# *********************************************************************
28 """Automatically passes all values.
29 """
30
31 def __call__(self, fieldname, value):
32 pass
33
34# *********************************************************************
35class NullValidator(BaseValidator):
36 """Throws an exception on NULL values.
37 """
38 def __call__(self, fieldname, value):
39 if value == None:
40 raise Exception(f'{fieldname} cannot be null!')
41
42# *********************************************************************
44 """Throws an exception on blank values.
45 """
46 def __call__(self, fieldname, value):
47 if not value:
48 raise Exception(f'{fieldname} cannot be blank!')
49
50# *********************************************************************
52 """Throws an exception on invalid email addresses.
53
54 Note that this is a simple regex, so it won't validate more advanced
55 email addresses.
56 """
57 def __call__(self, fieldname, value):
58 if not re.match(r"/[a-z0-9!#%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9] (?:[a-z0-9-]*[a-z0-9])?/i", value):
59 raise Exception(f'{value} is not a valid email address!')
60
61# *********************************************************************
63 """Throws an exception on invalid date strings.
64
65 Note that this simpy uses dateutil's parse(), so it won't
66 validate more advanced dates.
67 """
68 def __call__(self, fieldname, value):
69 try:
70 dateutil.parser.parse(value)
71 except:
72 raise Exception(f'{value} is not a valid date!')
73
74# *********************************************************************
76 """Throws an exception on invalid time strings.
77
78 Note that this simpy uses dateutil's parse(), so it won't
79 validate more advanced dates.
80 """
81 def __call__(self, fieldname, value):
82 try:
83 dateutil.parser.parse(value)
84 except:
85 raise Exception(f'{value} is not a valid time!')
86
87# *********************************************************************
89 """Throws an exception on invalid datetime strings.
90
91 Note that this simpy uses dateutil's parse(), so it won't
92 validate more advanced dates.
93 """
94 def __call__(self, fieldname, value):
95 try:
96 dateutil.parser.parse(value)
97 except:
98 raise Exception(f'{value} is not a valid date!')
99
100# *********************************************************************
102 """Throws an exception if the value is too high or too low.
103 """
104
105 # -------------------------------------------------------------------
106 def __init__(self, min, max):
107 self.min = min
108 self.max = max
109
110 # -------------------------------------------------------------------
111 def __call__(self, fieldname, value):
112 if value < self.min:
113 raise Exception(f'{value} is too low for {fieldname}!')
114
115 if value > self.max:
116 raise Exception(f'{value} is too high for {fieldname}!')
Base class that doesn't do anything yet, but is a convenient place to add any functionality that is c...
Definition: validators.py:20
Throws an exception on blank values.
Definition: validators.py:43
def __call__(self, fieldname, value)
Definition: validators.py:46
Throws an exception on invalid datetime strings.
Definition: validators.py:88
def __call__(self, fieldname, value)
Definition: validators.py:94
Throws an exception on invalid date strings.
Definition: validators.py:62
def __call__(self, fieldname, value)
Definition: validators.py:68
Throws an exception on invalid email addresses.
Definition: validators.py:51
def __call__(self, fieldname, value)
Definition: validators.py:57
Automatically passes all values.
Definition: validators.py:27
def __call__(self, fieldname, value)
Definition: validators.py:31
def __call__(self, fieldname, value)
Definition: validators.py:38
Throws an exception if the value is too high or too low.
Definition: validators.py:101
def __call__(self, fieldname, value)
Definition: validators.py:111
def __init__(self, min, max)
Definition: validators.py:106
Throws an exception on invalid time strings.
Definition: validators.py:75
def __call__(self, fieldname, value)
Definition: validators.py:81