Skip to content

Commit

Permalink
Fixed ZonString early-failure behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Jun 16, 2024
1 parent cb32646 commit 6784a05
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implemented non-default validation
- Added dimension tests for `ZonString`
- Added `examples` folder
- Added `early failure` mechanism to prevent validation on objects after the first validation error

### Changed
- Moved everything into a single file to combat circular reference issues
Expand Down
5 changes: 0 additions & 5 deletions tests/str_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ def validator():
return zon.string()


@pytest.fixture
def fail_fast_validator():
return zon.string(fast_termination=True)


def test_str_validate(validator):
assert validator.validate("1")

Expand Down
32 changes: 32 additions & 0 deletions tests/zon_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Tests related to core Zon functionality that is not necessarily bound to any specific Zon implementation.
"""

import pytest

import zon

@pytest.fixture
def normal_validator():
# This is just for demonstration purposes
return zon.string(fast_termination=False).length(3).max(2)

@pytest.fixture
def fail_fast_validator():
# This is just for demonstration purposes
return zon.string(fast_termination=True).length(3).max(2)


def test_fail_fast(normal_validator, fail_fast_validator):

test_input = "12345"

with pytest.raises(zon.error.ZonError) as fail_fast_result:
fail_fast_validator.validate(test_input)

assert len(fail_fast_result.value.issues) == 1

with pytest.raises(zon.error.ZonError) as normal_result:
normal_validator.validate(test_input)

assert len(normal_result.value.issues) == 2
6 changes: 4 additions & 2 deletions zon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, name: str, fn: Callable[[T], bool], *, additional_data: dict[
self.name = name
self.additional_data = additional_data

def check(self, data: Any, ctx: ValidationContext):
def check(self, data: Any, ctx: ValidationContext) -> bool:
valid = self.fn(data)

if not valid:
Expand All @@ -75,6 +75,7 @@ def check(self, data: Any, ctx: ValidationContext):
)
)

return valid

class Zon(ABC):
"""
Expand Down Expand Up @@ -132,6 +133,7 @@ def _validate(self, data: T) -> bool:
ctx = ValidationContext()

def check_early_termination():
print(self._terminate_early, ctx)
if self._terminate_early and ctx.dirty:
ctx.raise_error()

Expand Down Expand Up @@ -267,7 +269,7 @@ def string(*, fast_termination=False) -> ZonString:
Returns:
ZonString: The string data validator.
"""
return ZonString(fast_termination=fast_termination)
return ZonString(terminate_early=fast_termination)


class ZonString(ZonContainer):
Expand Down

0 comments on commit 6784a05

Please sign in to comment.