Skip to content

Commit

Permalink
fix: do not fetch peri parser on raw data schemas (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe authored Aug 2, 2024
1 parent 859a0fd commit f942da9
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/peri.ex
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,9 @@ defmodule Peri do
end

defp validate_field(val, {:cond, condition, true_type, else_type}, parser) do
if condition.(parser.root_data) do
root = maybe_get_root_data(parser)

if condition.(root) do
validate_field(val, true_type, parser)
else
validate_field(val, else_type, parser)
Expand All @@ -592,7 +594,9 @@ defmodule Peri do

defp validate_field(val, {:dependent, callback}, parser)
when is_function(callback, 1) do
with {:ok, type} <- callback.(parser.root_data),
root = maybe_get_root_data(parser)

with {:ok, type} <- callback.(root),
{:ok, schema} <- validate_schema(type) do
validate_field(val, schema, parser)
end
Expand Down Expand Up @@ -725,7 +729,7 @@ defmodule Peri do
end

defp validate_field(data, schema, p) when is_enumerable(data) do
root = p.root_data
root = maybe_get_root_data(p)

case traverse_schema(schema, Peri.Parser.new(data, root_data: root)) do
%Peri.Parser{errors: []} = parser -> {:ok, parser.data}
Expand All @@ -738,6 +742,10 @@ defmodule Peri do
{:error, "expected type of %{expected} received %{actual} value", info}
end

# if schema is matches a raw data structure, it will not use the Peri.Parser
defp maybe_get_root_data(%Peri.Parser{} = p), do: p.root_data
defp maybe_get_root_data(data), do: data

@doc """
Validates a schema definition to ensure it adheres to the expected structure and types.
Expand Down

1 comment on commit f942da9

@dvv
Copy link

@dvv dvv commented on f942da9 Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!
Am getting

(CaseClauseError) no case clause matching: {:error, [%Peri.Error{path: [:email], key: :email, content: %{}, message: "is required", errors: nil}]}
    (peri 0.2.7) deps/peri/lib/peri.ex:264: Peri.validate/2

on the current master.
Please consider.
TIA

Please sign in to comment.