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