Skip to content

Commit

Permalink
feat: Check if it is valid name
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Borsa <borsa.dado@gmail.com>
  • Loading branch information
edborsa committed Dec 13, 2023
1 parent be5b137 commit c2f46e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/beacon/types/site.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/beacon/types/site_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c2f46e7

Please sign in to comment.