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.set_confdir( self.uci.confdir_default )
73 self.uci.load( config )
76 local co = self.uci.get_all( config )
78 local function _uci_foreach( type, func )
80 for k, v in pairs(co) do
81 if co[k]['.type'] == type then
82 ok, err = func( k, v )
83 if not ok then break end
89 for k, v in pairs( self.packages[config].sections ) do
90 local ok, err = _uci_foreach( k,
92 local sect = luci.uvl.section( self, co, k, config, s )
93 return self:_validate_section( sect )
96 if not ok then return false, err end
99 if STRICT_UNKNOWN_SECTIONS then
100 for k, v in pairs(co) do
101 if not self.beenthere[config..'.'..k] then
102 return false, "Section '" .. config .. '.' .. co[k]['.type'] ..
103 "' not found in scheme"
111 function UVL.validate_section( self, config, section )
112 self.uci.set_confdir( self.uci.confdir_default )
113 self.uci.load( config )
116 local co = self.uci.get_all( config )
118 return self:_validate_section( luci.uvl.section(
119 self, co, co[section]['.type'], config, section
122 return false, "Section '" .. config .. '.' .. section ..
123 "' not found in config. Nothing to do."
127 function UVL.validate_option( self, config, section, option )
128 self.uci.set_confdir( self.uci.confdir_default )
129 self.uci.load( config )
132 local co = self.uci.get_all( config )
133 if co[section] and co[section][option] then
134 return self:_validate_option( luci.uvl.option(
135 self, co, co[section]['.type'], config, section, option
138 return false, "Option '" ..
139 config .. '.' .. section .. '.' .. option ..
140 "' not found in config. Nothing to do."
144 --- Validate given section of given configuration.
145 -- @param config Name of the configuration to validate
146 -- @param section Key of the section to validate
147 -- @param scheme Scheme to validate against
148 -- @return Boolean indicating weather the given config validates
149 -- @return String containing the reason for errors (if any)
150 function UVL._validate_section( self, section )
152 if section:values() then
154 for _, v in ipairs(section:variables()) do
155 local ok, err = self:_validate_option( v )
162 local ok, err = luci.uvl.dependencies.check( self, section )
165 return false, "All possible dependencies failed"
168 print( "Error, scheme section '" .. section:sid() .. "' not found in data" )
171 if STRICT_UNKNOWN_OPTIONS and not section:section().dynamic then
172 for k, v in pairs(section:values()) do
173 if k:sub(1,1) ~= "." and not self.beenthere[
174 section:cid() .. '.' .. k
176 return false, "Option '" .. section:sid() .. '.' .. k ..
177 "' not found in scheme"
185 --- Validate given option within section of given configuration.
186 -- @param config Name of the configuration to validate
187 -- @param section Key of the section to validate
188 -- @param option Name of the option to validate
189 -- @param scheme Scheme to validate against
190 -- @return Boolean indicating weather the given config validates
191 -- @return String containing the reason for errors (if any)
192 function UVL._validate_option( self, option, nodeps )
194 if not option:option() and
195 not ( option:section() and option:section().dynamic )
197 return false, "Requested option '" .. option:sid() ..
198 "' not found in scheme"
201 if option:option() then
202 if option:option().required and not option:value() then
203 return false, "Mandatory variable '" .. option:cid() ..
204 "' doesn't have a value"
207 if option:option().type == "enum" and option:value() then
208 if not option:option().values or
209 not option:option().values[option:value()]
211 return false, "Value '" .. ( option:value() or '<nil>' ) ..
212 "' of given option '" .. option:cid() ..
213 "' is not defined in enum { " ..
214 table.concat(self:_keys(option:option().values),", ") ..
219 if option:option().datatype and option:value() then
220 if self.datatypes[option:option().datatype] then
221 if not self.datatypes[option:option().datatype](
224 return false, "Value '" .. ( option:value() or '<nil>' ) ..
225 "' of given option '" .. option:cid() ..
226 "' doesn't validate as datatype '" ..
227 option:option().datatype .. "'"
230 return false, "Unknown datatype '" ..
231 option:option().datatype .. "' encountered"
236 return luci.uvl.dependencies.check( self, option )
243 --- Find all parts of given scheme and construct validation tree
244 -- @param scheme Name of the scheme to parse
245 -- @return Parsed scheme
246 function UVL.read_scheme( self, scheme )
249 for i, file in ipairs( luci.fs.glob(self.schemedir .. '/*/' .. scheme) ) do
250 _assert( luci.fs.access(file), "Can't access file '%s'", file )
252 self.uci.set_confdir( luci.fs.dirname(file) )
253 self.uci.load( luci.fs.basename(file) )
255 table.insert( schemes, self.uci.get_all( luci.fs.basename(file) ) )
258 return self:_read_scheme_parts( scheme, schemes )
261 -- Process all given parts and construct validation tree
262 function UVL._read_scheme_parts( self, scheme, schemes )
264 -- helper function to construct identifiers for given elements
265 local function _id( c, t )
266 if c == TYPE_SECTION then
267 return string.format(
269 scheme, t.name or '?' )
270 elseif c == TYPE_VARIABLE then
271 return string.format(
272 "variable '%s.%s.%s'",
273 scheme, t.section or '?.?', t.name or '?' )
274 elseif c == TYPE_ENUM then
275 return string.format(
277 scheme, t.variable or '?.?.?', t.value or '?' )
281 -- helper function to check for required fields
282 local function _req( c, t, r )
283 for i, v in ipairs(r) do
284 _assert( t[v], "Missing required field '%s' in %s", v, _id(c, t) )
288 -- helper function to validate references
289 local function _ref( c, t )
291 if c == TYPE_SECTION then
293 elseif c == TYPE_VARIABLE then
295 elseif c == TYPE_ENUM then
299 local r = luci.util.split( t[k], "." )
300 r[1] = ( #r[1] > 0 and r[1] or scheme )
302 _assert( #r == c, "Malformed %s reference in %s", k, _id(c, t) )
307 -- Step 1: get all sections
308 for i, conf in ipairs( schemes ) do
309 for k, v in pairs( conf ) do
310 if v['.type'] == 'section' then
312 _req( TYPE_SECTION, v, { "name", "package" } )
314 local r = _ref( TYPE_SECTION, v )
316 self.packages[r[1]] =
317 self.packages[r[1]] or {
322 local p = self.packages[r[1]]
323 p.sections[v.name] = p.sections[v.name] or { }
324 p.variables[v.name] = p.variables[v.name] or { }
326 local s = p.sections[v.name]
328 for k, v2 in pairs(v) do
329 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
330 if k:match("^depends") then
331 s["depends"] = _assert(
332 self:_read_dependency( v2, s["depends"] ),
333 "Section '%s' in scheme '%s' has malformed " ..
334 "dependency specification in '%s'",
335 v.name or '<nil>', scheme or '<nil>', k
346 -- Step 2: get all variables
347 for i, conf in ipairs( schemes ) do
348 for k, v in pairs( conf ) do
349 if v['.type'] == "variable" then
351 _req( TYPE_VARIABLE, v, { "name", "section" } )
353 local r = _ref( TYPE_VARIABLE, v )
355 local p = _assert( self.packages[r[1]],
356 "Variable '%s' in scheme '%s' references unknown package '%s'",
357 v.name, scheme, r[1] )
359 local s = _assert( p.variables[r[2]],
360 "Variable '%s' in scheme '%s' references unknown section '%s'",
361 v.name, scheme, r[2] )
363 s[v.name] = s[v.name] or { }
367 for k, v in pairs(v) do
368 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
369 if k:match("^depends") then
370 t["depends"] = _assert(
371 self:_read_dependency( v, t["depends"] ),
372 "Variable '%s' in scheme '%s' has malformed " ..
373 "dependency specification in '%s'",
376 elseif k:match("^validator") then
377 t["validators"] = _assert(
378 self:_read_validator( v, t["validators"] ),
379 "Variable '%s' in scheme '%s' has malformed " ..
380 "validator specification in '%s'",
392 -- Step 3: get all enums
393 for i, conf in ipairs( schemes ) do
394 for k, v in pairs( conf ) do
395 if v['.type'] == "enum" then
397 _req( TYPE_ENUM, v, { "value", "variable" } )
399 local r = _ref( TYPE_ENUM, v )
401 local p = _assert( self.packages[r[1]],
402 "Enum '%s' in scheme '%s' references unknown package '%s'",
403 v.value, scheme, r[1] )
405 local s = _assert( p.variables[r[2]],
406 "Enum '%s' in scheme '%s' references unknown section '%s'",
407 v.value, scheme, r[2] )
409 local t = _assert( s[r[3]],
410 "Enum '%s' in scheme '%s', section '%s' references " ..
411 "unknown variable '%s'",
412 v.value, scheme, r[2], r[3] )
414 _assert( t.type == "enum",
415 "Enum '%s' in scheme '%s', section '%s' references " ..
416 "variable '%s' with non enum type '%s'",
417 v.value, scheme, r[2], r[3], t.type )
420 t.values = { [v.value] = v.title or v.value }
422 t.values[v.value] = v.title or v.value
426 _assert( not t.default,
427 "Enum '%s' in scheme '%s', section '%s' redeclares " ..
428 "the default value of variable '%s'",
429 v.value, scheme, r[2], v.variable )
440 -- Read a dependency specification
441 function UVL._read_dependency( self, value, deps )
442 local parts = luci.util.split( value, "%s*,%s*", nil, true )
443 local condition = { }
445 for i, val in ipairs(parts) do
446 local k, v = unpack(luci.util.split( val, "%s*=%s*", nil, true ))
449 k:match("^%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+$") or
450 k:match("^%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+$") or
451 k:match("^%$?[a-zA-Z0-9_]+$")
453 condition[k] = v or true
462 table.insert( deps, condition )
468 -- Read a validator specification
469 function UVL._read_validator( self, value, validators )
472 if value and value:match("/") and self.datatypes.file(value) then
475 validator = self:_resolve_function( value )
479 if not validators then
480 validators = { validator }
482 table.insert( validators, validator )
489 -- Resolve given path
490 function UVL._resolve_function( self, value )
491 local path = luci.util.split(value, ".")
494 local stat, mod = pcall(require, table.concat(path, ".", 1, i))
496 for j=i+1, #path-1 do
497 if not type(mod) == "table" then
505 mod = type(mod) == "table" and mod[path[#path]] or nil
506 if type(mod) == "function" then
514 section = luci.util.class()
516 function section.__init__(self, scheme, co, st, c, s)
517 self.csection = co[s]
518 self.ssection = scheme.packages[c].sections[st]
520 self.sref = { c, st }
523 self.type = luci.uvl.TYPE_SECTION
526 function section.cid(self)
527 return ( self.cref[1] or '?' ) .. '.' .. ( self.cref[2] or '?' )
530 function section.sid(self)
531 return ( self.sref[1] or '?' ) .. '.' .. ( self.sref[2] or '?' )
534 function section.values(self)
538 function section.section(self)
542 function section.variables(self)
544 if self.scheme.packages[self.sref[1]].variables[self.sref[2]] then
546 self.scheme.packages[self.sref[1]].variables[self.sref[2]]
548 table.insert( v, luci.uvl.option(
549 self.scheme, self.config, self.sref[2],
550 self.cref[1], self.cref[2], o
558 option = luci.util.class()
560 function option.__init__(self, scheme, co, st, c, s, o)
561 self.coption = co[s] and co[s][o] or nil
562 self.soption = scheme.packages[c].variables[st][o]
563 self.cref = { c, s, o }
564 self.sref = { c, st, o }
567 self.type = luci.uvl.TYPE_OPTION
570 function option.cid(self)
571 return ( self.cref[1] or '?' ) .. '.' ..
572 ( self.cref[2] or '?' ) .. '.' ..
573 ( self.cref[3] or '?' )
576 function option.sid(self)
577 return ( self.sref[1] or '?' ) .. '.' ..
578 ( self.sref[2] or '?' ) .. '.' ..
579 ( self.sref[3] or '?' )
582 function option.value(self)
586 function option.option(self)
590 function option.section(self)
591 return self.scheme.packages[self.sref[1]].sections[self.sref[2]]