Merge "Use StringType from WSME"
This commit is contained in:
commit
f843cdaaaa
@ -16,9 +16,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import re
|
|
||||||
import six
|
|
||||||
|
|
||||||
import wsme
|
import wsme
|
||||||
from wsme import types as wtypes
|
from wsme import types as wtypes
|
||||||
|
|
||||||
@ -69,57 +66,11 @@ macaddress = MacAddressType()
|
|||||||
uuid = UuidType()
|
uuid = UuidType()
|
||||||
|
|
||||||
|
|
||||||
# TODO(lucasagomes): WSME already has this StringType implementation on trunk,
|
|
||||||
# so remove it on the next WSME release (> 0.5b6)
|
|
||||||
class StringType(wtypes.UserType):
|
|
||||||
"""A simple string type. Can validate a length and a pattern.
|
|
||||||
|
|
||||||
:param min_length: Possible minimum length
|
|
||||||
:param max_length: Possible maximum length
|
|
||||||
:param pattern: Possible string pattern
|
|
||||||
|
|
||||||
Example::
|
|
||||||
|
|
||||||
Name = StringType(min_length=1, pattern='^[a-zA-Z ]*$')
|
|
||||||
|
|
||||||
"""
|
|
||||||
basetype = six.string_types
|
|
||||||
name = "string"
|
|
||||||
|
|
||||||
def __init__(self, min_length=None, max_length=None, pattern=None):
|
|
||||||
self.min_length = min_length
|
|
||||||
self.max_length = max_length
|
|
||||||
if isinstance(pattern, six.string_types):
|
|
||||||
self.pattern = re.compile(pattern)
|
|
||||||
else:
|
|
||||||
self.pattern = pattern
|
|
||||||
|
|
||||||
def validate(self, value):
|
|
||||||
if not isinstance(value, self.basetype):
|
|
||||||
error = 'Value should be string'
|
|
||||||
raise ValueError(error)
|
|
||||||
|
|
||||||
if self.min_length is not None and len(value) < self.min_length:
|
|
||||||
error = 'Value should have a minimum character requirement of %s' \
|
|
||||||
% self.min_length
|
|
||||||
raise ValueError(error)
|
|
||||||
|
|
||||||
if self.max_length is not None and len(value) > self.max_length:
|
|
||||||
error = 'Value should have a maximum character requirement of %s' \
|
|
||||||
% self.max_length
|
|
||||||
raise ValueError(error)
|
|
||||||
|
|
||||||
if self.pattern is not None and not self.pattern.match(value):
|
|
||||||
error = 'Value should match the pattern %s' % self.pattern.pattern
|
|
||||||
raise ValueError(error)
|
|
||||||
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
class JsonPatchType(wtypes.Base):
|
class JsonPatchType(wtypes.Base):
|
||||||
"""A complex type that represents a single json-patch operation."""
|
"""A complex type that represents a single json-patch operation."""
|
||||||
|
|
||||||
path = wtypes.wsattr(StringType(pattern='^(/[\w-]+)+$'), mandatory=True)
|
path = wtypes.wsattr(wtypes.StringType(pattern='^(/[\w-]+)+$'),
|
||||||
|
mandatory=True)
|
||||||
op = wtypes.wsattr(wtypes.Enum(str, 'add', 'replace', 'remove'),
|
op = wtypes.wsattr(wtypes.Enum(str, 'add', 'replace', 'remove'),
|
||||||
mandatory=True)
|
mandatory=True)
|
||||||
value = wtypes.text
|
value = wtypes.text
|
||||||
|
@ -16,8 +16,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
import six
|
import six
|
||||||
import webtest
|
import webtest
|
||||||
@ -55,35 +53,6 @@ class TestUuidType(base.FunctionalTest):
|
|||||||
types.UuidType.validate, 'invalid-uuid')
|
types.UuidType.validate, 'invalid-uuid')
|
||||||
|
|
||||||
|
|
||||||
# TODO(lucasagomes): The tests for the StringType class were ported from
|
|
||||||
# WSME trunk remove it on the next WSME release (> 0.5b6)
|
|
||||||
class TestStringType(base.FunctionalTest):
|
|
||||||
|
|
||||||
def test_validate_string_type(self):
|
|
||||||
v = types.StringType(min_length=1, max_length=10,
|
|
||||||
pattern='^[a-zA-Z0-9]*$')
|
|
||||||
v.validate('1')
|
|
||||||
v.validate('12345')
|
|
||||||
v.validate('1234567890')
|
|
||||||
self.assertRaises(ValueError, v.validate, '')
|
|
||||||
self.assertRaises(ValueError, v.validate, '12345678901')
|
|
||||||
|
|
||||||
# Test a pattern validation
|
|
||||||
v.validate('a')
|
|
||||||
v.validate('A')
|
|
||||||
self.assertRaises(ValueError, v.validate, '_')
|
|
||||||
|
|
||||||
def test_validate_string_type_precompile(self):
|
|
||||||
precompile = re.compile('^[a-zA-Z0-9]*$')
|
|
||||||
v = types.StringType(min_length=1, max_length=10,
|
|
||||||
pattern=precompile)
|
|
||||||
|
|
||||||
# Test a pattern validation
|
|
||||||
v.validate('a')
|
|
||||||
v.validate('A')
|
|
||||||
self.assertRaises(ValueError, v.validate, '_')
|
|
||||||
|
|
||||||
|
|
||||||
class MyPatchType(types.JsonPatchType):
|
class MyPatchType(types.JsonPatchType):
|
||||||
"""Helper class for TestJsonPatchType tests."""
|
"""Helper class for TestJsonPatchType tests."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user