Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[289509] Implement parameter validation #272

Merged
merged 42 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7530c57
[289509] Implement `BaseParamStruct`
Double-oxygeN Jun 5, 2024
f533799
[289509] Implement `Antikythera.BodyJsonStruct`
Double-oxygeN Jun 7, 2024
4e4ee7b
[280509] Implement `Antikythera.ParamStringStruct`
Double-oxygeN Jun 7, 2024
cb9e297
[289509] Refactor
Double-oxygeN Jun 7, 2024
1e65957
[289509] Implement `Antikythera.Plug.ParamsValidator.validate/2`
Double-oxygeN Jun 10, 2024
9a579e4
[289509] Implement `BodyJsonList` and `BodyJsonMap`
Double-oxygeN Jun 10, 2024
925741b
[289509] Use default value if the module of a missing field has `defa…
Double-oxygeN Jun 13, 2024
0552b36
[289509] Fix `from_params/1`
Double-oxygeN Jun 13, 2024
6dde765
[289509] Refactor `Antikythera.BaseParamStruct`
Double-oxygeN Jun 13, 2024
c136a10
[289509] Add support of `:accept_case` option
Double-oxygeN Jun 18, 2024
e77010f
[289509] Add support of `:default` option
Double-oxygeN Jun 18, 2024
870a784
[289509] Write documentations
Double-oxygeN Jun 19, 2024
ceef040
[289509] Add "Exported functions" sections
Double-oxygeN Jun 21, 2024
080fc60
[289509] Refactor base_param_struct.ex
Double-oxygeN Jun 21, 2024
f08bc95
[289509] Fix: implicit preprocessors can be defined as anonymous func…
Double-oxygeN Jun 26, 2024
91f7d22
Revert "[289509] Fix: implicit preprocessors can be defined as anonym…
Double-oxygeN Jun 26, 2024
2a2b1ff
[289509] Add more description about the limitation of module attributes
Double-oxygeN Jun 26, 2024
73d6d6f
[289509] Wrap by `v[]` as much as possible
Double-oxygeN Jun 27, 2024
659deeb
[289509] Move `BaseParamStruct` to `AntikytheraCore`
Double-oxygeN Jun 27, 2024
8e72700
[289509] Use alias for `Croma.Result`
Double-oxygeN Jun 27, 2024
0e9dea2
[289509] Add `BaseParamStruct.attach_attributes_to_field/3`
Double-oxygeN Jun 27, 2024
99a36cb
[289509] Use `Map.new/1` instead of `Enum.into/2`
Double-oxygeN Jun 27, 2024
e5c39dd
[289509] Fix type annotations
Double-oxygeN Jun 27, 2024
d983a98
[289509] Rename guards
Double-oxygeN Jun 27, 2024
e739bec
[289509] Fix: JSON objects → JSON values
Double-oxygeN Jun 27, 2024
cafa89d
[289509] Default preprocessor is fixed to `&Function.identity/1`
Double-oxygeN Jun 27, 2024
842bcc5
[289509] Add `AntikytheraCore.BodyJsonCommon`
Double-oxygeN Jun 27, 2024
bf5ee47
[289509] Fix docs
Double-oxygeN Jun 27, 2024
a85f5eb
[289509] Move test code file
Double-oxygeN Jun 28, 2024
d456650
[289509] Fix tests
Double-oxygeN Jun 28, 2024
4140383
[289509] Add explanation about validation error handling
Double-oxygeN Jul 1, 2024
413a07c
[289509] Make `BaseParamStruct` docs simpler
Double-oxygeN Jul 3, 2024
d3b77bb
[289509] Rename to `:preprocessor_generator`
Double-oxygeN Jul 3, 2024
92da097
[289509] Omit `key` argument from `BaseParamStruct.fetch_from_dict`
Double-oxygeN Jul 3, 2024
f4a5ac1
[289509] Fix docs about how to get the validated params
Double-oxygeN Jul 3, 2024
b81530d
[289509] Refactor `preprocess_params`
Double-oxygeN Jul 3, 2024
88c9f5e
[289509] Add `Antikythera.StringPreprocessor`
Double-oxygeN Jul 4, 2024
a676746
[289509] Rename `Preprocessor.default/1` to `PreprocessorGenerator.ge…
Double-oxygeN Jul 4, 2024
2a4c8db
[289509] Hide `AntikytheraCore.BodyJsonCommon.PreprocessorGenerator`
Double-oxygeN Jul 4, 2024
65a1f2a
[289509] Fix types
Double-oxygeN Jul 4, 2024
f69e267
[289509] Rename to `get_default_value_from_module`
Double-oxygeN Jul 4, 2024
e61e49b
[289509] Make some functions private
Double-oxygeN Jul 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[289509] Move BaseParamStruct to AntikytheraCore
  • Loading branch information
