Merge pull request #320 from philomena-dev/extended-source-validation

Extended source validation
This commit is contained in:
liamwhite 2024-07-08 14:26:28 -04:00 committed by GitHub
commit 6c94595f8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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