Skip to content

Commit 31f2adf

Browse files
committed
Allow repeated SLA properties
A contract can list the same SLA property on the same element, as the ODCS SLA example does with timeOfAvailability. A repeated SLA id still fails validation.
1 parent 20b1d84 commit 31f2adf

3 files changed

Lines changed: 17 additions & 22 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Added
1616
- `datacontract export polars`: export the schemas as Polars `pl.Schema` definitions (#1552)
1717
- Error messages link to the matching docs page: the server type's reference page when `datacontract test` fails, the format's guide when `import` or `export` fails, and the installation page when an optional dependency is missing
18-
- `datacontract lint` and `datacontract test` fails on duplicate schema, property, server, role and SLA property names, and on duplicate `id`s within the same array; checks are skipped with a custom `--json-schema`
18+
- `datacontract lint` and `datacontract test` fail on duplicate schema, property, server and role names, and on duplicate `id`s within the same array; checks are skipped with a custom `--json-schema`
1919

2020
## [1.2.9] - 2026-10-30
2121

‎datacontract/lint/constraints.py‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,34 @@ def visit(node, path):
6060

6161

6262
def duplicate_name_errors(contract: dict) -> list[DataContractException]:
63-
"""Require unique schema, property, server, role, support channel, team member and SLA entries, and unique ids.
63+
"""Require unique schema, property, server and role names, and unique ids.
6464
6565
JSON Schema cannot express uniqueness by a key. Invalid shapes are left to JSON Schema.
6666
"""
6767
errors = []
6868

69-
def check(entries, path, keys, label):
69+
def check(entries, path, key, label):
7070
seen = {}
7171
for index, entry in enumerate(entries if isinstance(entries, list) else []):
72-
if not isinstance(entry, dict):
72+
value = entry.get(key) if isinstance(entry, dict) else None
73+
if not isinstance(value, str):
7374
continue
74-
key = tuple(entry.get(k) for k in keys)
75-
if not isinstance(key[0], str) or not all(v is None or isinstance(v, str) for v in key):
76-
continue
77-
if key in seen:
78-
value = ", ".join(repr(v) for v in key if v is not None)
75+
if value in seen:
7976
errors.append(
8077
DataContractException(
8178
type="lint",
8279
name=f"Check that {label} are unique",
83-
reason=f"{path}[{index}].{keys[0]} duplicates {path}[{seen[key]}].{keys[0]} ({value})",
80+
reason=f"{path}[{index}].{key} duplicates {path}[{seen[value]}].{key} ({value!r})",
8481
)
8582
)
8683
else:
87-
seen[key] = index
84+
seen[value] = index
8885

8986
def visit(node, path):
9087
if not isinstance(node, dict):
9188
return
9289
properties = node.get("properties")
93-
check(properties, f"{path}.properties", ("name",), "property names")
90+
check(properties, f"{path}.properties", "name", "property names")
9491
for index, prop in enumerate(properties if isinstance(properties, list) else []):
9592
visit(prop, f"{path}.properties[{index}]")
9693
visit(node.get("items"), f"{path}.items")
@@ -100,12 +97,11 @@ def visit(node, path):
10097
visit(mapping.get(side), f"{path}.map.{side}")
10198

10299
schemas = contract.get("schema")
103-
check(schemas, "schema", ("name",), "schema names")
100+
check(schemas, "schema", "name", "schema names")
104101
for index, schema in enumerate(schemas if isinstance(schemas, list) else []):
105102
visit(schema, f"schema[{index}]")
106-
check(contract.get("servers"), "servers", ("server",), "server names")
107-
check(contract.get("roles"), "roles", ("role",), "role names")
108-
check(contract.get("slaProperties"), "slaProperties", ("property", "element"), "SLA properties")
103+
check(contract.get("servers"), "servers", "server", "server names")
104+
check(contract.get("roles"), "roles", "role", "role names")
109105

110106
def collect_ids(node, path):
111107
if isinstance(node, list):

‎tests/test_lint_duplicates.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ def test_unique_names_are_valid():
5757
"servers[1].server duplicates servers[0].server ('prod')",
5858
),
5959
({"roles": [{"role": "r"}, {"role": "r"}]}, "roles[1].role duplicates roles[0].role"),
60-
(
61-
{"slaProperties": [{"property": "latency", "value": 1}, {"property": "latency", "value": 2}]},
62-
"slaProperties[1].property duplicates slaProperties[0].property ('latency')",
63-
),
6460
(
6561
{"schema": [{"name": "o", "properties": [{"name": "a", "id": "x"}, {"name": "b", "id": "x"}]}]},
6662
"schema[0].properties[1].id duplicates schema[0].properties[0].id ('x')",
@@ -87,8 +83,11 @@ def test_same_name_in_different_scopes_is_valid():
8783
assert lint(doc).result == "passed"
8884

8985

90-
def test_sla_property_differing_by_element_is_valid():
91-
sla = [{"property": "latency", "value": 1, "element": "a.x"}, {"property": "latency", "value": 1, "element": "b.x"}]
86+
def test_repeated_sla_property_is_valid():
87+
sla = [
88+
{"property": "timeOfAvailability", "value": "09:00-08:00", "element": "orders.a", "driver": "regulatory"},
89+
{"property": "timeOfAvailability", "value": "08:00-08:00", "element": "orders.a", "driver": "analytics"},
90+
]
9291
assert lint(document(slaProperties=sla)).result == "passed"
9392

9493

0 commit comments

Comments
 (0)