Double-oxygeN committed Jun 27, 2024
commit 659deebbeba14b2c9f9cee42b90badbb19dbaf65
4 changes: 2 additions & 2 deletions core/handler/gear_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule AntikytheraCore.Handler.GearError do
defun parameter_validation_error(
conn :: v[Conn.t()],
parameter_type :: Antikythera.Plug.ParamsValidator.parameter_type_t(),
reason :: Antikythera.BaseParamStruct.validate_error_t()
reason :: AntikytheraCore.BaseParamStruct.validate_error_t()
) :: v[Conn.t()] do
invoke_error_handler(
conn,
Expand All @@ -72,7 +72,7 @@ defmodule AntikytheraCore.Handler.GearError do

defunp create_default_parameter_validation_error_message(
parameter_type :: Antikythera.Plug.ParamsValidator.parameter_type_t(),
{error_reason, mods} :: Antikythera.BaseParamStruct.validate_error_t()
{error_reason, mods} :: AntikytheraCore.BaseParamStruct.validate_error_t()
) :: v[String.t()] do
field_path =
Enum.flat_map(mods, fn
Expand Down
18 changes: 8 additions & 10 deletions lib/type/base_param_struct.ex → core/type/base_param_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

use Croma

defmodule Antikythera.BaseParamStruct do
defmodule AntikytheraCore.BaseParamStruct do
@moduledoc """
Module to define a struct that represents abstract parameters.

This module is a base module of `Antikythera.ParamStringStruct` and `Antikythera.BodyJsonStruct`.
See their respective module documents for details.

It is not recommended to use this module directly from gears.
"""

@type json_value_t() ::
Expand Down Expand Up @@ -302,7 +300,7 @@ defmodule Antikythera.BaseParamStruct do
{field_name, {mod, preprocessor}}
when is_atom(field_name) and is_atom(mod) and is_function(preprocessor, 1) ->
{field_name, mod, preprocessor,
Antikythera.BaseParamStruct.compute_default_value(mod)}
AntikytheraCore.BaseParamStruct.compute_default_value(mod)}

{field_name, {mod, [default: default_value]}}
when is_atom(field_name) and is_atom(mod) ->
Expand All @@ -311,11 +309,11 @@ defmodule Antikythera.BaseParamStruct do

{field_name, mod} when is_atom(field_name) and is_atom(mod) ->
{field_name, mod, Croma.Result.get(default_pp.(mod), &Function.identity/1),
Antikythera.BaseParamStruct.compute_default_value(mod)}
AntikytheraCore.BaseParamStruct.compute_default_value(mod)}
end)
|> Enum.map(fn {field_name, mod, preprocessor, default_value_opt} ->
{field_name,
Antikythera.BaseParamStruct.compute_accepted_field_names(
AntikytheraCore.BaseParamStruct.compute_accepted_field_names(
field_name,
opts[:accept_case]
), mod, preprocessor, default_value_opt}
Expand Down Expand Up @@ -344,7 +342,7 @@ defmodule Antikythera.BaseParamStruct do

defun from_params(params :: term()) :: Croma.Result.t(t()) do
params when is_map(params) ->
Antikythera.BaseParamStruct.preprocess_params(
AntikytheraCore.BaseParamStruct.preprocess_params(
__MODULE__,
@base_param_struct_fields_with_attrs,
params
Expand All @@ -361,7 +359,7 @@ defmodule Antikythera.BaseParamStruct do

# Override
def new(dict) do
Antikythera.BaseParamStruct.new_impl(
AntikytheraCore.BaseParamStruct.new_impl(
__MODULE__,
@base_param_struct_fields_with_attrs,
dict
Expand All @@ -370,7 +368,7 @@ defmodule Antikythera.BaseParamStruct do

# Override
def update(s, dict) do
Antikythera.BaseParamStruct.update_impl(
AntikytheraCore.BaseParamStruct.update_impl(
s,
__MODULE__,
@base_param_struct_fields_with_accept_fields,
Expand All @@ -381,7 +379,7 @@ defmodule Antikythera.BaseParamStruct do
# Override
def valid?(%__MODULE__{} = s) do
Enum.all?(@base_param_struct_fields, fn {field_name, mod} ->
Map.fetch!(s, field_name) |> Antikythera.BaseParamStruct.valid_field?(mod)
Map.fetch!(s, field_name) |> AntikytheraCore.BaseParamStruct.valid_field?(mod)
end)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/gear_application/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Antikythera.GearApplication.ErrorHandler do
- Optional error handlers
- `bad_executor_pool_id(Antikythera.Conn.t, Antikythera.ExecutorPool.BadIdReason.t) :: Antikythera.Conn.t`
- `ws_too_many_connections(Antikythera.Conn.t) :: Antikythera.Conn.t` (when your gear uses websocket)
- `parameter_validation_error(Antikythera.Conn.t, Antikythera.Plug.ParamsValidator.parameter_type_t, Antikythera.BaseParamStruct.validate_error_t) :: Antikythera.Conn.t` (when your gear uses parameter validation)
- `parameter_validation_error(Antikythera.Conn.t, Antikythera.Plug.ParamsValidator.parameter_type_t, AntikytheraCore.BaseParamStruct.validate_error_t) :: Antikythera.Conn.t` (when your gear uses parameter validation)

This module generates `YourGear.error_handler_module/0` function, which is called by antikythera when handling errors.
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/type/body_json_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Antikythera.BodyJsonList do
- `max_length`: The maximum length of the list. If not specified, there is no maximum length.
"""

alias Antikythera.BaseParamStruct
alias AntikytheraCore.BaseParamStruct
alias Antikythera.BodyJsonStruct

@doc false
Expand Down
2 changes: 1 addition & 1 deletion lib/type/body_json_map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Antikythera.BodyJsonMap do
- `min_size`: The minimum size of the map. If not specified, there is no minimum size.
- `max_size`: The maximum size of the map. If not specified, there is no maximum size.
"""
alias Antikythera.BaseParamStruct
alias AntikytheraCore.BaseParamStruct
alias Antikythera.BodyJsonStruct

@doc false
Expand Down
4 changes: 2 additions & 2 deletions lib/type/body_json_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ defmodule Antikythera.BodyJsonStruct do
The preprocessor must be specified as a capture form like `&module.function/1`.
aYukiSekiguchi marked this conversation as resolved.
Show resolved Hide resolved
"""

alias Antikythera.BaseParamStruct
alias AntikytheraCore.BaseParamStruct

defmodule Preprocessor do
@moduledoc false
Expand Down Expand Up @@ -166,7 +166,7 @@ defmodule Antikythera.BodyJsonStruct do
&Antikythera.BodyJsonStruct.Preprocessor.default/1
)

use Antikythera.BaseParamStruct, opts_with_default_preprocessor
use AntikytheraCore.BaseParamStruct, opts_with_default_preprocessor
end
end
end
2 changes: 1 addition & 1 deletion lib/type/param_string_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ defmodule Antikythera.ParamStringStruct do
&Antikythera.ParamStringStruct.Preprocessor.default/1
)

use Antikythera.BaseParamStruct, opts_with_default_preprocessor
use AntikytheraCore.BaseParamStruct, opts_with_default_preprocessor
end
end
end
2 changes: 1 addition & 1 deletion lib/web/plug/params_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ defmodule Antikythera.Plug.ParamsValidator do
conn :: v[Conn.t()],
param_type :: parameter_type_t(),
validator :: v[module()],
params :: Antikythera.BaseParamStruct.params_t()
params :: AntikytheraCore.BaseParamStruct.params_t()
) :: v[Conn.t()] do
case validator.from_params(params) do
{:ok, validated_params} ->
Expand Down
2 changes: 1 addition & 1 deletion test/lib/type/base_param_struct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Croma

defmodule Antikythera.BaseParamStructTest do
defmodule AntikytheraCore.BaseParamStructTest do
use Croma.TestCase

defmodule TestStruct1 do
Expand Down