Merge "Add Regex string validator"
This commit is contained in:
commit
7780a20d0f
|
@ -14,6 +14,7 @@
|
|||
# under the License.
|
||||
|
||||
import abc
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from oslo_log import log as logging
|
||||
|
@ -105,6 +106,23 @@ class Version(Validator):
|
|||
'+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/')}
|
||||
|
||||
|
||||
class Regex(Validator):
|
||||
|
||||
def __init__(self, pattern):
|
||||
self.pattern = re.compile(pattern)
|
||||
|
||||
@staticmethod
|
||||
def get_allowed_types():
|
||||
return fields.StringField,
|
||||
|
||||
def __call__(self, value):
|
||||
if not self.pattern.match(value):
|
||||
raise ValueError
|
||||
|
||||
def to_jsonschema(self):
|
||||
return {'pattern': self.pattern.pattern}
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
class SizeValidator(Validator):
|
||||
|
||||
|
|
|
@ -1602,6 +1602,23 @@ class TestUpdate(base.TestArtifact):
|
|||
"value": 'c' * 11}]
|
||||
self.patch(url=url, data=patch, status=400)
|
||||
|
||||
# string_regex format it '^([0-9a-fA-F]){8}$'
|
||||
patch = [{"op": "replace", "path": "/string_regex",
|
||||
"value": 'INVALID'}]
|
||||
self.patch(url=url, data=patch, status=400)
|
||||
|
||||
patch = [{"op": "replace", "path": "/string_regex",
|
||||
"value": '167f808Z'}]
|
||||
self.patch(url=url, data=patch, status=400)
|
||||
|
||||
patch = [{"op": "replace", "path": "/string_regex",
|
||||
"value": '167f80835'}]
|
||||
self.patch(url=url, data=patch, status=400)
|
||||
|
||||
patch = [{"op": "replace", "path": "/string_regex",
|
||||
"value": '167f8083'}]
|
||||
self.patch(url=url, data=patch)
|
||||
|
||||
# test list has 3 elements maximum
|
||||
patch = [{"op": "add", "path": "/list_validators/-", "value": 'd'}]
|
||||
self.patch(url=url, data=patch, status=400)
|
||||
|
|
|
@ -440,6 +440,12 @@ fixtures = {
|
|||
u'required_on_activate': False,
|
||||
u'type': [u'string',
|
||||
u'null']},
|
||||
u'string_regex': {u'filter_ops': [u'eq', u'neq', u'in'],
|
||||
u'glareType': u'String',
|
||||
u'maxLength': 255,
|
||||
u'pattern': u'^([0-9a-fA-F]){8}$',
|
||||
u'required_on_activate': False,
|
||||
u'type': [u'string', u'null']},
|
||||
u'string_required': {
|
||||
u'filter_ops': [u'eq',
|
||||
u'neq',
|
||||
|
|
|
@ -94,6 +94,10 @@ class SampleArtifact(base_artifact.BaseArtifact):
|
|||
'string_mutable': Field(fields.StringField,
|
||||
required_on_activate=False,
|
||||
mutable=True),
|
||||
'string_regex': Field(fields.StringField,
|
||||
required_on_activate=False,
|
||||
validators=[
|
||||
validators.Regex('^([0-9a-fA-F]){8}$')]),
|
||||
'string_required': Field(fields.StringField,
|
||||
required_on_activate=True),
|
||||
'string_validators': Field(fields.StringField,
|
||||
|
|
|
@ -42,6 +42,26 @@ class TestValidators(base.BaseTestArtifactAPI):
|
|||
'{4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$')},
|
||||
validator.to_jsonschema())
|
||||
|
||||
def test_regex(self):
|
||||
# test regex '^([0-9a-fA-F]){8}$'
|
||||
validator = validators.Regex('^([0-9a-fA-F]){8}$')
|
||||
|
||||
# valid string - no exception
|
||||
validator('167f8083')
|
||||
|
||||
# invalid string - ValueError
|
||||
self.assertRaises(ValueError, validator, 'INVALID')
|
||||
self.assertRaises(ValueError, validator, '167f808Z')
|
||||
self.assertRaises(ValueError, validator, '167f80835')
|
||||
|
||||
# only strings can be applied as values
|
||||
self.assertEqual((fields.StringField,),
|
||||
validators.UUID.get_allowed_types())
|
||||
|
||||
self.assertEqual(
|
||||
{'pattern': '^([0-9a-fA-F]){8}$'},
|
||||
validator.to_jsonschema())
|
||||
|
||||
def test_allowed_values(self):
|
||||
# test that field may have preoccupied values
|
||||
validator_s = validators.AllowedValues(['aaa', 'bbb'])
|
||||
|
|
Loading…
Reference in New Issue