-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_validators.py
101 lines (85 loc) · 3.6 KB
/
custom_validators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright William Lees
#
# This source code, and any executable file compiled or derived from it, is governed by the European Union Public License v. 1.2,
# the English version of which is available here: https://perma.cc/DK5U-NDVE
#
# Custom validators for WTF
from wtforms import ValidationError
from collections import namedtuple
class ValidationResult:
def __init__(self, valid=True, tag=None, route=None, id=None):
self.valid = valid
self.tag = tag
self.route = route
self.id = id
class ValidOrcidID(object):
def __init__(self, message=None):
if not message:
message = 'ORCID Id must have the format NNNN-NNNN-NNNN-NNNN.'
self.message = message
def __call__(self, form, field):
try:
fields = field.data.split('-')
if len(fields) != 4:
raise ValidationError(self.message)
for f in fields:
if len(f) != 4:
raise ValidationError(self.message)
i = int(f)
except ValidationError as e:
raise ValidationError(e)
except:
raise ValidationError(self.message)
class ValidNucleotideSequence(object):
def __init__(self, gapped=False, rna=False, ambiguous=False, message=None, x=False, dot=False):
self.permitted = 'ACGU' if rna else 'ACGT'
self.permitted += 'RYSWKMBDHVN' if ambiguous else ''
self.permitted += 'X' if x else ''
self.permitted += '.' if dot else ''
self.permitted += '.-' if gapped else ''
if not message:
message = 'Sequence may only contain the characters %s (in either upper or lower case)' % self.permitted
self.message = message
def __call__(self, form, field):
if field.data is not None:
try:
for c in list(field.data):
if c.upper() not in self.permitted:
message = self.message
message = message + " ('%s' is not allowed)" % c
raise ValidationError(message)
except ValidationError as e:
raise ValidationError(e)
except:
raise ValidationError(self.message)
class ValidAASequence(object):
def __init__(self, gapped=False, message=None, x=False, dot = False):
self.permitted = 'ACDEFGHIKLMNPQRSTVWY'
self.permitted += 'X' if x else ''
self.permitted += '.' if dot else ''
self.permitted += '.-' if gapped else ''
if not message:
message = 'Sequence may only contain the characters %s (in either upper or lower case)' % self.permitted
self.message = message
def __call__(self, form, field):
if field.data is not None:
try:
for c in list(field.data.upper()):
if c not in self.permitted:
raise ValidationError(self.message)
except:
raise ValidationError(self.message)
# validators.DataRequired and InputRequired stop the validation chain if they fail
# This continues, so that if Optional() has been added to the chain, it takes precedence
# This is needed by EditableTable, which adds Optional to force validation
class NonEmpty(object):
def __init__(self, message=None):
if not message:
message = 'Field cannot be blank.'
self.message = message
def __call__(self, form, field):
if not field.raw_data or not field.raw_data[0]:
raise ValidationError(self.message)
class NullValidator(object):
def __call__(self, form, field):
return