fix: avoid exception in Changeset.new/2 for bad attribute

This commit is contained in:
Zach Daniel 2021-03-20 17:16:57 -04:00
parent f1a6fb7418
commit b98650aeb7
2 changed files with 10 additions and 5 deletions

View file

@ -1576,11 +1576,6 @@ defmodule Ash.Changeset do
name: attribute
)
changeset = %{
changeset
| attributes: Map.put(changeset.attributes, attribute.name, value)
}
add_error(changeset, error)
%{writable?: false} = attribute ->

View file

@ -433,6 +433,16 @@ defmodule Ash.Test.Changeset.ChangesetTest do
end
end
test "if an attribute does not exist in `new/2`, a corresponding error is added`" do
error =
Author
|> Changeset.new(%{"flarb" => 10})
|> Map.get(:errors)
|> Enum.at(0)
assert %Ash.Error.Changes.NoSuchAttribute{name: "flarb"} = error
end
test "it destroys related records if not present" do
post1 = %{title: "title"}
post2 = %{title: "title"}