Make user bio optional

This commit is contained in:
Ekaterina Vaartis 2018-02-21 23:31:57 +03:00
parent eec6a87dbb
commit ec07e087bc
2 changed files with 6 additions and 5 deletions

View File

@ -93,7 +93,7 @@ def update_changeset(struct, params \\ %{}) do
|> cast(params, [:bio, :name]) |> cast(params, [:bio, :name])
|> unique_constraint(:nickname) |> unique_constraint(:nickname)
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|> validate_length(:bio, min: 1, max: 1000) |> validate_length(:bio, max: 1000)
|> validate_length(:name, min: 1, max: 100) |> validate_length(:name, min: 1, max: 100)
end end
@ -119,13 +119,13 @@ def reset_password(user, data) do
def register_changeset(struct, params \\ %{}) do def register_changeset(struct, params \\ %{}) do
changeset = struct changeset = struct
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
|> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation]) |> validate_required([:email, :name, :nickname, :password, :password_confirmation])
|> validate_confirmation(:password) |> validate_confirmation(:password)
|> unique_constraint(:email) |> unique_constraint(:email)
|> unique_constraint(:nickname) |> unique_constraint(:nickname)
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|> validate_format(:email, @email_regex) |> validate_format(:email, @email_regex)
|> validate_length(:bio, min: 1, max: 1000) |> validate_length(:bio, max: 1000)
|> validate_length(:name, min: 1, max: 100) |> validate_length(:name, min: 1, max: 100)
if changeset.valid? do if changeset.valid? do

View File

@ -101,13 +101,14 @@ test "test if a user is following another user" do
email: "email@example.com" email: "email@example.com"
} }
test "it requires a bio, email, name, nickname and password" do test "it requires an email, name, nickname and password, bio is optional" do
@full_user_data @full_user_data
|> Map.keys |> Map.keys
|> Enum.each(fn (key) -> |> Enum.each(fn (key) ->
params = Map.delete(@full_user_data, key) params = Map.delete(@full_user_data, key)
changeset = User.register_changeset(%User{}, params) changeset = User.register_changeset(%User{}, params)
assert changeset.valid? == false
assert (if key == :bio, do: changeset.valid?, else: not changeset.valid?)
end) end)
end end