Skip to content

Commit

Permalink
Merge pull request #35 from fishtown-analytics/dont_best_match
Browse files Browse the repository at this point in the history
Do not use jsonschema.best_match because it obscures the errors
  • Loading branch information
gshank authored Sep 30, 2020
2 parents 67c837a + 79bb73f commit 9247dee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hologram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,6 @@ def _get_field_type_name(field_type: Any) -> str:
def validate(cls, data: Any):
schema = _validate_schema(cls)
validator = jsonschema.Draft7Validator(schema)
error = jsonschema.exceptions.best_match(validator.iter_errors(data))
error = next(iter(validator.iter_errors(data)), None)
if error is not None:
raise ValidationError.create_from(error) from error
20 changes: 19 additions & 1 deletion tests/test_union.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest

from dataclasses import dataclass
from typing import Union, Optional, List
from typing import Union, Optional, List, Dict, Any
import re

from hologram import JsonSchemaMixin, ValidationError

Expand Down Expand Up @@ -77,3 +78,20 @@ def test_long_union_decoding():
except ValidationError as exc:
str(exc)
raise


@dataclass
class UnionDefinition(JsonSchemaMixin):
my_field: Union[str, Dict[str, Any]]


def test_union_definition():
dct = {"my_field": ["string_a", "string_b"]}
with pytest.raises(ValidationError):
try:
UnionDefinition.from_dict(dct)
except ValidationError as exc:
assert exc.validator == "oneOf"
assert re.search("'type': 'string'", str(exc))
assert re.search("'type': 'object'", str(exc))
raise

0 comments on commit 9247dee

Please sign in to comment.