harden changesets
This commit is contained in:
parent
f84f5ac133
commit
42bc918abc
@ -44,17 +44,12 @@ defmodule Cannery.Ammo.AmmoGroup do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(t() | new_ammo_group(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_ammo_group())
|
||||
@spec create_changeset(new_ammo_group(), attrs :: map()) :: Changeset.t(new_ammo_group())
|
||||
def create_changeset(ammo_group, attrs) do
|
||||
ammo_group
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :ammo_type_id, :container_id, :user_id])
|
||||
|> validate_required([
|
||||
:count,
|
||||
:ammo_type_id,
|
||||
:container_id,
|
||||
:user_id
|
||||
])
|
||||
|> validate_number(:count, greater_than: 0)
|
||||
|> validate_required([:count, :ammo_type_id, :container_id, :user_id])
|
||||
end
|
||||
|
||||
@doc false
|
||||
@ -62,11 +57,20 @@ defmodule Cannery.Ammo.AmmoGroup do
|
||||
Changeset.t(t() | new_ammo_group())
|
||||
def update_changeset(ammo_group, attrs) do
|
||||
ammo_group
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :ammo_type_id, :container_id, :user_id])
|
||||
|> validate_required([
|
||||
:count,
|
||||
:ammo_type_id,
|
||||
:container_id
|
||||
])
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :ammo_type_id, :container_id])
|
||||
|> validate_number(:count, greater_than: 0)
|
||||
|> validate_required([:count, :ammo_type_id, :container_id, :user_id])
|
||||
end
|
||||
|
||||
@doc """
|
||||
This range changeset is used when "using up" ammo groups, and allows for
|
||||
updating the count to 0
|
||||
"""
|
||||
@spec range_changeset(t() | new_ammo_group(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_ammo_group())
|
||||
def range_changeset(ammo_group, attrs) do
|
||||
ammo_group
|
||||
|> cast(attrs, [:count])
|
||||
|> validate_required([:count, :ammo_type_id, :container_id, :user_id])
|
||||
end
|
||||
end
|
||||
|
@ -93,8 +93,7 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
]
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(t() | new_ammo_type(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_ammo_type())
|
||||
@spec create_changeset(new_ammo_type(), attrs :: map()) :: Changeset.t(new_ammo_type())
|
||||
def create_changeset(ammo_type, attrs) do
|
||||
ammo_type
|
||||
|> cast(attrs, [:user_id | changeset_fields()])
|
||||
|
@ -42,8 +42,7 @@ defmodule Cannery.Containers.Container do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(t() | new_container(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_container())
|
||||
@spec create_changeset(new_container(), attrs :: map()) :: Changeset.t(new_container())
|
||||
def create_changeset(container, attrs) do
|
||||
container
|
||||
|> cast(attrs, [:name, :desc, :type, :location, :user_id])
|
||||
@ -56,6 +55,6 @@ defmodule Cannery.Containers.Container do
|
||||
def update_changeset(container, attrs) do
|
||||
container
|
||||
|> cast(attrs, [:name, :desc, :type, :location])
|
||||
|> validate_required([:name, :type])
|
||||
|> validate_required([:name, :type, :user_id])
|
||||
end
|
||||
end
|
||||
|
@ -31,8 +31,7 @@ defmodule Cannery.Containers.ContainerTag do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec changeset(t() | new_container_tag(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_container_tag())
|
||||
@spec changeset(new_container_tag(), attrs :: map()) :: Changeset.t(new_container_tag())
|
||||
def changeset(container_tag, attrs) do
|
||||
container_tag
|
||||
|> cast(attrs, [:tag_id, :container_id])
|
||||
|
@ -83,7 +83,7 @@ defmodule Cannery.Invites do
|
||||
%{"uses_left" => new_uses_left}
|
||||
end
|
||||
|
||||
invite |> Invite.changeset(attrs) |> Repo.update!()
|
||||
invite |> Invite.update_changeset(attrs) |> Repo.update!()
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -108,7 +108,7 @@ defmodule Cannery.Invites do
|
||||
|
||||
attrs = attrs |> Map.merge(%{"user_id" => user_id, "token" => token})
|
||||
|
||||
%Invite{} |> Invite.changeset(attrs) |> Repo.insert()
|
||||
%Invite{} |> Invite.create_changeset(attrs) |> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -126,7 +126,7 @@ defmodule Cannery.Invites do
|
||||
@spec update_invite(Invite.t(), attrs :: map(), User.t()) ::
|
||||
{:ok, Invite.t()} | {:error, Changeset.t(Invite.t())}
|
||||
def update_invite(invite, attrs, %User{role: :admin}),
|
||||
do: invite |> Invite.changeset(attrs) |> Repo.update()
|
||||
do: invite |> Invite.update_changeset(attrs) |> Repo.update()
|
||||
|
||||
@doc """
|
||||
Deletes a invite.
|
||||
@ -169,5 +169,5 @@ defmodule Cannery.Invites do
|
||||
Changeset.t(Invite.t() | Invite.new_invite())
|
||||
@spec change_invite(Invite.t() | Invite.new_invite(), attrs :: map()) ::
|
||||
Changeset.t(Invite.t() | Invite.new_invite())
|
||||
def change_invite(invite, attrs \\ %{}), do: invite |> Invite.changeset(attrs)
|
||||
def change_invite(invite, attrs \\ %{}), do: invite |> Invite.update_changeset(attrs)
|
||||
end
|
||||
|
@ -38,11 +38,20 @@ defmodule Cannery.Invites.Invite do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec changeset(t() | new_invite(), attrs :: map()) :: Changeset.t(t() | new_invite())
|
||||
def changeset(invite, attrs) do
|
||||
@spec create_changeset(new_invite(), attrs :: map()) :: Changeset.t(new_invite())
|
||||
def create_changeset(invite, attrs) do
|
||||
invite
|
||||
|> cast(attrs, [:name, :token, :uses_left, :disabled_at, :user_id])
|
||||
|> validate_required([:name, :token, :user_id])
|
||||
|> validate_number(:uses_left, greater_than_or_equal_to: 0)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec update_changeset(t() | new_invite(), attrs :: map()) :: Changeset.t(t() | new_invite())
|
||||
def update_changeset(invite, attrs) do
|
||||
invite
|
||||
|> cast(attrs, [:name, :token, :uses_left, :disabled_at])
|
||||
|> validate_required([:name, :token, :user_id])
|
||||
|> validate_number(:uses_left, greater_than_or_equal_to: 0)
|
||||
end
|
||||
end
|
||||
|
@ -35,7 +35,7 @@ defmodule Cannery.Tags.Tag do
|
||||
@type id() :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(t() | new_tag(), attrs :: map()) :: Changeset.t(t() | new_tag())
|
||||
@spec create_changeset(new_tag(), attrs :: map()) :: Changeset.t(new_tag())
|
||||
def create_changeset(tag, attrs) do
|
||||
tag
|
||||
|> cast(attrs, [:name, :bg_color, :text_color, :user_id])
|
||||
|
Loading…
Reference in New Issue
Block a user