Commit d288911a authored by Xavier Thompson's avatar Xavier Thompson

slapconfiguration:jsonschema: Validate defaults

parent c7db2923
...@@ -335,18 +335,22 @@ class DefaultValidator(object): ...@@ -335,18 +335,22 @@ class DefaultValidator(object):
for error in self.validator.iter_errors(instance): for error in self.validator.iter_errors(instance):
invalid = True invalid = True
yield error yield error
# Apply collected defaults - only to valid instances # Stop there in case of validation errors
if invalid: if invalid:
return return
for instance, defaults in self.defaults.values(): # Apply collected defaults
for data, defaults in self.defaults.values():
for key, defaultdict in defaults.items(): for key, defaultdict in defaults.items():
if key not in instance: if key not in data:
it = iter(defaultdict.values()) it = iter(defaultdict.values())
default = next(it) default = next(it)
if any(d != default for d in it): if any(d != default for d in it):
raise UserError( raise UserError(
"Conflicting defaults for key %s: %r" % (key, defaultlist)) "Conflicting defaults for key %s: %r" % (key, defaultlist))
instance[key] = default data[key] = default
# Validate the updated instance
for error in self.validatorfor(self.schema).iter_errors(instance):
yield error
class JsonSchema(Recipe): class JsonSchema(Recipe):
......
...@@ -54,7 +54,7 @@ class SlapConfigurationTest(unittest.TestCase): ...@@ -54,7 +54,7 @@ class SlapConfigurationTest(unittest.TestCase):
self.assertEqual(options['address-list'], [10, 20], self.assertEqual(options['address-list'], [10, 20],
"All underscores should be replaced with -") "All underscores should be replaced with -")
def writeJsonSchema(self, serialisation='json-in-xml'): def writeJsonSchema(self, serialisation='json-in-xml', valid_defaults=True):
self.software_json_file = os.path.join(self.software_root, 'software.cfg.json') self.software_json_file = os.path.join(self.software_root, 'software.cfg.json')
software_schema = { software_schema = {
"name": "Test", "name": "Test",
...@@ -82,7 +82,7 @@ class SlapConfigurationTest(unittest.TestCase): ...@@ -82,7 +82,7 @@ class SlapConfigurationTest(unittest.TestCase):
"letter": { "letter": {
"type": "string", "type": "string",
"enum": ["a", "b", "c"], "enum": ["a", "b", "c"],
"default": "a" "default": "a" if valid_defaults else 1,
}, },
"number": { "number": {
"type": "integer", "type": "integer",
...@@ -136,7 +136,7 @@ class SlapConfigurationTest(unittest.TestCase): ...@@ -136,7 +136,7 @@ class SlapConfigurationTest(unittest.TestCase):
}, },
"thing": { "thing": {
"type": "string", "type": "string",
"default": "hello", "default": "hello" if valid_defaults else 1,
}, },
}, },
"required": ["kind"], "required": ["kind"],
...@@ -153,7 +153,7 @@ class SlapConfigurationTest(unittest.TestCase): ...@@ -153,7 +153,7 @@ class SlapConfigurationTest(unittest.TestCase):
}, },
"thing": { "thing": {
"type": "integer", "type": "integer",
"default": 42, "default": 42 if valid_defaults else "forty-two",
}, },
"required": ["kind"], "required": ["kind"],
} }
...@@ -286,6 +286,15 @@ class SlapConfigurationTest(unittest.TestCase): ...@@ -286,6 +286,15 @@ class SlapConfigurationTest(unittest.TestCase):
self.receiveParameters, self.receiveParameters,
) )
def test_jsonschema_json_in_xml_invalid_defaults_json_input(self):
self.writeJsonSchema(valid_defaults=False)
parameters = {"number": 1}
with self.patchSlap(parameters, True):
self.assertRaises(
slapconfiguration.UserError,
self.receiveParameters,
)
def test_jsonschema_shared_1_valid_defaults(self): def test_jsonschema_shared_1_valid_defaults(self):
self.writeJsonSchema() self.writeJsonSchema()
parameters = {"number": 1} parameters = {"number": 1}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment