refactor(es_extended/server/functions): Refactor command argument validation logic

This commit is contained in:
Kenshin13
2025-08-31 16:21:46 +02:00
committed by _Not_
parent b777daccd6
commit 9ed99a8d7c
2 changed files with 7 additions and 6 deletions
+3 -3
View File
@@ -147,11 +147,11 @@ function ESX.RegisterCommand(name, group, cb, allowConsole, suggestion)
end
end
if v.isValid and not err then
if ESX.IsFunctionReference(v.Validator?.validate) and not err then
local candidate = newArgs[v.name]
local ok, res = pcall(v.isValid, candidate)
local ok, res = pcall(v.Validator.validate, candidate)
if not ok or res ~= true then
err = v.err or TranslateCap("commanderror_argumentmismatch")
err = v.Validator.err or TranslateCap("commanderror_argumentmismatch")
end
end
@@ -313,9 +313,10 @@ if not Config.CustomInventory then
arguments = {
{ name = "playerId", help = TranslateCap("commandgeneric_playerid"), type = "player" },
{ name = "item", help = TranslateCap("command_giveitem_item"), type = "item" },
{ name = "count", help = TranslateCap("command_giveitem_count"), type = "number", isValid = function (countValue)
return countValue > 0
end, err = TranslateCap("commanderror_argumentmismatch_positive_number", "count")},
{ name = "count", help = TranslateCap("command_giveitem_count"), type = "number", Validator = {
validate = function(x) return x > 0 end,
err = TranslateCap("commanderror_argumentmismatch_positive_number", "count")
}},
},
}
)