The special type implemented for flavors

* it supports integers w/ and w/o quotes (1, "1") and uuids

Change-Id: I121ee96d54a6d55e66bde6cc04e455ae8af82c82
This commit is contained in:
Sergey Lukjanov 2013-06-27 14:02:13 +04:00
parent d390c48070
commit d04de5df89
3 changed files with 33 additions and 3 deletions

View File

@ -20,8 +20,7 @@ node_group_template_schema = {
"type": "string",
},
"flavor_id": {
"type": "string",
"format": "uuid",
'type': 'flavor',
},
"plugin_name": {
"type": "string",

View File

@ -225,3 +225,18 @@ class ApiValidatorTest(unittest2.TestCase):
],
}
})
def test_validate_flavor(self):
schema = {
'type': "flavor",
}
self._validate_success(schema, 0)
self._validate_success(schema, 1)
self._validate_success(schema, "0")
self._validate_success(schema, "1")
self._validate_success(schema, uuidutils.generate_uuid())
self._validate_failure(schema, True)
self._validate_failure(schema, 0.1)
self._validate_failure(schema, "0.1")
self._validate_failure(schema, "asd")

View File

@ -58,10 +58,26 @@ class ConfigsType(dict):
__metaclass__ = ConfigTypeMeta
class FlavorTypeMeta(type):
def __instancecheck__(cls, instance):
try:
int(instance)
except (ValueError, TypeError):
return (isinstance(instance, six.string_types)
and uuidutils.is_uuid_like(instance))
return (isinstance(instance, six.integer_types + six.string_types)
and type(instance) != bool)
class FlavorType(object):
__metaclass__ = FlavorTypeMeta
class ApiValidator(jsonschema.Draft4Validator):
def __init__(self, schema):
format_checker = jsonschema.FormatChecker()
super(ApiValidator, self).__init__(
schema, format_checker=format_checker, types={
"configs": ConfigsType
"configs": ConfigsType,
"flavor": FlavorType,
})