Skip to content

Commit

Permalink
Merge pull request #350 from woylie/fix/replace-invalid-page
Browse files Browse the repository at this point in the history
fix/replace invalid page
  • Loading branch information
woylie authored Jun 27, 2023
2 parents 0a754a2 + fa3d8c9 commit 39bddb2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

### Fixed

- When the `replace_invalid_params` option was set to `true`, cast errors for
pagination and sorting parameters were still causing validation errors instead
of defaulting to valid parameters.

## [0.20.3] - 2023-06-23

### Changed
Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ config :stream_data,
max_runs: if(System.get_env("CI"), do: 100, else: 50),
max_run_time: if(System.get_env("CI"), do: 3000, else: 200)

config :logger, level: :warn
config :logger, level: :warning
8 changes: 7 additions & 1 deletion lib/flop/validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ defmodule Flop.Validation do
validated_changeset = validate_func.(changeset, field, opts)

if validated_changeset.errors[field] do
delete_change(changeset, field)
changeset
|> delete_change(field)
|> Map.update!(:errors, &Keyword.delete(&1, field))
else
validated_changeset
end
Expand Down Expand Up @@ -259,6 +261,10 @@ defmodule Flop.Validation do
changeset
|> put_change(:order_by, new_order_by)
|> put_change(:order_directions, new_order_directions)
|> Map.update!(
:errors,
&Keyword.drop(&1, [:order_by, :order_directions])
)
else
validate_subset(changeset, :order_by, sortable_fields)
end
Expand Down
17 changes: 17 additions & 0 deletions test/flop/validation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ defmodule Flop.ValidationTest do
validate(params, replace_invalid_params: true)
end

test "replaces malformed page with replace_invalid_params" do
params = %{page: "a", page_size: 10}

assert {:ok, %Flop{page: 1}} =
validate(params, replace_invalid_params: true)
end

test "page size must be a positive integer" do
params = %{page_size: 0}
assert {:error, changeset} = validate(params)
Expand Down Expand Up @@ -686,6 +693,16 @@ defmodule Flop.ValidationTest do
validate(params, for: Pet, replace_invalid_params: true)
end

test "replaces malformed order fields with replace_invalid_params" do
params = %{
order_by: 5,
order_directions: true
}

assert {:ok, %Flop{order_by: [:name], order_directions: [:asc]}} =
validate(params, for: Fruit, replace_invalid_params: true)
end

test "validates order directions" do
params = %{order_directions: [:up, :down]}
assert {:error, changeset} = validate(params)
Expand Down

0 comments on commit 39bddb2

Please sign in to comment.