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

34 lines
859 B
Elixir
Raw Normal View History

defmodule MatrixServerWeb.Client.Request.Register do
2021-06-26 20:02:18 +00:00
use Ecto.Schema
import Ecto.Changeset
alias Ecto.Changeset
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