Use jsonschema to validate efficacy indicators

This patch replaces voluptuous with JSON-schema to validate
efficacy indicator since in watcher we want to remove voluptuous
and use JSONSchema as our only JSON validation tool to keep consistency.

Change-Id: Iaa77566f1cdfdac03ce8e7d5a75406274c7d5298
Implements: blueprint replace-voplutuous-with-jsonschema
This commit is contained in:
Yumeng_Bao
2018-04-13 17:53:06 +08:00
committed by Hidekazu Nakamura
parent 831e58df10
commit 0540cd22d6
5 changed files with 75 additions and 43 deletions

View File

@@ -24,10 +24,10 @@ calculating its :ref:`global efficacy <efficacy_definition>`.
"""
import abc
import jsonschema
from oslo_serialization import jsonutils
import six
import voluptuous
@six.add_metaclass(abc.ABCMeta)
@@ -65,17 +65,21 @@ class EfficacySpecification(object):
@property
def schema(self):
"""Combined schema from the schema of the indicators"""
schema = voluptuous.Schema({}, required=True)
schema = {
"type": "object",
"properties": {},
"required": []
}
for indicator in self.indicators_specs:
key_constraint = (voluptuous.Required
if indicator.required else voluptuous.Optional)
schema = schema.extend(
{key_constraint(indicator.name): indicator.schema.schema})
schema["properties"][indicator.name] = indicator.schema
schema["required"].append(indicator.name)
return schema
def validate_efficacy_indicators(self, indicators_map):
return self.schema(indicators_map)
if indicators_map:
jsonschema.validate(indicators_map, self.schema)
else:
True
def get_indicators_specs_dicts(self):
return [indicator.to_dict()