Skip to content

Commit

Permalink
feat: pass root data to the dependet and cond types
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Jul 8, 2024
1 parent 9031b1e commit b79dfbd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
16 changes: 9 additions & 7 deletions lib/peri.ex
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ defmodule Peri do
"""
def validate(schema, data) when is_enumerable(schema) and is_enumerable(data) do
data = filter_data(schema, data)
state = Peri.Parser.new(data)
state = Peri.Parser.new(data, root_data: data)

case traverse_schema(schema, state) do
%Peri.Parser{errors: [], data: result} -> {:ok, result}
Expand Down Expand Up @@ -527,7 +527,7 @@ defmodule Peri do
end

defp validate_field(val, {:cond, condition, true_type, else_type}, parser) do
if condition.(parser.data) do
if condition.(parser.root_data) do
validate_field(val, true_type, parser)
else
validate_field(val, else_type, parser)
Expand All @@ -536,7 +536,7 @@ defmodule Peri do

defp validate_field(val, {:dependent, callback}, parser)
when is_function(callback, 1) do
with {:ok, type} <- callback.(parser.data),
with {:ok, type} <- callback.(parser.root_data),
{:ok, schema} <- validate_schema(type) do
validate_field(val, schema, parser)
end
Expand Down Expand Up @@ -667,8 +667,10 @@ defmodule Peri do
{:error, "expected a nested schema but received schema: %{type}", [type: schema]}
end

defp validate_field(data, schema, _data) when is_enumerable(data) do
case traverse_schema(schema, Peri.Parser.new(data)) do
defp validate_field(data, schema, p) when is_enumerable(data) do
root = p.root_data

case traverse_schema(schema, Peri.Parser.new(data, root_data: root)) do
%Peri.Parser{errors: []} = parser -> {:ok, parser.data}
%Peri.Parser{errors: errors} -> {:error, errors}
end
Expand Down Expand Up @@ -733,14 +735,14 @@ defmodule Peri do
```
"""
def validate_schema(schema) when is_enumerable(schema) do
case traverse_definition(schema, Peri.Parser.new(schema)) do
case traverse_definition(schema, Peri.Parser.new(schema, root_data: schema)) do
%Peri.Parser{errors: [], data: data} -> {:ok, data}
%Peri.Parser{errors: errors} -> {:error, errors}
end
end

def validate_schema(schema) do
case validate_type(schema, Peri.Parser.new(schema)) do
case validate_type(schema, Peri.Parser.new(schema, root_data: schema)) do
:ok ->
{:ok, schema}

Expand Down
6 changes: 3 additions & 3 deletions lib/peri/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Peri.Parser do
- `:path` - The current path within the data structure being validated.
"""

defstruct [:data, :errors, :path]
defstruct [:data, :errors, :path, :root_data]

@doc """
Initializes a new `Peri.Parser` struct with the given data.
Expand All @@ -25,8 +25,8 @@ defmodule Peri.Parser do
iex> Peri.Parser.new(%{name: "Alice"})
%Peri.Parser{data: %{name: "Alice"}, errors: [], path: []}
"""
def new(data) do
%__MODULE__{data: data, errors: [], path: []}
def new(data, root_data: root) do
%__MODULE__{data: data, root_data: root, errors: [], path: []}
end

@doc """
Expand Down

0 comments on commit b79dfbd

Please sign in to comment.