diff --git a/pydantic_jsonlogic/__init__.py b/pydantic_jsonlogic/__init__.py index a8d3531..5a358fc 100644 --- a/pydantic_jsonlogic/__init__.py +++ b/pydantic_jsonlogic/__init__.py @@ -1,5 +1,5 @@ from abc import ABCMeta -from typing import Any +from typing import Any, Union from pydantic import BaseModel, Field @@ -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): ... diff --git a/tests/test_fields.py b/tests/test_fields.py index fc8cadb..ef5157a 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -6,6 +6,7 @@ Cat, Divide, Equals, + Filter, GreaterThan, GreaterThanOrEqual, If, @@ -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)