Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ defmodule CreateAccount do
use Commanded.Command,
username: :string,
email: :string,
age: :integer
age: {:integer, default: 0},
aliases: {{:array, :string}} # Composite Type requires extra brace to avoid being interprated as opts
end

iex> CreateAccount.new()
iex> CreateAccount.new()
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #CreateAccount<>, valid?: true>
```

Expand All @@ -54,7 +55,7 @@ defmodule CreateAccount do
end
end

iex> CreateAccount.new()
iex> CreateAccount.new()
#Ecto.Changeset<
action: nil,
changes: %{},
Expand All @@ -67,7 +68,7 @@ iex> CreateAccount.new()
valid?: false
>

iex> changeset = CreateAccount.new(username: "chris", email: "[email protected]", age: 5)
iex> changeset = CreateAccount.new(username: "chris", email: "[email protected]", age: 5)
#Ecto.Changeset<
action: nil,
changes: %{age: 5, email: "[email protected]", username: "chris"},
Expand Down Expand Up @@ -149,7 +150,7 @@ iex> event = AccountCreated.new(cmd)

You may have noticed that we provide a default version of `1`.

You can change the version of an event at anytime.
You can change the version of an event at anytime.

After doing so, you should define an upcast instance that knows how to transform older events into the latest version.

Expand Down
14 changes: 10 additions & 4 deletions lib/commanded/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ defmodule Commanded.Command do
use Commanded.Command,
username: :string,
email: :string,
age: :integer
age: :integer,
aliases: {{:array, :string}}

def handle_validate(changeset) do
changeset
|> validate_required([:username, :email, :age])
|> validate_required([:username, :email, :age, :aliases])
|> validate_format(:email, ~r/@/)
|> validate_number(:age, greater_than: 12)
end
end

iex> CreateAccount.new(username: "chris", email: "[email protected]", age: 5)
#Ecto.Changeset<action: nil, changes: %{age: 5, email: "[email protected]", username: "chris"}, errors: [age: {"must be greater than %{number}", [validation: :number, kind: :greater_than, number: 12]}], data: #CreateAccount<>, valid?: false>
iex> CreateAccount.new(username: "chris", email: "[email protected]", age: 5, aliases: ["christopher", "kris"])
#Ecto.Changeset<action: nil, changes: %{age: 5, aliases: ["christopher", "kris"], email: "[email protected]", username: "chris"}, errors: [age: {"must be greater than %{number}", [validation: :number, kind: :greater_than, number: 12]}], data: #CreateAccount<>, valid?: false>
"""

@doc """
Expand All @@ -35,6 +36,8 @@ defmodule Commanded.Command do
@primary_key false
embedded_schema do
Enum.map(unquote(schema), fn
{name, {{_, _} = composite_type, opts}} -> field(name, field_type(composite_type), opts)
{name, {{_, _} = composite_type}} -> field(name, field_type(composite_type))
{name, {type, opts}} -> field(name, field_type(type), opts)
{name, type} -> field(name, field_type(type))
end)
Expand All @@ -60,5 +63,8 @@ defmodule Commanded.Command do
end

def field_type(:binary_id), do: Ecto.UUID
def field_type(:array) do
raise "`:array` is not a valid Ecto.Type\nIf you are using a composite data type, wrap the type definition like this `{{:array, :string}}`"
end
def field_type(type), do: type
end
3 changes: 2 additions & 1 deletion test/support/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ defmodule CreateAccount do
use Commanded.Command,
username: :string,
email: :string,
age: :integer
age: :integer,
aliases: {{:array, :string}}

def handle_validate(changeset) do
changeset
Expand Down