Skip to content

Commit db5e94f

Browse files
authored
Merge pull request #155 from QWED-AI/fix/schema-uniqueitems-fail-closed
fix: fail closed on uniqueItems validation errors
2 parents 0abe2e7 + a43631e commit db5e94f

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/qwed_new/core/schema_verifier.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,18 @@ def _validate_array(
504504
))
505505
break
506506
seen.add(item_key)
507-
except (TypeError, ValueError):
508-
pass # Can't check uniqueness for unhashable items
509-
507+
except (TypeError, ValueError) as exc:
508+
issues.append(SchemaIssue(
509+
path=path,
510+
issue_type="uniqueness_validation_error",
511+
expected="provably unique items",
512+
actual="uniqueness check could not be completed",
513+
message=(
514+
"uniqueItems could not be verified deterministically: "
515+
f"{exc}"
516+
)
517+
))
518+
510519
# items (single schema for all items)
511520
if "items" in schema and isinstance(schema["items"], dict):
512521
for i, item in enumerate(data):

tests/test_schema_verifier.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,18 @@ def test_unique_items_invalid(self, verifier):
242242
"""Array with duplicates fails."""
243243
schema = {"type": "array", "uniqueItems": True}
244244
result = verifier.verify([1, 2, 2, 3], schema)
245+
assert not result["is_valid"]
246+
247+
def test_unique_items_uncheckable_fails_closed(self, verifier):
248+
"""If uniqueness cannot be proven, validation must fail closed."""
249+
schema = {"type": "array", "uniqueItems": True}
250+
result = verifier.verify([{"bad": {1, 2}}, {"bad": {3, 4}}], schema)
251+
245252
assert result["is_valid"] == False
246-
253+
assert result["status"] == "INVALID"
254+
assert result["issues"][0]["type"] == "uniqueness_validation_error"
255+
assert "uniqueItems could not be verified deterministically" in result["issues"][0]["message"]
256+
247257
def test_items_schema(self, verifier):
248258
"""Array items validated against item schema."""
249259
schema = {

0 commit comments

Comments
 (0)