Extended source validation

This commit is contained in:
Liam 2024-07-04 19:09:56 -04:00
parent 41219bb217
commit 420099890c
2 changed files with 8 additions and 8 deletions

View file

@ -13,7 +13,9 @@ defmodule Philomena.Images.Source do
@doc false
def changeset(source, attrs) do
source
|> cast(attrs, [])
|> validate_required([])
|> cast(attrs, [:source])
|> validate_required([:source])
|> validate_format(:source, ~r/\Ahttps?:\/\//)
|> validate_length(:source, max: 255)
end
end

View file

@ -1,6 +1,5 @@
defmodule Philomena.Images.SourceDiffer do
import Ecto.Changeset
alias Philomena.Images.Source
def diff_input(changeset, old_sources, new_sources) do
old_set = MapSet.new(flatten_input(old_sources))
@ -13,12 +12,11 @@ defmodule Philomena.Images.SourceDiffer do
{sources, actually_added, actually_removed} =
apply_changes(source_set, added_sources, removed_sources)
image_id = fetch_field!(changeset, :id)
changeset
|> cast(source_params(sources), [])
|> put_change(:added_sources, actually_added)
|> put_change(:removed_sources, actually_removed)
|> put_assoc(:sources, source_structs(image_id, sources))
|> cast_assoc(:sources)
end
defp apply_changes(source_set, added_set, removed_set) do
@ -44,8 +42,8 @@ defmodule Philomena.Images.SourceDiffer do
{sources, actually_added, actually_removed}
end
defp source_structs(image_id, sources) do
Enum.map(sources, &%Source{image_id: image_id, source: &1})
defp source_params(sources) do
%{sources: Enum.map(sources, &%{source: &1})}
end
defp flatten_input(input) when is_map(input) do