Skip to content

Commit

Permalink
adds util to validate keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
vilenarios committed Oct 16, 2024
1 parent 15f997f commit db00472
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
18 changes: 1 addition & 17 deletions src/common/balances.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,7 @@ function balances.setDescription(description)
end

function balances.setKeywords(keywords)
assert(type(keywords) == "table", "Keywords must be an array")
assert(#keywords <= 16, "There must not be more than 16 keywords")

local seenKeywords = {} -- Table to track seen keywords

for _, keyword in ipairs(keywords) do
assert(type(keyword) == "string", "Each keyword must be a string")
assert(#keyword <= 32, "Each keyword must not be longer than 32 characters")
assert(not keyword:find("%s"), "Keywords must not contain spaces")
assert(
keyword:match("^[%w-_#@]+$"),
"Keywords must only contain alphanumeric characters, dashes, underscores, #, or @"
)
-- Check for duplicates
assert(not seenKeywords[keyword], "Duplicate keyword detected: " .. keyword)
seenKeywords[keyword] = true
end
utils.validateKeywords(keywords)

Keywords = keywords
return json.encode({ keywords = Keywords })
Expand Down
13 changes: 1 addition & 12 deletions src/common/initialize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,7 @@ function initialize.initializeANTState(state)
utils.validateTTLSeconds(v.ttlSeconds)
end

assert(type(keywords) == "table", "Keywords must be an array")
assert(#keywords <= 16, "There must not be more than 16 keywords")

local seenKeywords = {}
for _, keyword in ipairs(keywords) do
assert(type(keyword) == "string", "Each keyword must be a string")
assert(#keyword <= 32, "Each keyword must not be longer than 32 characters")
assert(not keyword:find("%s"), "Keywords must not contain spaces")
assert(keyword:match("^[%w-_]+$"), "Keywords must only contain alphanumeric characters, dashes, or underscores")
assert(not seenKeywords[keyword], "Duplicate keyword detected: " .. keyword)
seenKeywords[keyword] = true
end
utils.validateKeywords(keywords)

Name = name
Ticker = ticker
Expand Down
20 changes: 20 additions & 0 deletions src/common/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,24 @@ function utils.getHandlerNames(handlers)
return names
end

function utils.validateKeywords(keywords)
assert(type(keywords) == "table", "Keywords must be an array")
assert(#keywords <= 16, "There must not be more than 16 keywords")

local seenKeywords = {} -- Table to track seen keywords

for _, keyword in ipairs(keywords) do
assert(type(keyword) == "string", "Each keyword must be a string")
assert(#keyword <= 32, "Each keyword must not be longer than 32 characters")
assert(not keyword:find("%s"), "Keywords must not contain spaces")
assert(
keyword:match("^[%w-_#@]+$"),
"Keywords must only contain alphanumeric characters, dashes, underscores, #, or @"
)
-- Check for duplicates
assert(not seenKeywords[keyword], "Duplicate keyword detected: " .. keyword)
seenKeywords[keyword] = true
end
end

return utils

0 comments on commit db00472

Please sign in to comment.