Skip to content

Commit

Permalink
refactor: rename strict key to strict?
Browse files Browse the repository at this point in the history
  • Loading branch information
heywhy committed Feb 1, 2025
1 parent b4e3de5 commit a16c697
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ defmodule Request.Validator do
module.rules()
end

opts = set_default_opts(opts)

errors =
params
|> Fields.new()
|> collect_errors(rules, set_strict_default(opts))
|> collect_errors(rules, opts)

case Enum.empty?(errors) do
true -> :ok
Expand Down Expand Up @@ -203,13 +205,15 @@ defmodule Request.Validator do
end
end

defp set_strict_default(opts) do
Keyword.put_new(opts, :strict, Application.get_env(:request_validator, :strict, false))
defp set_default_opts(opts) do
Keyword.put_new_lazy(opts, :strict?, fn ->
Application.get_env(:request_validator, :strict?, false)
end)
end

defp undeclared_fields(fields, rules, opts) do
opts
|> Keyword.get(:strict, false)
|> Keyword.get(:strict?, false)
|> case do
false ->
[]
Expand Down
3 changes: 1 addition & 2 deletions lib/validator/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ defmodule Request.Validator.Plug do
"""
def call(conn, opts) do
with action <- Map.get(conn.private, :phoenix_action),
module <- get_validator(opts, action),
false <- is_nil(module),
module when not is_nil(module) <- get_validator(opts, action),
{:authorized, true} <- {:authorized, module.authorize(conn)},
:ok <- module.validate(Conn.fetch_query_params(conn)) do
conn
Expand Down
2 changes: 1 addition & 1 deletion test/support/strict_request.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule RequestValidatorTest.StrictRequest do
use Request.Validator, strict: true
use Request.Validator, strict?: true

import Request.Validator.Rules

Expand Down

0 comments on commit a16c697

Please sign in to comment.