diff --git a/lib/philomena/images/source.ex b/lib/philomena/images/source.ex index 476872b9..3936383f 100644 --- a/lib/philomena/images/source.ex +++ b/lib/philomena/images/source.ex @@ -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 diff --git a/lib/philomena/images/source_differ.ex b/lib/philomena/images/source_differ.ex index 8ac29a08..31a3b5b8 100644 --- a/lib/philomena/images/source_differ.ex +++ b/lib/philomena/images/source_differ.ex @@ -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