Skip to content

Commit

Permalink
Add "filter" model (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
4c0n authored Aug 28, 2024
1 parent a40fb4b commit 312e594
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pydantic_jsonlogic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABCMeta
from typing import Any
from typing import Any, Union

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -131,3 +131,15 @@ class And(BaseJSONLogicOperation):

class If(BaseJSONLogicOperation):
if_: list[Any] = Field(validation_alias="if")


class Filter(BaseJSONLogicOperation):
filter: tuple[
# An array or an operation that produces an array
Union[list[Any], Var, Missing, MissingSome, If, Merge, "Filter", "Map"],
# Something that produces a truthy or falsy result
bool | BaseJSONLogicOperation,
]


class Map(BaseJSONLogicOperation): ...
14 changes: 14 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Cat,
Divide,
Equals,
Filter,
GreaterThan,
GreaterThanOrEqual,
If,
Expand Down Expand Up @@ -479,3 +480,16 @@ def test_and(json: str) -> None:
)
def test_if(json: str) -> None:
If.model_validate_json(json)


@pytest.mark.parametrize(
"json",
[
'{"filter":[{"var":"integers"}, true]}',
'{"filter":[{"var":"integers"}, false]}',
'{"filter":[{"var":"integers"}, {">=":[{"var":""},2]}]}',
'{"filter":[{"var":"integers"}, {"%":[{"var":""},2]}]}',
],
)
def test_filter(json: str) -> None:
Filter.model_validate_json(json)

0 comments on commit 312e594

Please sign in to comment.