Support forbidden chars in strings.
This commit is contained in:
parent
b74a97580f
commit
f4a59b2d71
@ -156,14 +156,19 @@ class String(BaseType):
|
|||||||
|
|
||||||
typestr = 'string'
|
typestr = 'string'
|
||||||
|
|
||||||
def __init__(self, minlen=None, maxlen=None):
|
def __init__(self, minlen=None, maxlen=None, forbidden=None):
|
||||||
self.minlen = minlen
|
self.minlen = minlen
|
||||||
self.maxlen = maxlen
|
self.maxlen = maxlen
|
||||||
|
self.forbidden = forbidden
|
||||||
|
|
||||||
def transform(self, value):
|
def transform(self, value):
|
||||||
return value.lower()
|
return value.lower()
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
|
if self.forbidden is not None and any(c in value
|
||||||
|
for c in self.forbidden):
|
||||||
|
raise ValidationError(value, "may not contain the chars "
|
||||||
|
"\"{}\"".format(self.forbidden))
|
||||||
if self.minlen is not None and len(value) < self.minlen:
|
if self.minlen is not None and len(value) < self.minlen:
|
||||||
raise ValidationError(value, "must be at least {} chars "
|
raise ValidationError(value, "must be at least {} chars "
|
||||||
"long!".format(self.minlen))
|
"long!".format(self.minlen))
|
||||||
|
Loading…
Reference in New Issue
Block a user