Use Ecto.UUID instead of hand-rolled type
This commit is contained in:
@@ -19,7 +19,7 @@ defmodule MusicLibrary.ScrobbleRules.ScrobbleRule do
|
|||||||
schema "scrobble_rules" do
|
schema "scrobble_rules" do
|
||||||
field :type, :string
|
field :type, :string
|
||||||
field :match_value, :string
|
field :match_value, :string
|
||||||
field :target_musicbrainz_id, :string
|
field :target_musicbrainz_id, Ecto.UUID
|
||||||
field :enabled, :boolean, default: true
|
field :enabled, :boolean, default: true
|
||||||
field :description, :string
|
field :description, :string
|
||||||
|
|
||||||
@@ -32,28 +32,5 @@ defmodule MusicLibrary.ScrobbleRules.ScrobbleRule do
|
|||||||
|> cast(attrs, [:type, :match_value, :target_musicbrainz_id, :enabled, :description])
|
|> cast(attrs, [:type, :match_value, :target_musicbrainz_id, :enabled, :description])
|
||||||
|> validate_required([:type, :match_value, :target_musicbrainz_id])
|
|> validate_required([:type, :match_value, :target_musicbrainz_id])
|
||||||
|> validate_inclusion(:type, @valid_types)
|
|> validate_inclusion(:type, @valid_types)
|
||||||
|> validate_musicbrainz_id_format(:target_musicbrainz_id)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp validate_musicbrainz_id_format(changeset, field) do
|
|
||||||
validate_change(changeset, field, fn _, value ->
|
|
||||||
case value do
|
|
||||||
"" ->
|
|
||||||
[{field, "cannot be empty"}]
|
|
||||||
|
|
||||||
value when is_binary(value) ->
|
|
||||||
if String.match?(
|
|
||||||
value,
|
|
||||||
~r/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i
|
|
||||||
) do
|
|
||||||
[]
|
|
||||||
else
|
|
||||||
[{field, "must be a valid MusicBrainz ID (UUID format)"}]
|
|
||||||
end
|
|
||||||
|
|
||||||
_ ->
|
|
||||||
[{field, "must be a string"}]
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ defmodule MusicLibrary.ScrobbleRules.ScrobbleRuleTest do
|
|||||||
changeset = ScrobbleRule.changeset(%ScrobbleRule{}, attrs)
|
changeset = ScrobbleRule.changeset(%ScrobbleRule{}, attrs)
|
||||||
refute changeset.valid?
|
refute changeset.valid?
|
||||||
|
|
||||||
assert "must be a valid MusicBrainz ID (UUID format)" in errors_on(changeset).target_musicbrainz_id
|
assert "is invalid" in errors_on(changeset).target_musicbrainz_id
|
||||||
end
|
end
|
||||||
|
|
||||||
test "valid changeset with uppercase UUID" do
|
test "valid changeset with uppercase UUID" do
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ defmodule MusicLibraryWeb.ScrobbleRulesLiveTest do
|
|||||||
target_musicbrainz_id: "invalid-uuid"
|
target_musicbrainz_id: "invalid-uuid"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|> render_change() =~ "must be a valid MusicBrainz ID"
|
|> render_change() =~ "is invalid"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "updates form labels based on rule type", %{conn: conn} do
|
test "updates form labels based on rule type", %{conn: conn} do
|
||||||
|
|||||||
Reference in New Issue
Block a user