Added validation for min/max amounts
This commit is contained in:
parent
31e46dfbb8
commit
f457235b75
3 changed files with 42 additions and 0 deletions
|
|
@ -123,6 +123,7 @@ defmodule Pinchflat.Sources.Source do
|
||||||
|> dynamic_default(:uuid, fn _ -> Ecto.UUID.generate() end)
|
|> dynamic_default(:uuid, fn _ -> Ecto.UUID.generate() end)
|
||||||
|> validate_required(required_fields)
|
|> validate_required(required_fields)
|
||||||
|> validate_title_regex()
|
|> validate_title_regex()
|
||||||
|
|> validate_min_and_max_durations()
|
||||||
|> validate_number(:retention_period_days, greater_than_or_equal_to: 0)
|
|> validate_number(:retention_period_days, greater_than_or_equal_to: 0)
|
||||||
# Ensures it ends with `.{{ ext }}` or `.%(ext)s` or similar (with a little wiggle room)
|
# Ensures it ends with `.{{ ext }}` or `.%(ext)s` or similar (with a little wiggle room)
|
||||||
|> validate_format(:output_path_template_override, MediaProfile.ext_regex(), message: "must end with .{{ ext }}")
|
|> validate_format(:output_path_template_override, MediaProfile.ext_regex(), message: "must end with .{{ ext }}")
|
||||||
|
|
@ -169,6 +170,17 @@ defmodule Pinchflat.Sources.Source do
|
||||||
|
|
||||||
defp validate_title_regex(changeset), do: changeset
|
defp validate_title_regex(changeset), do: changeset
|
||||||
|
|
||||||
|
defp validate_min_and_max_durations(changeset) do
|
||||||
|
min_duration = get_change(changeset, :min_duration_seconds)
|
||||||
|
max_duration = get_change(changeset, :max_duration_seconds)
|
||||||
|
|
||||||
|
case {min_duration, max_duration} do
|
||||||
|
{min, max} when is_nil(min) or is_nil(max) -> changeset
|
||||||
|
{min, max} when min >= max -> add_error(changeset, :max_duration_seconds, "must be greater than minumum duration")
|
||||||
|
_ -> changeset
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defimpl Jason.Encoder, for: Source do
|
defimpl Jason.Encoder, for: Source do
|
||||||
def encode(value, opts) do
|
def encode(value, opts) do
|
||||||
value
|
value
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,7 @@
|
||||||
field={f[:min_duration_seconds]}
|
field={f[:min_duration_seconds]}
|
||||||
type="number"
|
type="number"
|
||||||
label="Minimum Duration (seconds)"
|
label="Minimum Duration (seconds)"
|
||||||
|
min="0"
|
||||||
help="Minimum duration of the media to be downloaded. Can be blank"
|
help="Minimum duration of the media to be downloaded. Can be blank"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
@ -132,6 +133,7 @@
|
||||||
field={f[:max_duration_seconds]}
|
field={f[:max_duration_seconds]}
|
||||||
type="number"
|
type="number"
|
||||||
label="Maximum Duration (seconds)"
|
label="Maximum Duration (seconds)"
|
||||||
|
min="0"
|
||||||
help="Maximum duration of the media to be downloaded. Can be blank"
|
help="Maximum duration of the media to be downloaded. Can be blank"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -681,6 +681,34 @@ defmodule Pinchflat.SourcesTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "change_source/3 when testing min/max duration validations" do
|
||||||
|
test "succeeds if min and max are nil" do
|
||||||
|
source = source_fixture()
|
||||||
|
|
||||||
|
assert %{errors: []} = Sources.change_source(source, %{min_duration_seconds: nil, max_duration_seconds: nil})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "succeeds if either min or max is nil" do
|
||||||
|
source = source_fixture()
|
||||||
|
|
||||||
|
assert %{errors: []} = Sources.change_source(source, %{min_duration_seconds: nil, max_duration_seconds: 100})
|
||||||
|
assert %{errors: []} = Sources.change_source(source, %{min_duration_seconds: 100, max_duration_seconds: nil})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "succeeds if min is less than max" do
|
||||||
|
source = source_fixture()
|
||||||
|
|
||||||
|
assert %{errors: []} = Sources.change_source(source, %{min_duration_seconds: 100, max_duration_seconds: 200})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "fails if min is greater than or equal to max" do
|
||||||
|
source = source_fixture()
|
||||||
|
|
||||||
|
assert %{errors: [_]} = Sources.change_source(source, %{min_duration_seconds: 200, max_duration_seconds: 100})
|
||||||
|
assert %{errors: [_]} = Sources.change_source(source, %{min_duration_seconds: 100, max_duration_seconds: 100})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "change_source/3 when testing original_url validation" do
|
describe "change_source/3 when testing original_url validation" do
|
||||||
test "succeeds when an original URL is valid" do
|
test "succeeds when an original URL is valid" do
|
||||||
source = source_fixture()
|
source = source_fixture()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue