Skip to content

Commit

Permalink
Unify Module API
Browse files Browse the repository at this point in the history
  • Loading branch information
bluzky committed Feb 28, 2024
1 parent 9ab7aa5 commit e2702e8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 55 deletions.
7 changes: 0 additions & 7 deletions lib/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ defmodule Skema.Schema do
Skema.validate(params, @ts_fields)
end

def cast_and_validate(params) do
case Skema.cast_and_validate(params, @ts_fields) do
{:ok, data} -> {:ok, new(data)}
error -> error
end
end

def __fields__ do
@ts_fields
end
Expand Down
33 changes: 9 additions & 24 deletions lib/skema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,10 @@ defmodule Skema do
```
"""

defp schema_mod?(module) do
function_exported?(module, :__fields__, 0)
end

defp prepare_schema(schema) do
if schema_mod?(schema) do
{schema.__fields__(), schema}
else
{schema, nil}
end
end

@spec cast_and_validate(data :: map(), schema :: map()) ::
{:ok, map()} | {:error, errors :: map()}
def cast_and_validate(data, schema) do
schema = Skema.SchemaHelper.expand(schema)

result =
[schema: schema, params: data]
|> Result.new()
|> cast()

with {_, {:ok, data}} <- {:cast, result},
with {_, {:ok, data}} <- {:cast, cast(data, schema)},
{_, :ok} <- {:validate, validate(data, schema)} do
{:ok, data}
else
Expand All @@ -70,10 +51,10 @@ defmodule Skema do
end
end

def cast_and_validate!(data, schema) do
case cast_and_validate(data, schema) do
{:ok, value} -> value
_ -> raise "Skema :: bad input data"
def cast(data, schema) when is_atom(schema) do
case cast(data, schema.__fields__()) do
{:ok, data} -> {:ok, struct(schema, data)}
error -> error
end
end

Expand Down Expand Up @@ -102,6 +83,10 @@ defmodule Skema do
end
end

def validate(data, schema) when is_atom(schema) do
validate(data, schema.__fields__())
end

def validate(data, schema) when is_map(data) do
schema = Skema.SchemaHelper.expand(schema)

Expand Down
26 changes: 12 additions & 14 deletions test/defschema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule DefSchemaTest do
}
}

assert {:ok, %UserNestedModel{user: %UserModel{name: "D", email: "d@h.com", age: 10}}} = UserNestedModel.cast(data)
assert {:ok, %UserNestedModel{user: %UserModel{name: "D", email: "d@h.com", age: 10}}} =
Skema.cast(data, UserNestedModel)
end

test "cast with no value should default to nil and skip validation" do
Expand All @@ -33,7 +34,7 @@ defmodule DefSchemaTest do
}
}

assert {:ok, %{user: %{email: nil}}} = UserNestedModel.cast(data)
assert {:ok, %{user: %{email: nil}}} = Skema.cast(data, UserNestedModel)
end

test "cast_and_validate embed validation invalid should error" do
Expand All @@ -46,10 +47,10 @@ defmodule DefSchemaTest do
}

assert {:ok, casted_data} =
UserNestedModel.cast(data)
Skema.cast(data, UserNestedModel)

assert {:error, %{errors: %{user: [%{errors: %{email: ["length must be greater than or equal to 5"]}}]}}} =
UserNestedModel.validate(casted_data)
Skema.validate(casted_data, UserNestedModel)
end

test "cast_and_validate missing required value should error" do
Expand All @@ -60,10 +61,10 @@ defmodule DefSchemaTest do
}

assert {:ok, casted_data} =
UserNestedModel.cast(data)
Skema.cast(data, UserNestedModel)

assert {:error, %{errors: %{user: [%{errors: %{name: ["is required"]}}]}}} =
UserNestedModel.validate(casted_data)
Skema.validate(casted_data, UserNestedModel)
end

defschema UserListModel do
Expand All @@ -89,15 +90,15 @@ defmodule DefSchemaTest do
"users" => []
}

assert {:ok, %{users: []}} = UserListModel.cast(data)
assert {:ok, %{users: []}} = Skema.cast(data, UserListModel)
end

test "cast_and_validate nil array embed should ok" do
data = %{
"users" => nil
}

assert {:ok, %{users: nil}} = UserListModel.cast(data)
assert {:ok, %{users: nil}} = Skema.cast(data, UserListModel)
end

test "cast_and_validate array embed with invalid value should error" do
Expand All @@ -115,8 +116,6 @@ defmodule DefSchemaTest do
]
}

assert {:ok, casted} = UserListModel.cast(data)

assert {:error,
%{
errors: %{
Expand All @@ -125,8 +124,7 @@ defmodule DefSchemaTest do
%{errors: %{email: ["length must be greater than or equal to 5"]}}
]
}
}} =
UserListModel.validate(casted)
}} = Skema.cast_and_validate(data, UserListModel)
end

defschema UserModel2 do
Expand All @@ -152,11 +150,11 @@ defmodule DefSchemaTest do
},
id: ["is invalid"]
}
}} = UserRoleModel.cast_and_validate(params)
}} = Skema.cast_and_validate(params, UserRoleModel)
end

test "return error when given map for array type" do
assert {:error, %{errors: %{users: ["is invalid"]}}} = UserListModel.cast(%{users: %{}})
assert {:error, %{errors: %{users: ["is invalid"]}}} = Skema.cast(%{users: %{}}, UserListModel)
end
end
end
10 changes: 0 additions & 10 deletions test/skema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,6 @@ defmodule ParamTest do
)
end

test "Skema.cast_and_validate! success" do
assert %{number: 10} = Skema.cast_and_validate!(%{number: "10"}, %{number: :integer})
end

test "Skema.cast_and_validate! raise exception" do
assert_raise RuntimeError, fn ->
Skema.cast_and_validate!(%{number: 10}, %{number: {:array, :string}})
end
end

test "cast_and_validate with from" do
schema = %{
user_email: [type: :string, from: :email]
Expand Down

0 comments on commit e2702e8

Please sign in to comment.