ConfigValidator
The config validator interface helps you to easily validate your config files by given rule sets.
Example
server.lua
-- A demo config
local demo_config = {
Title = 'Demo Config',
Description = 'This is a demo config.',
DemoCount = 12,
DemoMoney = 100.10,
DemoObject = {
a = 1,
b = 2,
},
DemoList = {
'a',
'b',
'c',
},
DemoItem = 'coal',
DemoRadius = 10.2,
DemoObjectWithChildren = {
{
a = 1,
b = 2,
},
{
a = 1,
b = 2,
},
{
a = 1,
b = 2,
},
}
}
-- A demo rule set
---@type NssLibsServerConfigValidatorRuleSet
local demo_rule_set = {
demo_object_rule = {
type = 'table',
required = true,
contains = {
a = {
type = 'integer',
required = true,
},
b = {
type = 'integer',
required = true,
},
}
},
config_rule = {
Title = {
type = 'string',
required = true,
custom_validation = function(value, rule, error_cb)
if not value and value ~= 'SOMETHING' then
error_cb('The value does not contain something.')
return false
end
return true
end
},
Description = {
type = 'string',
required = true,
},
DemoCount = {
type = 'integer',
required = true,
},
DemoMoney = {
type = 'number',
required = true,
},
DemoObject = {
type = 'table',
required = true,
contains = 'demo_object_rule',
},
DemoList = {
type = 'table',
required = true,
array = true,
},
DemoItem = {
type = 'item',
required = true,
},
DemoRadius = {
type = 'float',
required = true,
},
DemoObjectWithChildren = {
type = 'table',
required = true,
array = true,
contains = 'demo_object_rule',
},
}
}
-- Getting the api
---@type NssLibsServerConfigValidatorApi
local config_validator_api = exports.nss_libs:getConfigValidatorApi(GetCurrentResourceName())
-- Creating an instance
---@type NssLibsServerConfigValidatorInstanceApi
local config_validator_api_instance = config_validator_api.create(demo_rule_set)
-- Name of the starting value
local path = 'Config'
local result = config_validator_api_instance:isValid(
demo_config,
demo_rule_set.config_rule,
path
)
print('Result', result)Public interface methods (NssLibsServerConfigValidatorApi)
NssLibsServerConfigValidatorApi).create(rule_set)
.create(rule_set)Creates a resource related instance of its api NssLibsServerConfigValidatorInstanceApi.
rule_set(NssLibsServerConfigValidatorRuleSet) - The rule set for the validator.
Returns NssLibsServerConfigValidatorInstanceApi on success otherwise nil on invalid rule set.
Public interface methods (NssLibsServerConfigValidatorApi)
NssLibsServerConfigValidatorApi).isValid(value, rule, path)
.isValid(value, rule, path)Checks the given value (recursive) by the given starting rule and returns true if the value is valid.
value(any) - The value to check.rule(NssLibsServerConfigValidatorRule) - The (starting) rule to be applied to the value.path(string) - The path to the value (used for error messages), e.g. the name of the value.
Returns boolean.
.destroy()
.destroy()Destroys the instance of the NssLibsServerConfigValidatorApi api.
Returns nothing.
Rules in details
Basic rule object
Dev notes
Nothing ;)
Last updated
Was this helpful?