]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Make original schema available everywhere (prepare to resolve $ref).
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Fri, 29 Oct 2021 07:22:47 +0000 (09:22 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Fri, 29 Oct 2021 07:22:47 +0000 (09:22 +0200)
wrpylib/json_tools.py

index 8490b5947e9569f74f20e4292e4398b4b37c2450..fc6223f1ec0d46aac5cd356809c4d6ffb7fe61b0 100644 (file)
@@ -12,19 +12,20 @@ def _fmt_path(path: List) -> str:
     return f'schema[{"][".join(map(repr, path))}]'
 
 
-def _order_json_keys_string(sub_value: JsonTypes, sub_schema: JsonTypes, path: List) -> str:
+def _order_json_keys_string(sub_value: JsonTypes, sub_schema: JsonTypes, schema: JsonTypes, path: List) -> str:
     if not isinstance(sub_value, str):
         raise ValidationError(f'Type of {_fmt_path(path)} needs to be string (Python str).')
     return sub_value
 
 
-def _order_json_keys_number(sub_value: JsonTypes, sub_schema: JsonTypes, path: List) -> Union[int, float]:
+def _order_json_keys_number(sub_value: JsonTypes, sub_schema: JsonTypes,
+                            schema: JsonTypes, path: List) -> Union[int, float]:
     if not isinstance(sub_value, (int, float)) or isinstance(sub_value, bool):
         raise ValidationError(f'Type of {_fmt_path(path)} needs to be number (Python int or float).')
     return sub_value
 
 
-def _order_json_keys_object(sub_value: JsonTypes, sub_schema: JsonTypes, path: List) -> Dict:
+def _order_json_keys_object(sub_value: JsonTypes, sub_schema: JsonTypes, schema: JsonTypes, path: List) -> Dict:
     if not isinstance(sub_value, dict):
         raise ValidationError(f'Type of {_fmt_path(path)} needs to be object (Python dict).')
     v = sub_value.copy()
@@ -32,7 +33,7 @@ def _order_json_keys_object(sub_value: JsonTypes, sub_schema: JsonTypes, path: L
     result = {}
     for key in p:
         if key in v:
-            result[key] = _order_json_keys(v.pop(key), p[key], path + [key])
+            result[key] = _order_json_keys(v.pop(key), p[key], schema, path + [key])
         else:
             if key in sub_schema.get('required', []):
                 raise ValidationError(f'Required key "{key}" not present ({_fmt_path(path)}).')
@@ -45,26 +46,26 @@ def _order_json_keys_object(sub_value: JsonTypes, sub_schema: JsonTypes, path: L
     return result
 
 
-def _order_json_keys_array(sub_value: JsonTypes, sub_schema: JsonTypes, path: List) -> List:
+def _order_json_keys_array(sub_value: JsonTypes, sub_schema: JsonTypes, schema: JsonTypes, path: List) -> List:
     if not isinstance(sub_value, list):
         raise ValidationError(f'Type of {"".join(_fmt_path(path))} needs to be array (Python list).')
     s = sub_schema.get('items', True)
-    return [_order_json_keys(v, s, path + [i]) for i, v in enumerate(sub_value)]
+    return [_order_json_keys(v, s, schema, path + [i]) for i, v in enumerate(sub_value)]
 
 
-def _order_json_keys_boolean(sub_value: JsonTypes, sub_schema: JsonTypes, path: List) -> bool:
+def _order_json_keys_boolean(sub_value: JsonTypes, sub_schema: JsonTypes, schema: JsonTypes, path: List) -> bool:
     if not isinstance(sub_value, bool):
         raise ValidationError(f'Type of {_fmt_path(path)} needs to be boolean (Python bool).')
     return sub_value
 
 
-def _order_json_keys_null(sub_value: JsonTypes, sub_schema: JsonTypes, path: List) -> None:
+def _order_json_keys_null(sub_value: JsonTypes, sub_schema: JsonTypes, schema: JsonTypes, path: List) -> None:
     if sub_value is not None:
         raise ValidationError(f'Type of {_fmt_path(path)} needs to be null (Python None).')
     return sub_value
 
 
-def _order_json_keys(sub_value: JsonTypes, sub_schema: JsonTypes, path: List) -> JsonTypes:
+def _order_json_keys(sub_value: JsonTypes, sub_schema: JsonTypes, schema: JsonTypes, path: List) -> JsonTypes:
     if isinstance(sub_schema, bool):
         if sub_schema:
             return sub_value
@@ -76,8 +77,8 @@ def _order_json_keys(sub_value: JsonTypes, sub_schema: JsonTypes, path: List) ->
         'array': _order_json_keys_array,
         'boolean': _order_json_keys_boolean,
         'null': _order_json_keys_null,
-    }[sub_schema['type']](sub_value, sub_schema, path)
+    }[sub_schema['type']](sub_value, sub_schema, schema, path)
 
 
 def order_json_keys(value: JsonTypes, schema: JsonTypes) -> JsonTypes:
-    return _order_json_keys(value, schema, [])
+    return _order_json_keys(value, schema, schema, [])