@@ -20,40 +20,78 @@ def _filter_tree(info, filters):
20
20
return len (info .children ) > 0 or all (f (info .node , info .identifier ) for f in filters )
21
21
22
22
23
- def _get_matching_schema_property (schema , key ):
23
+ def _get_matching_schema_property (schema , property_name ):
24
+ """
25
+ Extract a property subschema for a given property_name.
26
+ This function does not descend into the schema (beyond
27
+ looking for a "properties" key) and does not support
28
+ schema combiners.
29
+
30
+ Parameters
31
+ ----------
32
+ schema : dict
33
+ A dictionary containing a JSONSCHEMA
34
+ property_name : str
35
+ The name of the property to extract
36
+
37
+ Returns
38
+ -------
39
+ dict or None
40
+ The property subschema at the provided name or
41
+ ``None`` if the property doesn't exist.
42
+ """
24
43
if "properties" in schema :
25
44
props = schema ["properties" ]
26
- if key in props :
27
- return props [key ]
45
+ if property_name in props :
46
+ return props [property_name ]
28
47
if "patternProperties" in props :
29
48
patterns = props ["patternProperties" ]
30
49
for regex in patterns :
31
- if re .search (regex , key ):
50
+ if re .search (regex , property_name ):
32
51
return patterns [regex ]
33
52
return None
34
53
35
54
36
- def _get_subschema_for_property (schema , key ):
55
+ def _get_subschema_for_property (schema , property_name ):
56
+ """
57
+ Extract a property subschema for a given property_name.
58
+ This function will attempt to consider schema combiners
59
+ and will return None on an ambiguous result.
60
+
61
+ Parameters
62
+ ----------
63
+ schema : dict
64
+ A dictionary containing a JSONSCHEMA
65
+ property_name : str
66
+ The name of the property to extract
67
+
68
+ Returns
69
+ -------
70
+ dict or None
71
+ The property subschema at the provided name or
72
+ ``None`` if the property doesn't exist or is
73
+ ambiguous (has more than one subschema or is nested in a not).
74
+ """
37
75
# This does NOT handle $ref the expectation is that the schema
38
76
# is loaded with resolve_references=True
39
77
applicable = []
40
78
41
79
# first check properties and patternProperties
42
- subschema = _get_matching_schema_property (schema , key )
80
+ subschema = _get_matching_schema_property (schema , property_name )
43
81
if subschema is not None :
44
82
applicable .append (subschema )
45
83
46
84
# next handle schema combiners
47
85
if "not" in schema :
48
- subschema = _get_subschema_for_property (schema ["not" ], key )
86
+ subschema = _get_subschema_for_property (schema ["not" ], property_name )
49
87
if subschema is not None :
50
88
# We can't resolve a valid subschema under a "not" since
51
89
# we'd have to know how to invert a schema
52
90
return None
53
91
54
92
for combiner in ("allOf" , "oneOf" , "anyOf" ):
55
93
for combined_schema in schema .get (combiner , []):
56
- subschema = _get_subschema_for_property (combined_schema , key )
94
+ subschema = _get_subschema_for_property (combined_schema , property_name )
57
95
if subschema is not None :
58
96
applicable .append (subschema )
59
97
@@ -64,6 +102,25 @@ def _get_subschema_for_property(schema, key):
64
102
65
103
66
104
def _get_schema_key (schema , key ):
105
+ """
106
+ Extract a subschema at a given key.
107
+ This function will attempt to consider schema combiners
108
+ (allOf, oneOf, anyOf) and will return None on an
109
+ ambiguous result (where more than 1 match is found).
110
+
111
+ Parameters
112
+ ----------
113
+ schema : dict
114
+ A dictionary containing a JSONSCHEMA
115
+ key : str
116
+ The key under which the subschema is stored
117
+
118
+ Returns
119
+ -------
120
+ dict or None
121
+ The subschema at the provided key or
122
+ ``None`` if the key doesn't exist or is ambiguous.
123
+ """
67
124
applicable = []
68
125
if key in schema :
69
126
applicable .append (schema [key ])
0 commit comments