* luci/libs: uvl - cleanup round #2, reinitroduce validate_section() and validate_option()
This commit is contained in:
parent
d7b95a555b
commit
ef0be82b31
1 changed files with 68 additions and 42 deletions
|
@ -51,21 +51,6 @@ function UVL.__init__( self, schemedir )
|
|||
self.datatypes = luci.uvl.datatypes
|
||||
end
|
||||
|
||||
|
||||
function UVL._scheme_section( self, uci, c, s )
|
||||
if self.packages[c] and uci[s] then
|
||||
return self.packages[c].sections[uci[s][".type"]]
|
||||
end
|
||||
end
|
||||
|
||||
function UVL._scheme_option( self, uci, c, s, o )
|
||||
if self.packages[c] and uci[s] and uci[s][o] then
|
||||
return self.packages[c].variables[uci[s][".type"]][o]
|
||||
elseif self.packages[c] and self.packages[c].variables[s] then
|
||||
return self.packages[c].variables[s][o]
|
||||
end
|
||||
end
|
||||
|
||||
function UVL._keys( self, tbl )
|
||||
local keys = { }
|
||||
if tbl then
|
||||
|
@ -122,6 +107,37 @@ function UVL.validate( self, config )
|
|||
return true, nil
|
||||
end
|
||||
|
||||
function UVL.validate_section( self, config, section )
|
||||
self.uci.set_confdir( self.uci.confdir_default )
|
||||
self.uci.load( config )
|
||||
|
||||
local co = self.uci.get_all( config )
|
||||
if co[section] then
|
||||
return self:_validate_section( luci.uvl.section(
|
||||
self, co, co[section]['.type'], config, section
|
||||
) )
|
||||
else
|
||||
return false, "Section '" .. config .. '.' .. section ..
|
||||
"' not found in config. Nothing to do."
|
||||
end
|
||||
end
|
||||
|
||||
function UVL.validate_option( self, config, section, option )
|
||||
self.uci.set_confdir( self.uci.confdir_default )
|
||||
self.uci.load( config )
|
||||
|
||||
local co = self.uci.get_all( config )
|
||||
if co[section] and co[section][option] then
|
||||
return self:_validate_option( luci.uvl.option(
|
||||
self, co, co[section]['.type'], config, section, option
|
||||
) )
|
||||
else
|
||||
return false, "Option '" ..
|
||||
config .. '.' .. section .. '.' .. option ..
|
||||
"' not found in config. Nothing to do."
|
||||
end
|
||||
end
|
||||
|
||||
--- Validate given section of given configuration.
|
||||
-- @param config Name of the configuration to validate
|
||||
-- @param section Key of the section to validate
|
||||
|
@ -172,11 +188,14 @@ end
|
|||
-- @return String containing the reason for errors (if any)
|
||||
function UVL._validate_option( self, option, nodeps )
|
||||
|
||||
if not option:option() then
|
||||
if not option:option() and
|
||||
not ( option:section() and option:section().dynamic )
|
||||
then
|
||||
return false, "Requested option '" .. option:sid() ..
|
||||
"' not found in scheme"
|
||||
end
|
||||
|
||||
if option:option() then
|
||||
if option:option().required and not option:value() then
|
||||
return false, "Mandatory variable '" .. option:cid() ..
|
||||
"' doesn't have a value"
|
||||
|
@ -196,7 +215,9 @@ function UVL._validate_option( self, option, nodeps )
|
|||
|
||||
if option:option().datatype and option:value() then
|
||||
if self.datatypes[option:option().datatype] then
|
||||
if not self.datatypes[option:option().datatype](option:value()) then
|
||||
if not self.datatypes[option:option().datatype](
|
||||
option:value()
|
||||
) then
|
||||
return false, "Value '" .. ( option:value() or '<nil>' ) ..
|
||||
"' of given option '" .. option:cid() ..
|
||||
"' doesn't validate as datatype '" ..
|
||||
|
@ -211,6 +232,7 @@ function UVL._validate_option( self, option, nodeps )
|
|||
if not nodeps then
|
||||
return luci.uvl.dependencies.check( self, option )
|
||||
end
|
||||
end
|
||||
|
||||
return true, nil
|
||||
end
|
||||
|
@ -561,3 +583,7 @@ end
|
|||
function option.option(self)
|
||||
return self.soption
|
||||
end
|
||||
|
||||
function option.section(self)
|
||||
return self.scheme.packages[self.sref[1]].sections[self.sref[2]]
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue