Commit d288911a authored by Xavier Thompson's avatar Xavier Thompson

slapconfiguration:jsonschema: Validate defaults

parent c7db2923
......@@ -335,18 +335,22 @@ class DefaultValidator(object):
for error in self.validator.iter_errors(instance):
invalid = True
yield error
# Apply collected defaults - only to valid instances
# Stop there in case of validation errors
if invalid:
return
for instance, defaults in self.defaults.values():
# Apply collected defaults
for data, defaults in self.defaults.values():
for key, defaultdict in defaults.items():
if key not in instance:
if key not in data:
it = iter(defaultdict.values())
default = next(it)
if any(d != default for d in it):
raise UserError(
"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):
......
......@@ -54,7 +54,7 @@ class SlapConfigurationTest(unittest.TestCase):
self.assertEqual(options['address-list'], [10, 20],
"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')
software_schema = {
"name": "Test",
......@@ -82,7 +82,7 @@ class SlapConfigurationTest(unittest.TestCase):
"letter": {
"type": "string",
"enum": ["a", "b", "c"],
"default": "a"
"default": "a" if valid_defaults else 1,
},
"number": {
"type": "integer",
......@@ -136,7 +136,7 @@ class SlapConfigurationTest(unittest.TestCase):
},
"thing": {
"type": "string",
"default": "hello",
"default": "hello" if valid_defaults else 1,
},
},
"required": ["kind"],
......@@ -153,7 +153,7 @@ class SlapConfigurationTest(unittest.TestCase):
},
"thing": {
"type": "integer",
"default": 42,
"default": 42 if valid_defaults else "forty-two",
},
"required": ["kind"],
}
......@@ -286,6 +286,15 @@ class SlapConfigurationTest(unittest.TestCase):
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):
self.writeJsonSchema()
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