diff --git a/pydantic_jsonlogic/__init__.py b/pydantic_jsonlogic/__init__.py index bea3f7a..a471bbf 100644 --- a/pydantic_jsonlogic/__init__.py +++ b/pydantic_jsonlogic/__init__.py @@ -149,3 +149,14 @@ class Map(BaseJSONLogicOperation): # The operation to be performed on each element of the array BaseJSONLogicOperation, ] + + +class Reduce(BaseJSONLogicOperation): + reduce: tuple[ + # An array or an operation that produces an array + list[Any] | Var | Missing | MissingSome | If | Merge | Filter | Map, + # The operation to be performed for each element of the array + BaseJSONLogicOperation, + # The initial accumulator value + Any, + ] diff --git a/tests/test_fields.py b/tests/test_fields.py index 3742b7c..d374005 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -26,6 +26,7 @@ NotEquals, NotNot, Or, + Reduce, StrictEquals, StrictNotEquals, Substr, @@ -506,3 +507,27 @@ def test_filter(json: str) -> None: ) def test_map(json: str) -> None: Map.model_validate_json(json) + + +@pytest.mark.parametrize( + "json", + [ + """{"reduce":[ + {"var":"integers"}, + {"+":[{"var":"current"}, {"var":"accumulator"}]}, + 0 + ]}""", + """{"reduce":[ + {"var":"integers"}, + {"+":[{"var":"current"}, {"var":"accumulator"}]}, + {"var": "start_with"} + ]}""", + """{"reduce":[ + {"var":"integers"}, + {"*":[{"var":"current"}, {"var":"accumulator"}]}, + 1 + ]}""", + ], +) +def test_reduce(json: str) -> None: + Reduce.model_validate_json(json)