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.loghelper")
23 require("luci.uvl.datatypes")
24 --require("luci.uvl.validation")
25 require("luci.uvl.dependencies")
31 STRICT_UNKNOWN_SECTIONS = true
32 STRICT_UNKNOWN_OPTIONS = true
35 local default_schemedir = "/etc/scheme"
37 local function _assert( condition, fmt, ... )
39 return assert( nil, string.format( fmt, ... ) )
45 UVL = luci.util.class()
47 function UVL.__init__( self, schemedir )
48 self.schemedir = schemedir or default_schemedir
51 self.uci = luci.model.uci
52 self.log = luci.uvl.loghelper
53 self.datatypes = luci.uvl.datatypes
57 --- Validate given configuration.
58 -- @param config Name of the configuration to validate
59 -- @param scheme Scheme to validate against (optional)
60 -- @return Boolean indicating weather the given config validates
61 -- @return String containing the reason for errors (if any)
62 function UVL.validate( self, config )
63 self.uci.load_config( config )
66 local co = self.uci.get_all( config )
69 local function _uci_foreach( type, func )
71 for k, v in pairs(co) do
72 if co[k]['.type'] == type then
73 sc[type] = sc[type] + 1
74 ok, err = func( k, v )
76 err = self.log.config_error( config, err )
84 for k, v in pairs( self.packages[config].sections ) do
86 local ok, err = _uci_foreach( k,
88 local sect = luci.uvl.section( self, co, k, config, s )
89 return self:_validate_section( sect )
92 if not ok then return false, err end
95 if STRICT_UNKNOWN_SECTIONS then
96 for k, v in pairs(co) do
97 if not self.beenthere[config..'.'..k] then
98 return false, self.log.config_error( config,
99 "Section '" .. config .. '.' .. co[k]['.type'] ..
100 "' not found in scheme" )
105 for _, k in ipairs(luci.util.keys(sc)) do
106 local s = self.packages[config].sections[k]
108 if s.required and sc[k] == 0 then
109 return false, self.log.config_error( config,
110 'Required section "' .. k .. '" not found in config' )
111 elseif s.unique and sc[k] > 1 then
112 return false, self.log.config_error( config,
113 'Unique section "' .. k .. '" occurs multiple times in config' )
120 function UVL.validate_section( self, config, section )
121 self.uci.load_config( config )
124 local co = self.uci.get_all( config )
126 return self:_validate_section( luci.uvl.section(
127 self, co, co[section]['.type'], config, section
130 return false, "Section '" .. config .. '.' .. section ..
131 "' not found in config. Nothing to do."
135 function UVL.validate_option( self, config, section, option )
136 self.uci.load_config( config )
139 local co = self.uci.get_all( config )
140 if co[section] and co[section][option] then
141 return self:_validate_option( luci.uvl.option(
142 self, co, co[section]['.type'], config, section, option
145 return false, "Option '" ..
146 config .. '.' .. section .. '.' .. option ..
147 "' not found in config. Nothing to do."
151 --- Validate given section of given configuration.
152 -- @param config Name of the configuration to validate
153 -- @param section Key of the section to validate
154 -- @param scheme Scheme to validate against
155 -- @return Boolean indicating weather the given config validates
156 -- @return String containing the reason for errors (if any)
157 function UVL._validate_section( self, section )
159 if section:values() then
161 for _, v in ipairs(section:variables()) do
162 local ok, err = self:_validate_option( v )
165 return ok, self.log.section_error( section, err )
169 local ok, err = luci.uvl.dependencies.check( self, section )
175 print( "Error, scheme section '" .. section:sid() .. "' not found in data" )
178 if STRICT_UNKNOWN_OPTIONS and not section:section().dynamic then
179 for k, v in pairs(section:values()) do
180 if k:sub(1,1) ~= "." and not self.beenthere[
181 section:cid() .. '.' .. k
183 return false, "Option '" .. section:sid() .. '.' .. k ..
184 "' not found in scheme"
192 --- Validate given option within section of given configuration.
193 -- @param config Name of the configuration to validate
194 -- @param section Key of the section to validate
195 -- @param option Name of the option to validate
196 -- @param scheme Scheme to validate against
197 -- @return Boolean indicating weather the given config validates
198 -- @return String containing the reason for errors (if any)
199 function UVL._validate_option( self, option, nodeps )
201 if not option:option() and
202 not ( option:section() and option:section().dynamic )
204 return false, "Option '" .. option:cid() ..
205 "' not found in scheme"
208 if option:option() then
209 if option:option().required and not option:value() then
210 return false, "Mandatory variable '" .. option:cid() ..
211 "' doesn't have a value"
214 if option:option().type == "enum" and option:value() then
215 if not option:option().values or
216 not option:option().values[option:value()]
218 return false, "Value '" .. ( option:value() or '<nil>' ) ..
219 "' of given option '" .. option:cid() ..
220 "' is not defined in enum { " ..
221 table.concat(luci.util.keys(option:option().values),", ") ..
226 if option:option().datatype and option:value() then
227 if self.datatypes[option:option().datatype] then
228 if not self.datatypes[option:option().datatype](
231 return false, "Value '" .. ( option:value() or '<nil>' ) ..
232 "' of given option '" .. option:cid() ..
233 "' doesn't validate as datatype '" ..
234 option:option().datatype .. "'"
237 return false, "Unknown datatype '" ..
238 option:option().datatype .. "' encountered"
243 return luci.uvl.dependencies.check( self, option )
250 --- Find all parts of given scheme and construct validation tree
251 -- @param scheme Name of the scheme to parse
252 -- @return Parsed scheme
253 function UVL.read_scheme( self, scheme )
256 for i, file in ipairs( luci.fs.glob(self.schemedir .. '/*/' .. scheme) ) do
257 _assert( luci.fs.access(file), "Can't access file '%s'", file )
259 self.uci.set_confdir( luci.fs.dirname(file) )
260 self.uci.load( luci.fs.basename(file) )
262 table.insert( schemes, self.uci.get_all( luci.fs.basename(file) ) )
265 return self:_read_scheme_parts( scheme, schemes )
268 -- Process all given parts and construct validation tree
269 function UVL._read_scheme_parts( self, scheme, schemes )
271 -- helper function to construct identifiers for given elements
272 local function _id( c, t )
273 if c == TYPE_SECTION then
274 return string.format(
276 scheme, t.name or '?' )
277 elseif c == TYPE_VARIABLE then
278 return string.format(
279 "variable '%s.%s.%s'",
280 scheme, t.section or '?.?', t.name or '?' )
281 elseif c == TYPE_ENUM then
282 return string.format(
284 scheme, t.variable or '?.?.?', t.value or '?' )
288 -- helper function to check for required fields
289 local function _req( c, t, r )
290 for i, v in ipairs(r) do
291 _assert( t[v], "Missing required field '%s' in %s", v, _id(c, t) )
295 -- helper function to validate references
296 local function _ref( c, t )
298 if c == TYPE_SECTION then
300 elseif c == TYPE_VARIABLE then
302 elseif c == TYPE_ENUM then
306 local r = luci.util.split( t[k], "." )
307 r[1] = ( #r[1] > 0 and r[1] or scheme )
309 _assert( #r == c, "Malformed %s reference in %s", k, _id(c, t) )
314 -- helper function to read bools
315 local function _bool( v )
316 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
319 -- Step 1: get all sections
320 for i, conf in ipairs( schemes ) do
321 for k, v in pairs( conf ) do
322 if v['.type'] == 'section' then
324 _req( TYPE_SECTION, v, { "name", "package" } )
326 local r = _ref( TYPE_SECTION, v )
328 self.packages[r[1]] =
329 self.packages[r[1]] or {
334 local p = self.packages[r[1]]
335 p.sections[v.name] = p.sections[v.name] or { }
336 p.variables[v.name] = p.variables[v.name] or { }
338 local s = p.sections[v.name]
340 for k, v2 in pairs(v) do
341 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
342 if k:match("^depends") then
343 s["depends"] = _assert(
344 self:_read_dependency( v2, s["depends"] ),
345 "Section '%s' in scheme '%s' has malformed " ..
346 "dependency specification in '%s'",
347 v.name or '<nil>', scheme or '<nil>', k
349 elseif k == "dynamic" or k == "unique" or k == "required" then
360 -- Step 2: get all variables
361 for i, conf in ipairs( schemes ) do
362 for k, v in pairs( conf ) do
363 if v['.type'] == "variable" then
365 _req( TYPE_VARIABLE, v, { "name", "section" } )
367 local r = _ref( TYPE_VARIABLE, v )
369 local p = _assert( self.packages[r[1]],
370 "Variable '%s' in scheme '%s' references unknown package '%s'",
371 v.name, scheme, r[1] )
373 local s = _assert( p.variables[r[2]],
374 "Variable '%s' in scheme '%s' references unknown section '%s'",
375 v.name, scheme, r[2] )
377 s[v.name] = s[v.name] or { }
381 for k, v2 in pairs(v) do
382 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
383 if k:match("^depends") then
384 t["depends"] = _assert(
385 self:_read_dependency( v2, t["depends"] ),
386 'Invalid reference "%s" in "%s.%s.%s"',
387 v2, v.name, scheme, k
389 elseif k:match("^validator") then
390 t["validators"] = _assert(
391 self:_read_validator( v2, t["validators"] ),
392 "Variable '%s' in scheme '%s' has malformed " ..
393 "validator specification in '%s'",
396 elseif k == "required" then
404 t.type = t.type or "variable"
405 t.required = t.required or false
410 -- Step 3: get all enums
411 for i, conf in ipairs( schemes ) do
412 for k, v in pairs( conf ) do
413 if v['.type'] == "enum" then
415 _req( TYPE_ENUM, v, { "value", "variable" } )
417 local r = _ref( TYPE_ENUM, v )
419 local p = _assert( self.packages[r[1]],
420 "Enum '%s' in scheme '%s' references unknown package '%s'",
421 v.value, scheme, r[1] )
423 local s = _assert( p.variables[r[2]],
424 "Enum '%s' in scheme '%s' references unknown section '%s'",
425 v.value, scheme, r[2] )
427 local t = _assert( s[r[3]],
428 "Enum '%s' in scheme '%s', section '%s' references " ..
429 "unknown variable '%s'",
430 v.value, scheme, r[2], r[3] )
432 _assert( t.type == "enum",
433 "Enum '%s' in scheme '%s', section '%s' references " ..
434 "variable '%s' with non enum type '%s'",
435 v.value, scheme, r[2], r[3], t.type )
438 t.values = { [v.value] = v.title or v.value }
440 t.values[v.value] = v.title or v.value
444 _assert( not t.default,
445 "Enum '%s' in scheme '%s', section '%s' redeclares " ..
446 "the default value of variable '%s'",
447 v.value, scheme, r[2], v.variable )
458 -- Read a dependency specification
459 function UVL._read_dependency( self, value, deps )
460 local parts = luci.util.split( value, "%s*,%s*", nil, true )
461 local condition = { }
463 for i, val in ipairs(parts) do
464 local k, v = unpack(luci.util.split( val, "%s*=%s*", nil, true ))
467 k:match("^%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+$") or
468 k:match("^%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+$") or
469 k:match("^%$?[a-zA-Z0-9_]+$")
471 condition[k] = v or true
480 table.insert( deps, condition )
486 -- Read a validator specification
487 function UVL._read_validator( self, value, validators )
490 if value and value:match("/") and self.datatypes.file(value) then
493 validator = self:_resolve_function( value )
497 if not validators then
498 validators = { validator }
500 table.insert( validators, validator )
507 -- Resolve given path
508 function UVL._resolve_function( self, value )
509 local path = luci.util.split(value, ".")
512 local stat, mod = pcall(require, table.concat(path, ".", 1, i))
514 for j=i+1, #path-1 do
515 if not type(mod) == "table" then
523 mod = type(mod) == "table" and mod[path[#path]] or nil
524 if type(mod) == "function" then
532 section = luci.util.class()
534 function section.__init__(self, scheme, co, st, c, s)
535 self.csection = co[s]
536 self.ssection = scheme.packages[c].sections[st]
538 self.sref = { c, st }
541 self.type = luci.uvl.TYPE_SECTION
544 function section.cid(self)
545 return ( self.cref[1] or '?' ) .. '.' .. ( self.cref[2] or '?' )
548 function section.sid(self)
549 return ( self.sref[1] or '?' ) .. '.' .. ( self.sref[2] or '?' )
552 function section.values(self)
556 function section.section(self)
560 function section.variables(self)
562 if self.scheme.packages[self.sref[1]].variables[self.sref[2]] then
564 self.scheme.packages[self.sref[1]].variables[self.sref[2]]
566 table.insert( v, luci.uvl.option(
567 self.scheme, self.config, self.sref[2],
568 self.cref[1], self.cref[2], o
576 option = luci.util.class()
578 function option.__init__(self, scheme, co, st, c, s, o)
579 self.coption = co[s] and co[s][o] or nil
580 self.soption = scheme.packages[c].variables[st][o]
581 self.cref = { c, s, o }
582 self.sref = { c, st, o }
585 self.type = luci.uvl.TYPE_OPTION
588 function option.cid(self)
589 return ( self.cref[1] or '?' ) .. '.' ..
590 ( self.cref[2] or '?' ) .. '.' ..
591 ( self.cref[3] or '?' )
594 function option.sid(self)
595 return ( self.sref[1] or '?' ) .. '.' ..
596 ( self.sref[2] or '?' ) .. '.' ..
597 ( self.sref[3] or '?' )
600 function option.value(self)
604 function option.option(self)
608 function option.section(self)
609 return self.scheme.packages[self.sref[1]].sections[self.sref[2]]