From 324d041d5c5cbcdf0083dcd802144a57443789f6 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Wed, 1 Feb 2023 11:58:57 -0700 Subject: Fix upsert on discord id conflict --- lib/chessh/schema/player.ex | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'lib/chessh/schema/player.ex') diff --git a/lib/chessh/schema/player.ex b/lib/chessh/schema/player.ex index 9c83349..dcb3548 100644 --- a/lib/chessh/schema/player.ex +++ b/lib/chessh/schema/player.ex @@ -35,11 +35,19 @@ defmodule Chessh.Player do |> cast(attrs, [:authentications]) end + def discord_changeset(player, attrs) do + player + |> cast(attrs, [:username, :discord_id]) + |> validate_username() + |> validate_discord_id() + end + def registration_changeset(player, attrs, opts \\ []) do player |> cast(attrs, [:username, :password, :discord_id]) |> validate_username() |> validate_password(opts) + |> validate_discord_id() end def password_changeset(player, attrs, opts \\ []) do @@ -67,13 +75,16 @@ defmodule Chessh.Player do end end + defp validate_discord_id(changeset) do + changeset + |> unique_constraint(:discord_id) + end + defp validate_username(changeset) do changeset |> validate_required([:username]) - |> validate_length(:username, min: 2, max: 16) - |> validate_format(:username, ~r/^[a-zA-Z0-9_\-]*$/, - message: "only letters, numbers, underscores, and hyphens allowed" - ) + |> validate_length(:username, min: 2, max: 40) + |> validate_format(:username, ~r/^.{3,32}#[0-9]{4}$/, message: "must match discord tag format") |> unique_constraint(:username) end -- cgit v1.2.3-70-g09d2