Skip to content

Commit ff9cdaa

Browse files
committed
Extend attributes_not_null_or_valid CHECK function with in_range check
1 parent 636d7bb commit ff9cdaa

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

esdlvalidator/validation/functions/check_attributes_not_null_or_valid.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,18 @@ def get_function_definition(self):
116116
},
117117
"validCheckItem": {
118118
"type": "object",
119-
"required": ["attribute", "count_as_valid"],
119+
"required": ["attribute"],
120120
"properties": {
121121
"attribute": {"type": "string", "description": "Attribute name to validate"},
122122
"count_as_valid": {"anyOf": [{"type": "string"}, {"type": "array", "items": {"type": "string"}}]},
123+
"in_range": {
124+
"type": "object",
125+
"required": ["min", "max"],
126+
"properties": {
127+
"min": {"type": "number"},
128+
"max": {"type": "number"},
129+
}
130+
}
123131
},
124132
"additionalProperties": False,
125133
},
@@ -209,23 +217,43 @@ def _run_valid_checks(self, entity: object, checks: list):
209217
results = []
210218
for check in checks:
211219
attr = check["attribute"]
212-
valid_values = check["count_as_valid"]
213-
valid_list = valid_values if isinstance(valid_values, list) else [valid_values]
214-
215220
value = utils.get_attr_or_ref_attr(entity, attr)
216221

217222
if value == self.NOT_FOUND:
218223
raise ValueError(f"Attribute [{attr}] not found.")
224+
225+
# Valid values check
226+
valid_values = check.get("count_as_valid")
227+
if valid_values:
228+
valid_list = valid_values if isinstance(valid_values, list) else [valid_values]
219229

220-
if value == self.UNSET:
221-
if self.UNSET not in valid_list:
230+
if value == self.UNSET:
231+
if self.UNSET not in valid_list:
232+
results.append(
233+
f"[{attr}] should be {'one of ' if len(valid_list) > 1 else ''}[{', '.join(valid_list)}], but is unset."
234+
)
235+
else:
236+
value_str = getattr(value, "name", str(value)).lower()
237+
if value_str not in [v.lower() for v in valid_list]:
238+
results.append(
239+
f"[{attr}] should be {'one of ' if len(valid_list) > 1 else ''}[{', '.join(valid_list)}], but found [{value_str}]."
240+
)
241+
242+
# Range check
243+
in_range = check.get("in_range")
244+
if in_range:
245+
min = in_range["min"]
246+
max = in_range["max"]
247+
248+
if value == self.UNSET:
222249
results.append(
223-
f"[{attr}] should be {'one of ' if len(valid_list) > 1 else ''}[{', '.join(valid_list)}], but is unset."
250+
f"[{attr}] should be in range [{min}, {max}], but is unset."
224251
)
225-
else:
226-
value_str = getattr(value, "name", str(value)).lower()
227-
if value_str not in [v.lower() for v in valid_list]:
252+
break
253+
254+
if not (min <= value <= max):
228255
results.append(
229-
f"[{attr}] should be {'one of ' if len(valid_list) > 1 else ''}[{', '.join(valid_list)}], but found [{value_str}]."
256+
f"[{attr}] should be in range [{min}, {max}], but found [{value}]."
230257
)
258+
231259
return results

testdata/schemas/schema_NWN_optimizer.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,9 @@
363363
},
364364
{
365365
"name": "heatexchange_required_attributes_are_set",
366-
"description": "Report errors if the required attributes of HeatExchange assets are not set.",
366+
"description": "Report errors if the required attributes of HeatExchange assets are not set or incorrectly set.",
367367
"type": "error",
368-
"message": "Required attribute not set",
368+
"message": "Required attribute not set or invalid",
369369
"selects": [{
370370
"function": "get",
371371
"alias": "heatexchanges",
@@ -388,7 +388,15 @@
388388
"count_as_null": [0.0]
389389
}
390390
],
391-
"valid_checks": [],
391+
"valid_checks": [
392+
{
393+
"attribute": "efficiency",
394+
"in_range": {
395+
"min": 0.0,
396+
"max": 1.0
397+
}
398+
}
399+
],
392400
"resultMsgJSON": true
393401
}
394402
}

0 commit comments

Comments
 (0)