3 UCI Validation Layer - Main Library
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 module( "luci.uvl", package.seeall )
21 require("luci.model.uci")
22 require("luci.uvl.datatypes")
23 --require("luci.uvl.validation")
24 require("luci.uvl.dependencies")
30 STRICT_UNKNOWN_SECTIONS = true
31 STRICT_UNKNOWN_OPTIONS = true
34 local default_schemedir = "/etc/scheme"
36 local function _assert( condition, fmt, ... )
38 return assert( nil, string.format( fmt, ... ) )
44 UVL = luci.util.class()
46 function UVL.__init__( self, schemedir )
47 self.schemedir = schemedir or default_schemedir
50 self.uci = luci.model.uci
51 self.datatypes = luci.uvl.datatypes
54 function UVL._keys( self, tbl )
57 for k, _ in luci.util.kspairs(tbl) do
58 table.insert( keys, k )
65 --- Validate given configuration.
66 -- @param config Name of the configuration to validate
67 -- @param scheme Scheme to validate against (optional)
68 -- @return Boolean indicating weather the given config validates
69 -- @return String containing the reason for errors (if any)
70 function UVL.validate( self, config )
72 self.uci.load_config( config )
75 local co = self.uci.get_all( config )
77 local function _uci_foreach( type, func )
79 for k, v in pairs(co) do
80 if co[k]['.type'] == type then
81 ok, err = func( k, v )
82 if not ok then break end
88 for k, v in pairs( self.packages[config].sections ) do
89 local ok, err = _uci_foreach( k,
91 local sect = luci.uvl.section( self, co, k, config, s )
92 return self:_validate_section( sect )
95 if not ok then return false, err end
98 if STRICT_UNKNOWN_SECTIONS then
99 for k, v in pairs(co) do
100 if not self.beenthere[config..'.'..k] then
101 return false, "Section '" .. config .. '.' .. co[k]['.type'] ..
102 "' not found in scheme"
110 function UVL.validate_section( self, config, section )
111 self.uci.load_config( config )
114 local co = self.uci.get_all( config )
116 return self:_validate_section( luci.uvl.section(
117 self, co, co[section]['.type'], config, section
120 return false, "Section '" .. config .. '.' .. section ..
121 "' not found in config. Nothing to do."
125 function UVL.validate_option( self, config, section, option )
126 self.uci.load_config( config )
129 local co = self.uci.get_all( config )
130 if co[section] and co[section][option] then
131 return self:_validate_option( luci.uvl.option(
132 self, co, co[section]['.type'], config, section, option
135 return false, "Option '" ..
136 config .. '.' .. section .. '.' .. option ..
137 "' not found in config. Nothing to do."
141 --- Validate given section of given configuration.
142 -- @param config Name of the configuration to validate
143 -- @param section Key of the section to validate
144 -- @param scheme Scheme to validate against
145 -- @return Boolean indicating weather the given config validates
146 -- @return String containing the reason for errors (if any)
147 function UVL._validate_section( self, section )
149 if section:values() then
151 for _, v in ipairs(section:variables()) do
152 local ok, err = self:_validate_option( v )
159 local ok, err = luci.uvl.dependencies.check( self, section )
162 return false, "All possible dependencies failed"
165 print( "Error, scheme section '" .. section:sid() .. "' not found in data" )
168 if STRICT_UNKNOWN_OPTIONS and not section:section().dynamic then
169 for k, v in pairs(section:values()) do
170 if k:sub(1,1) ~= "." and not self.beenthere[
171 section:cid() .. '.' .. k
173 return false, "Option '" .. section:sid() .. '.' .. k ..
174 "' not found in scheme"
182 --- Validate given option within section of given configuration.
183 -- @param config Name of the configuration to validate
184 -- @param section Key of the section to validate
185 -- @param option Name of the option to validate
186 -- @param scheme Scheme to validate against
187 -- @return Boolean indicating weather the given config validates
188 -- @return String containing the reason for errors (if any)
189 function UVL._validate_option( self, option, nodeps )
191 if not option:option() and
192 not ( option:section() and option:section().dynamic )
194 return false, "Requested option '" .. option:sid() ..
195 "' not found in scheme"
198 if option:option() then
199 if option:option().required and not option:value() then
200 return false, "Mandatory variable '" .. option:cid() ..
201 "' doesn't have a value"
204 if option:option().type == "enum" and option:value() then
205 if not option:option().values or
206 not option:option().values[option:value()]
208 return false, "Value '" .. ( option:value() or '<nil>' ) ..
209 "' of given option '" .. option:cid() ..
210 "' is not defined in enum { " ..
211 table.concat(self:_keys(option:option().values),", ") ..
216 if option:option().datatype and option:value() then
217 if self.datatypes[option:option().datatype] then
218 if not self.datatypes[option:option().datatype](
221 return false, "Value '" .. ( option:value() or '<nil>' ) ..
222 "' of given option '" .. option:cid() ..
223 "' doesn't validate as datatype '" ..
224 option:option().datatype .. "'"
227 return false, "Unknown datatype '" ..
228 option:option().datatype .. "' encountered"
233 return luci.uvl.dependencies.check( self, option )
240 --- Find all parts of given scheme and construct validation tree
241 -- @param scheme Name of the scheme to parse
242 -- @return Parsed scheme
243 function UVL.read_scheme( self, scheme )
246 for i, file in ipairs( luci.fs.glob(self.schemedir .. '/*/' .. scheme) ) do
247 _assert( luci.fs.access(file), "Can't access file '%s'", file )
249 self.uci.set_confdir( luci.fs.dirname(file) )
250 self.uci.load( luci.fs.basename(file) )
252 table.insert( schemes, self.uci.get_all( luci.fs.basename(file) ) )
255 return self:_read_scheme_parts( scheme, schemes )
258 -- Process all given parts and construct validation tree
259 function UVL._read_scheme_parts( self, scheme, schemes )
261 -- helper function to construct identifiers for given elements
262 local function _id( c, t )
263 if c == TYPE_SECTION then
264 return string.format(
266 scheme, t.name or '?' )
267 elseif c == TYPE_VARIABLE then
268 return string.format(
269 "variable '%s.%s.%s'",
270 scheme, t.section or '?.?', t.name or '?' )
271 elseif c == TYPE_ENUM then
272 return string.format(
274 scheme, t.variable or '?.?.?', t.value or '?' )
278 -- helper function to check for required fields
279 local function _req( c, t, r )
280 for i, v in ipairs(r) do
281 _assert( t[v], "Missing required field '%s' in %s", v, _id(c, t) )
285 -- helper function to validate references
286 local function _ref( c, t )
288 if c == TYPE_SECTION then
290 elseif c == TYPE_VARIABLE then
292 elseif c == TYPE_ENUM then
296 local r = luci.util.split( t[k], "." )
297 r[1] = ( #r[1] > 0 and r[1] or scheme )
299 _assert( #r == c, "Malformed %s reference in %s", k, _id(c, t) )
304 -- Step 1: get all sections
305 for i, conf in ipairs( schemes ) do
306 for k, v in pairs( conf ) do
307 if v['.type'] == 'section' then
309 _req( TYPE_SECTION, v, { "name", "package" } )
311 local r = _ref( TYPE_SECTION, v )
313 self.packages[r[1]] =
314 self.packages[r[1]] or {
319 local p = self.packages[r[1]]
320 p.sections[v.name] = p.sections[v.name] or { }
321 p.variables[v.name] = p.variables[v.name] or { }
323 local s = p.sections[v.name]
325 for k, v2 in pairs(v) do
326 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
327 if k:match("^depends") then
328 s["depends"] = _assert(
329 self:_read_dependency( v2, s["depends"] ),
330 "Section '%s' in scheme '%s' has malformed " ..
331 "dependency specification in '%s'",
332 v.name or '<nil>', scheme or '<nil>', k
343 -- Step 2: get all variables
344 for i, conf in ipairs( schemes ) do
345 for k, v in pairs( conf ) do
346 if v['.type'] == "variable" then
348 _req( TYPE_VARIABLE, v, { "name", "section" } )
350 local r = _ref( TYPE_VARIABLE, v )
352 local p = _assert( self.packages[r[1]],
353 "Variable '%s' in scheme '%s' references unknown package '%s'",
354 v.name, scheme, r[1] )
356 local s = _assert( p.variables[r[2]],
357 "Variable '%s' in scheme '%s' references unknown section '%s'",
358 v.name, scheme, r[2] )
360 s[v.name] = s[v.name] or { }
364 for k, v in pairs(v) do
365 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
366 if k:match("^depends") then
367 t["depends"] = _assert(
368 self:_read_dependency( v, t["depends"] ),
369 "Variable '%s' in scheme '%s' has malformed " ..
370 "dependency specification in '%s'",
373 elseif k:match("^validator") then
374 t["validators"] = _assert(
375 self:_read_validator( v, t["validators"] ),
376 "Variable '%s' in scheme '%s' has malformed " ..
377 "validator specification in '%s'",
389 -- Step 3: get all enums
390 for i, conf in ipairs( schemes ) do
391 for k, v in pairs( conf ) do
392 if v['.type'] == "enum" then
394 _req( TYPE_ENUM, v, { "value", "variable" } )
396 local r = _ref( TYPE_ENUM, v )
398 local p = _assert( self.packages[r[1]],
399 "Enum '%s' in scheme '%s' references unknown package '%s'",
400 v.value, scheme, r[1] )
402 local s = _assert( p.variables[r[2]],
403 "Enum '%s' in scheme '%s' references unknown section '%s'",
404 v.value, scheme, r[2] )
406 local t = _assert( s[r[3]],
407 "Enum '%s' in scheme '%s', section '%s' references " ..
408 "unknown variable '%s'",
409 v.value, scheme, r[2], r[3] )
411 _assert( t.type == "enum",
412 "Enum '%s' in scheme '%s', section '%s' references " ..
413 "variable '%s' with non enum type '%s'",
414 v.value, scheme, r[2], r[3], t.type )
417 t.values = { [v.value] = v.title or v.value }
419 t.values[v.value] = v.title or v.value
423 _assert( not t.default,
424 "Enum '%s' in scheme '%s', section '%s' redeclares " ..
425 "the default value of variable '%s'",
426 v.value, scheme, r[2], v.variable )
437 -- Read a dependency specification
438 function UVL._read_dependency( self, value, deps )
439 local parts = luci.util.split( value, "%s*,%s*", nil, true )
440 local condition = { }
442 for i, val in ipairs(parts) do
443 local k, v = unpack(luci.util.split( val, "%s*=%s*", nil, true ))
446 k:match("^%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+$") or
447 k:match("^%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+$") or
448 k:match("^%$?[a-zA-Z0-9_]+$")
450 condition[k] = v or true
459 table.insert( deps, condition )
465 -- Read a validator specification
466 function UVL._read_validator( self, value, validators )
469 if value and value:match("/") and self.datatypes.file(value) then
472 validator = self:_resolve_function( value )
476 if not validators then
477 validators = { validator }
479 table.insert( validators, validator )
486 -- Resolve given path
487 function UVL._resolve_function( self, value )
488 local path = luci.util.split(value, ".")
491 local stat, mod = pcall(require, table.concat(path, ".", 1, i))
493 for j=i+1, #path-1 do
494 if not type(mod) == "table" then
502 mod = type(mod) == "table" and mod[path[#path]] or nil
503 if type(mod) == "function" then
511 section = luci.util.class()
513 function section.__init__(self, scheme, co, st, c, s)
514 self.csection = co[s]
515 self.ssection = scheme.packages[c].sections[st]
517 self.sref = { c, st }
520 self.type = luci.uvl.TYPE_SECTION
523 function section.cid(self)
524 return ( self.cref[1] or '?' ) .. '.' .. ( self.cref[2] or '?' )
527 function section.sid(self)
528 return ( self.sref[1] or '?' ) .. '.' .. ( self.sref[2] or '?' )
531 function section.values(self)
535 function section.section(self)
539 function section.variables(self)
541 if self.scheme.packages[self.sref[1]].variables[self.sref[2]] then
543 self.scheme.packages[self.sref[1]].variables[self.sref[2]]
545 table.insert( v, luci.uvl.option(
546 self.scheme, self.config, self.sref[2],
547 self.cref[1], self.cref[2], o
555 option = luci.util.class()
557 function option.__init__(self, scheme, co, st, c, s, o)
558 self.coption = co[s] and co[s][o] or nil
559 self.soption = scheme.packages[c].variables[st][o]
560 self.cref = { c, s, o }
561 self.sref = { c, st, o }
564 self.type = luci.uvl.TYPE_OPTION
567 function option.cid(self)
568 return ( self.cref[1] or '?' ) .. '.' ..
569 ( self.cref[2] or '?' ) .. '.' ..
570 ( self.cref[3] or '?' )
573 function option.sid(self)
574 return ( self.sref[1] or '?' ) .. '.' ..
575 ( self.sref[2] or '?' ) .. '.' ..
576 ( self.sref[3] or '?' )
579 function option.value(self)
583 function option.option(self)
587 function option.section(self)
588 return self.scheme.packages[self.sref[1]].sections[self.sref[2]]