From a90b5ac7c747fead9cf2e794d70f4724187b82f0 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Wed, 7 Nov 2018 17:00:22 -0700 Subject: [PATCH] Allow specifying a schema for unknown JObj keys Signed-off-by: Zack Cerza --- qa/tasks/mgr/dashboard/helper.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/qa/tasks/mgr/dashboard/helper.py b/qa/tasks/mgr/dashboard/helper.py index 656fcec346c..9b8444e62cc 100644 --- a/qa/tasks/mgr/dashboard/helper.py +++ b/qa/tasks/mgr/dashboard/helper.py @@ -405,14 +405,16 @@ JList = namedtuple('JList', ['elem_typ']) JTuple = namedtuple('JList', ['elem_typs']) -class JObj(namedtuple('JObj', ['sub_elems', 'allow_unknown', 'none'])): - def __new__(cls, sub_elems, allow_unknown=False, none=False): +class JObj(namedtuple('JObj', ['sub_elems', 'allow_unknown', 'none', 'unknown_schema'])): + def __new__(cls, sub_elems, allow_unknown=False, none=False, unknown_schema=None): """ :type sub_elems: dict[str, JAny | JLeaf | JList | JObj | type] :type allow_unknown: bool + :type none: bool + :type unknown_schema: int, str, JAny | JLeaf | JList | JObj :return: """ - return super(JObj, cls).__new__(cls, sub_elems, allow_unknown, none) + return super(JObj, cls).__new__(cls, sub_elems, allow_unknown, none, unknown_schema) JAny = namedtuple('JAny', ['none']) @@ -462,10 +464,16 @@ def _validate_json(val, schema, path=[]): unknown_keys = set(val.keys()).difference(set(schema.sub_elems.keys())) if not schema.allow_unknown and unknown_keys: raise _ValError('unknown keys: {}'.format(unknown_keys), path) - return all( - _validate_json(val[sub_elem_name], sub_elem, path + [sub_elem_name]) - for sub_elem_name, sub_elem in schema.sub_elems.items() + result = all( + _validate_json(val[key], sub_schema, path + [key]) + for key, sub_schema in schema.sub_elems.items() ) + if unknown_keys and schema.allow_unknown and schema.unknown_schema: + result += all( + _validate_json(val[key], schema.unknown_schema, path + [key]) + for key in unknown_keys + ) + return result if schema in [str, int, float, bool, six.string_types]: return _validate_json(val, JLeaf(schema), path) -- 2.39.5