add length limits to all items

This commit is contained in:
2023-03-19 23:46:42 -04:00
parent e5e5449e8b
commit fe4e4f4f17
41 changed files with 473 additions and 399 deletions

View File

@ -48,8 +48,9 @@ defmodule Cannery.Accounts.Invite do
%__MODULE__{}
|> change(token: token, created_by_id: user_id)
|> cast(attrs, [:name, :uses_left, :disabled_at])
|> validate_required([:name, :token, :created_by_id])
|> validate_length(:name, max: 255)
|> validate_number(:uses_left, greater_than_or_equal_to: 0)
|> validate_required([:name, :token, :created_by_id])
end
@doc false
@ -57,7 +58,8 @@ defmodule Cannery.Accounts.Invite do
def update_changeset(invite, attrs) do
invite
|> cast(attrs, [:name, :uses_left, :disabled_at])
|> validate_required([:name])
|> validate_length(:name, max: 255)
|> validate_number(:uses_left, greater_than_or_equal_to: 0)
|> validate_required([:name])
end
end

View File

@ -79,6 +79,7 @@ defmodule Cannery.Accounts.User do
%User{}
|> cast(attrs, [:email, :password, :locale])
|> put_change(:invite_id, if(invite, do: invite.id))
|> validate_length(:locale, max: 255)
|> validate_email()
|> validate_password(opts)
end
@ -209,6 +210,7 @@ defmodule Cannery.Accounts.User do
def locale_changeset(user_or_changeset, locale) do
user_or_changeset
|> cast(%{"locale" => locale}, [:locale])
|> validate_length(:locale, max: 255)
|> validate_required(:locale)
end
end

View File

@ -61,6 +61,7 @@ defmodule Cannery.ActivityLog.ShotGroup do
|> change(user_id: user_id)
|> change(ammo_group_id: ammo_group_id)
|> cast(attrs, [:count, :notes, :date])
|> validate_length(:notes, max: 255)
|> validate_create_shot_group_count(ammo_group)
|> validate_required([:date, :ammo_group_id, :user_id])
end
@ -68,6 +69,7 @@ defmodule Cannery.ActivityLog.ShotGroup do
def create_changeset(shot_group, _invalid_user, _invalid_ammo_group, attrs) do
shot_group
|> cast(attrs, [:count, :notes, :date])
|> validate_length(:notes, max: 255)
|> validate_required([:ammo_group_id, :user_id])
|> add_error(:invalid, dgettext("errors", "Please select a valid user and ammo pack"))
end
@ -99,6 +101,7 @@ defmodule Cannery.ActivityLog.ShotGroup do
def update_changeset(%__MODULE__{} = shot_group, user, attrs) do
shot_group
|> cast(attrs, [:count, :notes, :date])
|> validate_length(:notes, max: 255)
|> validate_number(:count, greater_than: 0)
|> validate_required([:count, :date])
|> validate_update_shot_group_count(shot_group, user)

View File

@ -42,7 +42,7 @@ defmodule Cannery.Ammo.AmmoType do
field :name, :string
field :desc, :string
# https://en.wikipedia.org/wiki/Bullet#Abbreviations
# https://shootersreference.com/reloadingdata/bullet_abbreviations/
field :bullet_type, :string
field :bullet_core, :string
field :cartridge, :string
@ -129,20 +129,46 @@ defmodule Cannery.Ammo.AmmoType do
:upc
]
@spec string_fields() :: [atom()]
defp string_fields,
do: [
:name,
:bullet_type,
:bullet_core,
:cartridge,
:caliber,
:case_material,
:jacket_type,
:powder_type,
:pressure,
:primer_type,
:firing_type,
:manufacturer,
:upc
]
@doc false
@spec create_changeset(new_ammo_type(), User.t(), attrs :: map()) :: changeset()
def create_changeset(ammo_type, %User{id: user_id}, attrs) do
ammo_type
|> change(user_id: user_id)
|> cast(attrs, changeset_fields())
changeset =
ammo_type
|> change(user_id: user_id)
|> cast(attrs, changeset_fields())
string_fields()
|> Enum.reduce(changeset, fn field, acc -> acc |> validate_length(field, max: 255) end)
|> validate_required([:name, :user_id])
end
@doc false
@spec update_changeset(t() | new_ammo_type(), attrs :: map()) :: changeset()
def update_changeset(ammo_type, attrs) do
ammo_type
|> cast(attrs, changeset_fields())
changeset =
ammo_type
|> cast(attrs, changeset_fields())
string_fields()
|> Enum.reduce(changeset, fn field, acc -> acc |> validate_length(field, max: 255) end)
|> validate_required(:name)
end
end

View File

@ -53,6 +53,8 @@ defmodule Cannery.Containers.Container do
container
|> change(user_id: user_id)
|> cast(attrs, [:name, :desc, :type, :location])
|> validate_length(:name, max: 255)
|> validate_length(:type, max: 255)
|> validate_required([:name, :type, :user_id])
end
@ -61,6 +63,8 @@ defmodule Cannery.Containers.Container do
def update_changeset(container, attrs) do
container
|> cast(attrs, [:name, :desc, :type, :location])
|> validate_length(:name, max: 255)
|> validate_length(:type, max: 255)
|> validate_required([:name, :type])
end
end

View File

@ -47,6 +47,9 @@ defmodule Cannery.Containers.Tag do
tag
|> change(user_id: user_id)
|> cast(attrs, [:name, :bg_color, :text_color])
|> validate_length(:name, max: 255)
|> validate_length(:bg_color, max: 12)
|> validate_length(:text_color, max: 12)
|> validate_required([:name, :bg_color, :text_color, :user_id])
end
@ -55,6 +58,9 @@ defmodule Cannery.Containers.Tag do
def update_changeset(tag, attrs) do
tag
|> cast(attrs, [:name, :bg_color, :text_color])
|> validate_length(:name, max: 255)
|> validate_length(:bg_color, max: 12)
|> validate_length(:text_color, max: 12)
|> validate_required([:name, :bg_color, :text_color])
end
end