architex/lib/architex_web/client/request/register.ex

42 lines
1 KiB
Elixir
Raw Normal View History

2021-09-01 12:43:55 +00:00
defmodule ArchitexWeb.Client.Request.Register do
2021-06-26 20:02:18 +00:00
use Ecto.Schema
import Ecto.Changeset
alias Ecto.Changeset
@type t :: %__MODULE__{
device_id: String.t(),
initial_device_display_name: String.t(),
password: String.t(),
username: String.t(),
inhibit_login: boolean()
}
2021-06-27 15:28:28 +00:00
@primary_key false
2021-06-26 20:02:18 +00:00
embedded_schema do
field :device_id, :string
field :initial_device_display_name, :string
field :password, :string
field :username, :string
field :inhibit_login, :boolean, default: false
end
def changeset(params) do
2021-06-27 15:28:28 +00:00
%__MODULE__{}
2021-06-26 20:02:18 +00:00
|> cast(params, [
:device_id,
:initial_device_display_name,
:password,
:username,
:inhibit_login
])
2021-07-13 17:35:02 +00:00
|> validate_required([:password])
2021-06-26 20:02:18 +00:00
end
2021-07-10 21:16:00 +00:00
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
def get_error({:localpart, {_, [{:constraint, :unique} | _]}}), do: :user_in_use
def get_error({:localpart, {_, [{:validation, _} | _]}}), do: :invalid_username
def get_error(_), do: :bad_json
2021-06-26 20:02:18 +00:00
end