Add TOO_SHORT / min_length to VLength

This commit is contained in:
Keith Mitchell
2014-10-14 13:28:02 -07:00
committed by Neil Williams
parent 63dd2f0c94
commit c3ef951879
3 changed files with 17 additions and 1 deletions

View File

@@ -364,9 +364,17 @@ class OAuth2AccessController(MinimalController):
@validate(
scope=nop("scope"),
device_id=VLength("device_id", 30),
device_id=VLength("device_id", 30, min_length=20),
)
def _access_token_extension_client_credentials(self, scope, device_id):
if ((errors.NO_TEXT, "device_id") in c.errors or
(errors.TOO_SHORT, "device_id") in c.errors or
(errors.TOO_LONG, "device_id") in c.errors):
return self.api_wrapper({
"error": "invalid_request",
"error_description": "bad device_id",
})
client = c.oauth2_client
if scope:
scope = OAuth2Scope(scope)

View File

@@ -101,6 +101,7 @@ error_list = dict((
('BAD_CARD', _('card problem: %(message)s')),
('TOO_LONG', _("this is too long (max: %(max_length)s)")),
('NO_TEXT', _('we need something here')),
('TOO_SHORT', _("this is too short (min: %(min_length)s)")),
('INVALID_CODE', _("we've never seen that code before")),
('CLAIMED_CODE', _("that code has already been claimed -- perhaps by you?")),
('NO_SELFS', _("that subreddit doesn't allow text posts")),

View File

@@ -549,12 +549,16 @@ class VLength(Validator):
only_whitespace = re.compile(r"\A\s*\Z", re.UNICODE)
def __init__(self, param, max_length,
min_length=0,
empty_error = errors.NO_TEXT,
length_error = errors.TOO_LONG,
short_error=errors.TOO_SHORT,
**kw):
Validator.__init__(self, param, **kw)
self.max_length = max_length
self.min_length = min_length
self.length_error = length_error
self.short_error = short_error
self.empty_error = empty_error
def run(self, text, text2 = ''):
@@ -563,6 +567,9 @@ class VLength(Validator):
self.set_error(self.empty_error, code=400)
elif len(text) > self.max_length:
self.set_error(self.length_error, {'max_length': self.max_length}, code=400)
elif len(text) < self.min_length:
self.set_error(self.short_error, {'min_length': self.min_length},
code=400)
else:
return text