Skip to content

Commit 8d51e11

Browse files
committed
Modify attributes_validation CHECK function to allow 'ref.ref_list_filter.is_type' to accept either a string or an array of strings as a schema input
1 parent 3a4520a commit 8d51e11

5 files changed

Lines changed: 48 additions & 6 deletions

File tree

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python Debugger: Current File",
9+
"type": "debugpy",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal"
13+
}
14+
]
15+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ and return a result (for every entry) which will be returned by the service, che
431431
errors based on how the check is configured in the validation schema. New functions can be added easily, by
432432
adding ```@FunctionFactory.register``` to the class, giving it a name and extending the appropriate function type the
433433
function will be discovered automatically by the FunctionFactory. To use a function simply reference the function by
434-
it's name in the validation schema i.e ```"function": "not_null"```
434+
it's name in the validation schema i.e ```"function": "attributes_validation"```
435435

436436
#### Select
437437

esdlvalidator/validation/functions/check_attributes_not_null_or_valid.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ def get_function_definition(self):
7878
"type": "object",
7979
"required": ["is_type"],
8080
"properties": {
81-
"is_type": {"type": "string"},
81+
"is_type": {
82+
"anyOf": [
83+
{"type": "string"},
84+
{
85+
"type": "array",
86+
"items": {"type": "string"}
87+
}
88+
]
89+
},
8290
"match": {
8391
"type": "object",
8492
"description": "Optional attribute filters",
@@ -192,8 +200,17 @@ def _resolve_reference(self, entity: object, ref: dict, results: list):
192200
if entity_to_check is None:
193201
match_str = ", ".join(f"[{k}] = '{v}'" for k, v in ref["ref_list_filter"].get("match", {}).items())
194202
match_str = f" ({match_str})" if match_str else ""
203+
204+
ref_type = ref['ref_list_filter']['is_type']
205+
type_list = ref_type if isinstance(ref_type, list) else [ref_type]
206+
# Join with " or " for readability
207+
if len(type_list) == 1:
208+
type_str = type_list[0]
209+
else:
210+
type_str = " or ".join(type_list)
211+
195212
results.append(
196-
f"[{ref_path}] should contain a [{ref['ref_list_filter']['is_type']}]{match_str}, but not found."
213+
f"[{ref_path}] should contain a [{type_str}]{match_str}, but not found."
197214
)
198215
return entity_to_check
199216

esdlvalidator/validation/functions/check_reference_attributes_comparison.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def _resolve_reference(self, entity: object, ref: dict, results: list):
156156

157157
if isinstance(reference, EOrderedSet):
158158
if len(reference) > 1 and ref.get("ref_list_filter"):
159+
# NOTE: Only the first matching entity is returned.
159160
entity_to_check = utils.get_ref(reference, ref["ref_list_filter"])
160161
else:
161162
entity_to_check = reference[0]

esdlvalidator/validation/functions/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,21 @@ def get_ref(references: EOrderedSet, ref_filter: dict):
5454
Returns:
5555
The first matching entity, or None if no match is found.
5656
"""
57-
ref_type = ref_filter["is_type"]
57+
raw_types = ref_filter["is_type"]
5858
match_dict = ref_filter.get("match", {})
59-
esdl_type = esdlUtils.get_esdl_class_from_string(ref_type)[0]
59+
60+
if isinstance(raw_types, str):
61+
type_list = [raw_types]
62+
else:
63+
type_list = list(raw_types)
64+
65+
esdl_classes = []
66+
for t in type_list:
67+
esdl_cls = esdlUtils.get_esdl_class_from_string(t)[0]
68+
esdl_classes.append(esdl_cls)
6069

6170
for entity in references:
62-
if not isinstance(entity, esdl_type):
71+
if not any(isinstance(entity, cls) for cls in esdl_classes):
6372
continue
6473
if all(get_attr_or_ref_attr(entity, key) == value for key, value in match_dict.items()):
6574
return entity

0 commit comments

Comments
 (0)