##// END OF EJS Templates
validation-schema: added StringBoolean type and IPAddr validator.
marcink -
r1149:94d0095b default
parent child Browse files
Show More
@@ -36,6 +36,38 b' class GroupNameType(colander.String):'
36 return self.SEPARATOR.join(path)
36 return self.SEPARATOR.join(path)
37
37
38
38
39 class StringBooleanType(colander.String):
40 true_values = ['true', 't', 'yes', 'y', 'on', '1']
41 false_values = ['false', 'f', 'no', 'n', 'off', '0']
42
43 def serialize(self, node, appstruct):
44 if appstruct is colander.null:
45 return colander.null
46 if not isinstance(appstruct, bool):
47 raise colander.Invalid(node, '%r is not a boolean' % appstruct)
48
49 return appstruct and 'true' or 'false'
50
51 def deserialize(self, node, cstruct):
52 if cstruct is colander.null:
53 return colander.null
54
55 if isinstance(cstruct, bool):
56 return cstruct
57
58 if not isinstance(cstruct, basestring):
59 raise colander.Invalid(node, '%r is not a string' % cstruct)
60
61 value = cstruct.lower()
62 if value in self.true_values:
63 return True
64 elif value in self.false_values:
65 return False
66 else:
67 raise colander.Invalid(
68 node, '{} value cannot be translated to bool'.format(value))
69
70
39 class UserOrUserGroupType(colander.SchemaType):
71 class UserOrUserGroupType(colander.SchemaType):
40 """ colander Schema type for valid rhodecode user and/or usergroup """
72 """ colander Schema type for valid rhodecode user and/or usergroup """
41 scopes = ('user', 'usergroup')
73 scopes = ('user', 'usergroup')
@@ -17,6 +17,19 b' def ip_addr_validator(node, value):'
17 raise colander.Invalid(node, msg)
17 raise colander.Invalid(node, msg)
18
18
19
19
20 class IpAddrValidator(object):
21 def __init__(self, strict=True):
22 self.strict = strict
23
24 def __call__(self, node, value):
25 try:
26 # this raises an ValueError if address is not IpV4 or IpV6
27 ipaddress.ip_network(value, strict=self.strict)
28 except ValueError:
29 msg = _(u'Please enter a valid IPv4 or IpV6 address')
30 raise colander.Invalid(node, msg)
31
32
20 def glob_validator(node, value):
33 def glob_validator(node, value):
21 try:
34 try:
22 re.compile('^' + glob2re(value) + '$')
35 re.compile('^' + glob2re(value) + '$')
General Comments 0
You need to be logged in to leave comments. Login now