Commit 203ebca
authored
[JSON Validator] Tuple validation, multi-type enum checks (#213)
This PR addresses two related bugs in the schema validator that caused
it to throw fatal errors (`UnsupportedSchemaException` or a `TypeError`)
when it should have gracefully returned a `ValidationError`.
### The Problem
The validator would crash when attempting to validate data against a
schema that used either of the following valid JSON Schema constructs:
1. **Tuple Validation:** When an array's `items` keyword was an array of
schemas (e.g., `{"type": "array", "items": [{"type": "string"}, {"type":
"object"}]}`), the validator would throw an `UnsupportedSchemaException`
instead of validating the data.
2. **Multi-type Enums:** When a schema used an array for the `type`
keyword along with an `enum` (e.g., `{"type": ["string", "number"]}`),
the validator's internal integrity check would cause a fatal
`TypeError`.
This meant that schemas that are perfectly valid according to the JSON
Schema spec were not supported and would break the validation process
entirely.
### Root Cause Analysis
1. The `validate_array()` method was only implemented to handle "list
validation" (where `items` is a single schema object). It lacked the
logic to handle "tuple validation," causing it to misinterpret the array
of schemas and throw an exception.
2. The schema integrity check inside `validate_type()` was incorrectly
calling `type_matches()`—which only accepts a string for the
type—instead of the `type_matches_any()` method, which is designed to
handle an array of types.
### The Solution
This PR implements two fixes to make the validator more compliant with
the JSON Schema spec:
1. **Added Tuple Validation Logic:** The `validate_array()` method now
detects when the `items` keyword is an array (a tuple) and applies the
correct validation logic, iterating through the data and schema arrays
in lockstep.
2. **Corrected the Enum Integrity Check:** The check in
`validate_type()` has been updated to use the `type_matches_any()`
method, allowing it to correctly validate schemas that use multiple
types.
### How to Verify
I have added a new unit test,
`testItReturnsValidationErrorForInvalidTupleData()`, which uses a schema
with tuple validation.
* **Before this fix:** Running this test causes the validator to throw a
fatal `UnsupportedSchemaException`.
* **After this fix:** The test now passes, as the validator correctly
identifies the invalid data and returns a `ValidationError` object as
expected.1 parent 6542f2a commit 203ebca
File tree
2 files changed
+152
-5
lines changed- components/Blueprints
- Tests/Unit/Validator
- Validator
2 files changed
+152
-5
lines changedLines changed: 95 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
312 | 312 | | |
313 | 313 | | |
314 | 314 | | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
315 | 337 | | |
316 | 338 | | |
317 | 339 | | |
| |||
1643 | 1665 | | |
1644 | 1666 | | |
1645 | 1667 | | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
| 1703 | + | |
| 1704 | + | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
| 1718 | + | |
| 1719 | + | |
| 1720 | + | |
| 1721 | + | |
| 1722 | + | |
| 1723 | + | |
| 1724 | + | |
| 1725 | + | |
| 1726 | + | |
| 1727 | + | |
| 1728 | + | |
| 1729 | + | |
| 1730 | + | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
| 1734 | + | |
| 1735 | + | |
| 1736 | + | |
| 1737 | + | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
1646 | 1741 | | |
Lines changed: 57 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
582 | 582 | | |
583 | 583 | | |
584 | 584 | | |
585 | | - | |
| 585 | + | |
586 | 586 | | |
587 | 587 | | |
588 | 588 | | |
| |||
731 | 731 | | |
732 | 732 | | |
733 | 733 | | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
738 | 790 | | |
739 | 791 | | |
740 | 792 | | |
| |||
0 commit comments