From c2f46e72b6cd91bca4675c9f53d06d180b50600d Mon Sep 17 00:00:00 2001 From: Eduardo Borsa Date: Wed, 13 Dec 2023 18:06:48 -0300 Subject: [PATCH] feat: Check if it is valid name Signed-off-by: Eduardo Borsa --- lib/beacon/types/site.ex | 8 ++++++++ test/beacon/types/site_test.exs | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/beacon/types/site.ex b/lib/beacon/types/site.ex index 3175de36..30bd0d13 100644 --- a/lib/beacon/types/site.ex +++ b/lib/beacon/types/site.ex @@ -36,6 +36,14 @@ defmodule Beacon.Types.Site do def valid?(_site), do: false + def valid_name?(site) when is_atom(site) do + valid_name?(Atom.to_string(site)) + end + + def valid_name?(site) when is_binary(site) do + not String.starts_with?(site, "beacon_") + end + @doc false def safe_to_atom(site) when is_atom(site), do: site def safe_to_atom(site) when is_binary(site), do: String.to_existing_atom(site) diff --git a/test/beacon/types/site_test.exs b/test/beacon/types/site_test.exs index 924e4fb0..f30296ee 100644 --- a/test/beacon/types/site_test.exs +++ b/test/beacon/types/site_test.exs @@ -24,4 +24,14 @@ defmodule Beacon.Types.SiteTest do assert Site.load("site") == {:ok, :site} assert Site.load(0) == :error end + + describe "valid_name?/1" do + test "SUCCESS: Return TRUE if it is a valid name" do + assert Site.valid_name?("some_name") + end + + test "SUCCESS: Return FALSE if it is an invalid name" do + refute Site.valid_name?("beacon_some_name") + end + end end