Skip to content

Commit

Permalink
Define JsonschemaValidationErrorType
Browse files Browse the repository at this point in the history
For representing types of `jsonschema.exceptions.ValidationError` objects
  • Loading branch information
candleindark committed Oct 21, 2024
1 parent e8c397a commit d9afd97
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/dandisets_linkml_status_tools/cli/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Sequence
from datetime import datetime
from typing import Annotated, Any, Union
from typing import Annotated, Any, NamedTuple, Union

from dandi.dandiapi import VersionStatus
from jsonschema.exceptions import ValidationError
Expand Down Expand Up @@ -160,3 +160,26 @@ def dandiset_schema_version(self) -> str:

# Errors encountered in validation against the dandiset metadata model in LinkML
linkml_validation_errs: LinkmlValidationErrsType = []


class JsonschemaValidationErrorType(NamedTuple):
"""
A named tuple for representing types of `jsonschema.exceptions.ValidationError`
objects.
The type of a `jsonschema.exceptions.ValidationError` is decided by the value of its
`validator` field and the value of its `validator_value` field. The values
of these fields are bundled in an instance of this named tuple to represent a type
of `jsonschema.exceptions.ValidationError` objects.
"""

validator: str
validator_value: Any

def __eq__(self, other: object) -> bool:
return (
isinstance(other, JsonschemaValidationErrorType)
and self.validator == other.validator
and type(self.validator_value) is type(other.validator_value) # noqa E721
and self.validator_value == other.validator_value
)
41 changes: 41 additions & 0 deletions tests/test_cli/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest

from dandisets_linkml_status_tools.cli.models import JsonschemaValidationErrorType


@pytest.mark.parametrize(
("op1", "op2", "expected_result"),
[
(JsonschemaValidationErrorType("integer", [1, 2]), "hello", False),
(
JsonschemaValidationErrorType("integer", 1),
JsonschemaValidationErrorType("string", 1),
False,
),
(
JsonschemaValidationErrorType("integer", 1),
JsonschemaValidationErrorType("integer", "1"),
False,
),
(
JsonschemaValidationErrorType("integer", 1),
JsonschemaValidationErrorType("integer", 2),
False,
),
(
JsonschemaValidationErrorType("integer", 42),
JsonschemaValidationErrorType("integer", 42),
True,
),
(
JsonschemaValidationErrorType("integer", [1, 2, 3]),
JsonschemaValidationErrorType("integer", [1, 2, 3]),
True,
),
],
)
def test_jsonschema_validation_error_type_equality(op1, op2, expected_result):
"""
Test the equal operator of the `JsonschemaValidationErrorType` class
"""
assert (op1 == op2) == expected_result

0 comments on commit d9afd97

Please sign in to comment.