Skip to content

Instantly share code, notes, and snippets.

@zoedsoupe
Created February 6, 2025 18:10
Show Gist options
  • Save zoedsoupe/5e2e06693211ea86676428b4e0d09189 to your computer and use it in GitHub Desktop.
Save zoedsoupe/5e2e06693211ea86676428b4e0d09189 to your computer and use it in GitHub Desktop.
defmodule Example do
@schema %{
name: {:required, :string},
profile: %{
email: {:required, :string},
username: {:required, :string}
}
}
def changeset(%{} = attrs) do
Peri.to_changeset!(@schema, attrs)
end
end
# iex(2)> Example.changeset %{}
# #Ecto.Changeset<
# action: nil,
# changes: %{},
# errors: [
# profile: {"can't be blank", [validation: :required]},
# name: {"can't be blank", [validation: :required]}
# ],
# data: %{},
# valid?: false,
# ...
# >
# iex(3)> Example.changeset %{name: "hello", profile: %{email: :invalid}}
# #Ecto.Changeset<
# action: nil,
# changes: %{
# name: "hello",
# profile: #Ecto.Changeset<
# action: nil,
# changes: %{},
# errors: [
# username: {"can't be blank", [validation: :required]},
# email: {"is invalid", [type: :string, validation: :cast]}
# ],
# data: %{},
# valid?: false,
# ...
# >
# },
# errors: [],
# data: %{},
# valid?: false,
# ...
# >
# iex(4)> Example.changeset %{name: "hello", profile: %{email: "zoey", username: "zoedsoupe"}}
# #Ecto.Changeset<
# action: nil,
# changes: %{
# name: "hello",
# profile: #Ecto.Changeset<
# action: nil,
# changes: %{email: "zoey", username: "zoedsoupe"},
# errors: [],
# data: %{},
# valid?: true,
# ...
# >
# },
# errors: [],
# data: %{},
# valid?: true,
# ...
# >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment