Last active
December 19, 2019 18:27
-
-
Save enerick/5d438ad3c383baa828c976cb095a5900 to your computer and use it in GitHub Desktop.
introduction to params (v2.1.2?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# モジュール内定義 | |
defmodule M do | |
use Params | |
defparams person_params(%{ | |
name!: :string, | |
age: :integer | |
}) | |
end | |
# 別モジュール定義 | |
defmodule PersonParams do | |
use Params.Schema, %{ | |
name!: :string, | |
age: :integer | |
} | |
end | |
# 利用 | |
M.person_params(%{"name" => "ジョン", "age" => 18}) | |
PersonParams.from(%{"name" => "ジョン", "age" => 18}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# age は 18~200 であることをバリデート | |
defmodule PersonParams do | |
use Params.Schema, %{name!: :string, age: :integer} | |
def changeset(ch, params) do | |
# defoverride が定義されているので、実は super が使える | |
super(ch, params) | |
|> validate_inclusion(:age, 18..200) | |
end | |
end | |
defmodule M do | |
use Params | |
# defparams でもカスタムバリデータが実は使える | |
defparams person_params(%{name!: :string, age: :integer}) do | |
def changeset(ch, params) do | |
super(ch, params) | |
|> Ecto.Changeset.validate_inclusion(:age, 18..200) | |
end | |
end | |
end | |
# 利用 | |
PersonParams.from(%{"name" => "ジョン", "age" => 1}) # NG | |
PersonParams.from(%{"name" => "ジョン", "age" => 18}) # OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule M do | |
use Params | |
@pet %{name!: :string, category!: [field: :string, default: "dog"]} | |
defparams person_params(%{ | |
name!: :string, | |
age: :integer, | |
# 定数から定義を展開 | |
pets!: [@pet] | |
}) | |
end | |
# 利用 | |
M.person_params(%{"name" => "ジョン"}) # NG | |
M.person_params(%{"name" => "ジョン", "pets" => [%{"name" => "hoge"}]}) # OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule M do | |
use Params | |
import Ecto.Changeset, only: [validate_inclusion: 3] | |
defparams pet_params(%{name!: :string, category!: [field: :string, default: "dog"]}) do | |
def changeset(ch, params) do | |
super(ch, params) | |
|> validate_inclusion(:category, ~w(dog cat)) | |
end | |
end | |
defparams person_params(%{ | |
name!: :string, | |
age: :integer, | |
# 実は defparams の定義を再利用できる | |
pets: {:embeds_many, Params.M.PetParams} | |
}) | |
end | |
# 利用 | |
M.person_params(%{"pets" => [%{"category" => "bird"}]}) # NG | |
M.person_params(%{"name" => "ジョン", "pets" => [%{"name" => "hoge", "category" => "cat"}]}) # OK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment