mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 01:08:54 +00:00
Merge pull request #1309 from CENSOR1337/main
feat: add TypeCheck to check value types
This commit is contained in:
@@ -106,3 +106,36 @@ end
|
||||
function ESX.Round(value, numDecimalPlaces)
|
||||
return ESX.Math.Round(value, numDecimalPlaces)
|
||||
end
|
||||
|
||||
function ESX.ValidateType(value, ...)
|
||||
local types = { ... }
|
||||
if #types == 0 then return true end
|
||||
|
||||
local mapType = {}
|
||||
for i = 1, #types, 1 do
|
||||
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 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
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user