mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
Merge pull request #320 from philomena-dev/extended-source-validation
Extended source validation
This commit is contained in:
commit
6c94595f8c
2 changed files with 8 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue