refactor: removed TypeCheck

- replaced `TypeCheck` with `AssertType` this function will assert

 - add : `ValidateType` this function will return Boolean and error message
This commit is contained in:
Metee Tanathip
2024-02-12 04:14:09 +07:00
parent d98a3af265
commit 6b45a1a597
+20 -9
View File
@@ -107,24 +107,35 @@ function ESX.Round(value, numDecimalPlaces)
return ESX.Math.Round(value, numDecimalPlaces)
end
function ESX.TypeCheck(value, ...)
function ESX.ValidateType(value, ...)
local types = { ... }
if (#types == 0) then return true end
local mapType = {}
for i = 1, #types, 1 do
mapType[types[i]] = true
local validateType = types[i]
assert(type(validateType) == "string", "bad argument types, only expected string") -- should never use anyhing else than string
mapType[validateType] = true
end
local valueType = type(value)
local requireTypes = table.concat(types, " or ")
local errorMessage = ("bad value (%s expected, got %s)"):format(requireTypes, valueType)
local matches = mapType[valueType] ~= nil
-- Need feedback on this one, should we assert or return false and error message?
-- i prefer assert, but i'm open to suggestions
local matches = (mapType[valueType] ~= nil)
if not (matches) then
local requireTypes = table.concat(types, " or ")
local errorMessage = ("bad value (%s expected, got %s)"):format(requireTypes, valueType)
return false, errorMessage
end
return true
end
function ESX.AssertType(...)
local matches, errorMessage = ESX.ValidateType(...)
assert(matches, errorMessage)
return matches, errorMessage
return matches
end