Skip to content

Commit

Permalink
rename to cast_all
Browse files Browse the repository at this point in the history
  • Loading branch information
maltoe committed Dec 20, 2023
1 parent b23e33b commit fb5a9ee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Added

* Add `BitcrowdEcto.Changeset.auto_cast/3`, a introspection-based automatic cast function.
* Add `BitcrowdEcto.Changeset.cast_all/3`, a introspection-based automatic cast function.
* Add `BitcrowdEcto.Assertions.assert_cast_error_on/2`.

### Fixed
Expand Down
32 changes: 16 additions & 16 deletions lib/bitcrowd_ecto/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ defmodule BitcrowdEcto.Changeset do
end
end

@type auto_cast_option :: {:action, atom}
@type cast_all_option :: {:action, atom}

@doc """
Introspects a schema and casts all defined fields from a params map.
Expand All @@ -443,45 +443,45 @@ defmodule BitcrowdEcto.Changeset do
## Examples
iex> changeset = auto_cast(TestEmbeddedSchema, %{some_field: 4})
iex> changeset = cast_all(TestEmbeddedSchema, %{some_field: 4})
iex> changeset.valid?
true
iex> changeset = auto_cast(%TestEmbeddedSchema{}, %{some_field: 4})
iex> changeset = cast_all(%TestEmbeddedSchema{}, %{some_field: 4})
iex> changeset.valid?
true
iex> changeset = auto_cast(change(%TestEmbeddedSchema{}), %{some_field: 4})
iex> changeset = cast_all(change(%TestEmbeddedSchema{}), %{some_field: 4})
iex> changeset.valid?
true
iex> changeset = auto_cast(TestEmbeddedSchema, %{}, required: [:some_field])
iex> changeset = cast_all(TestEmbeddedSchema, %{}, required: [:some_field])
iex> changeset.errors
[some_field: {"can't be blank", [validation: :required]}]
iex> changeset = auto_cast(TestEmbeddedSchema, %{}, optional: [:some_other_field])
iex> changeset = cast_all(TestEmbeddedSchema, %{}, optional: [:some_other_field])
iex> changeset.errors
[some_field: {"can't be blank", [validation: :required]}]
"""
@doc since: "0.17.0"
@spec auto_cast(module | struct, map) :: Ecto.Changeset.t()
@spec auto_cast(module | struct, map, [auto_cast_option]) :: Ecto.Changeset.t()
def auto_cast(schema_or_struct_or_changeset, params, opts \\ [])
@spec cast_all(module | struct, map) :: Ecto.Changeset.t()
@spec cast_all(module | struct, map, [cast_all_option]) :: Ecto.Changeset.t()
def cast_all(schema_or_struct_or_changeset, params, opts \\ [])

def auto_cast(%Ecto.Changeset{} = changeset, params, opts) do
do_auto_cast(changeset.data.__struct__, changeset, params, opts)
def cast_all(%Ecto.Changeset{} = changeset, params, opts) do
do_cast_all(changeset.data.__struct__, changeset, params, opts)
end

def auto_cast(schema, params, opts) when is_atom(schema) do
do_auto_cast(schema, struct!(schema), params, opts)
def cast_all(schema, params, opts) when is_atom(schema) do
do_cast_all(schema, struct!(schema), params, opts)
end

def auto_cast(struct, params, opts) when is_struct(struct) do
do_auto_cast(struct.__struct__, struct, params, opts)
def cast_all(struct, params, opts) when is_struct(struct) do
do_cast_all(struct.__struct__, struct, params, opts)
end

defp do_auto_cast(schema, struct_or_changeset, params, opts) do
defp do_cast_all(schema, struct_or_changeset, params, opts) do
required = required_fields(schema, opts)
%{scalars: scalars, embeds: embeds} = grouped_fields(schema)

Expand Down
20 changes: 10 additions & 10 deletions test/bitcrowd_ecto/changeset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ defmodule BitcrowdEcto.ChangesetTest do
end
end

describe "auto_cast/3" do
describe "cast_all/3" do
defp params do
embedded = %{"some_field" => 12}

Expand All @@ -713,7 +713,7 @@ defmodule BitcrowdEcto.ChangesetTest do
end

test "allows to automatically cast all fields of a schema" do
%Ecto.Changeset{} = cs = auto_cast(TestVarietySchema, params())
%Ecto.Changeset{} = cs = cast_all(TestVarietySchema, params())
assert cs.valid?
assert_changes(cs, :some_scalar, 5)
assert_changes(cs, :some_enum, :foo)
Expand All @@ -728,44 +728,44 @@ defmodule BitcrowdEcto.ChangesetTest do
end

test "accepts structs as input" do
%Ecto.Changeset{} = cs = auto_cast(%TestVarietySchema{}, params())
%Ecto.Changeset{} = cs = cast_all(%TestVarietySchema{}, params())
assert cs.valid?
end

test "accepts changesets as input" do
%Ecto.Changeset{} = cs = auto_cast(change(%TestVarietySchema{}), params())
%Ecto.Changeset{} = cs = cast_all(change(%TestVarietySchema{}), params())
assert cs.valid?
end

test "accepts params maps with atom keys" do
cs = auto_cast(TestVarietySchema, %{some_scalar: 5})
cs = cast_all(TestVarietySchema, %{some_scalar: 5})
assert cs.valid?
assert_changes(cs, :some_scalar, 5)
end

test "returns invalid changeset on cast errors" do
cs = auto_cast(TestVarietySchema, %{some_scalar: "foo"})
cs = cast_all(TestVarietySchema, %{some_scalar: "foo"})
refute cs.valid?
assert_cast_error_on(cs, :some_scalar)
end

test "accepts a list of required fields and validates them" do
cs = auto_cast(TestVarietySchema, %{}, required: [:some_scalar])
cs = cast_all(TestVarietySchema, %{}, required: [:some_scalar])
refute cs.valid?
assert_required_error_on(cs, :some_scalar)
refute_errors_on(cs, :some_enum)
end

test "accepts a list of optional fields and validates them" do
cs = auto_cast(TestVarietySchema, %{}, optional: [:some_scalar])
cs = cast_all(TestVarietySchema, %{}, optional: [:some_scalar])
refute cs.valid?
assert_required_error_on(cs, :some_enum)
refute_errors_on(cs, :some_scalar)
end

test "required validations work for embeds, too" do
cs =
auto_cast(
cast_all(
TestVarietySchema,
%{
one_embed: nil,
Expand All @@ -781,7 +781,7 @@ defmodule BitcrowdEcto.ChangesetTest do

test ":required and :optional are mutually exclusive" do
assert_raise ArgumentError, ~r/options are mutually exclusive/, fn ->
auto_cast(TestVarietySchema, %{}, required: [], optional: [])
cast_all(TestVarietySchema, %{}, required: [], optional: [])
end
end
end
Expand Down

0 comments on commit fb5a9ee

Please sign in to comment.