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
18 --- UVL - UCI Validation Layer
22 module( "luci.uvl", package.seeall )
26 require("luci.model.uci")
27 require("luci.uvl.loghelper")
28 require("luci.uvl.datatypes")
29 require("luci.uvl.validation")
30 require("luci.uvl.dependencies")
37 --- Boolean; default true;
38 -- treat sections found in config but not in scheme as error
39 STRICT_UNKNOWN_SECTIONS = true
41 --- Boolean; default true;
42 -- treat options found in config but not in scheme as error
43 STRICT_UNKNOWN_OPTIONS = true
45 --- Boolean; default true;
46 -- treat failed external validators as error
47 STRICT_EXTERNAL_VALIDATORS = true
50 local default_schemedir = "/etc/scheme"
52 local function _assert( condition, fmt, ... )
54 return assert( nil, string.format( fmt, ... ) )
61 --- Object constructor
64 -- @param schemedir Path to the scheme directory (optional)
65 -- @return Instance object
66 UVL = luci.util.class()
68 function UVL.__init__( self, schemedir )
69 self.schemedir = schemedir or default_schemedir
72 self.uci = luci.model.uci
73 self.log = luci.uvl.loghelper
74 self.datatypes = luci.uvl.datatypes
78 --- Validate given configuration.
79 -- @param config Name of the configuration to validate
80 -- @return Boolean indicating whether the given config validates
81 -- @return String containing the reason for errors (if any)
82 function UVL.validate( self, config )
84 if not self.packages[config] then
85 local ok, err = pcall( self.read_scheme, self, config )
87 return false, self.log.scheme_error( config, err )
91 self.uci.load_config( config )
94 local co = self.uci.get_all( config )
97 local function _uci_foreach( type, func )
99 for k, v in pairs(co) do
100 if co[k]['.type'] == type then
101 sc[type] = sc[type] + 1
102 ok, err = func( k, v )
104 err = self.log.config_error( config, err )
112 for k, v in pairs( self.packages[config].sections ) do
114 local ok, err = _uci_foreach( k,
116 local sect = luci.uvl.section( self, co, k, config, s )
117 return self:_validate_section( sect )
120 if not ok then return false, err end
123 if STRICT_UNKNOWN_SECTIONS then
124 for k, v in pairs(co) do
125 if not self.beenthere[config..'.'..k] then
126 return false, self.log.config_error( config,
127 "Section '" .. config .. '.' .. co[k]['.type'] ..
128 "' not found in scheme" )
133 for _, k in ipairs(luci.util.keys(sc)) do
134 local s = self.packages[config].sections[k]
136 if s.required and sc[k] == 0 then
137 return false, self.log.config_error( config,
138 'Required section "' .. k .. '" not found in config' )
139 elseif s.unique and sc[k] > 1 then
140 return false, self.log.config_error( config,
141 'Unique section "' .. k .. '" occurs multiple times in config' )
148 --- Validate given config section.
149 -- @param config Name of the configuration to validate
150 -- @param section Name of the section to validate
151 -- @return Boolean indicating whether the given config validates
152 -- @return String containing the reason for errors (if any)
153 function UVL.validate_section( self, config, section )
155 if not self.packages[config] then
156 local ok, err = pcall( self.read_scheme, self, config )
158 return false, self.log.scheme_error( config, err )
162 self.uci.load_config( config )
165 local co = self.uci.get_all( config )
167 return self:_validate_section( luci.uvl.section(
168 self, co, co[section]['.type'], config, section
171 return false, "Section '" .. config .. '.' .. section ..
172 "' not found in config. Nothing to do."
176 --- Validate given config option.
177 -- @param config Name of the configuration to validate
178 -- @param section Name of the section to validate
179 -- @param option Name of the option to validate
180 -- @return Boolean indicating whether the given config validates
181 -- @return String containing the reason for errors (if any)
182 function UVL.validate_option( self, config, section, option )
184 if not self.packages[config] then
185 local ok, err = pcall( self.read_scheme, self, config )
187 return false, self.log.scheme_error( config, err )
191 self.uci.load_config( config )
194 local co = self.uci.get_all( config )
195 if co[section] and co[section][option] then
196 return self:_validate_option( luci.uvl.option(
197 self, co, co[section]['.type'], config, section, option
200 return false, "Option '" ..
201 config .. '.' .. section .. '.' .. option ..
202 "' not found in config. Nothing to do."
207 function UVL._validate_section( self, section )
209 if section:values() then
211 for _, v in ipairs(section:variables()) do
212 local ok, err = self:_validate_option( v )
215 return ok, self.log.section_error( section, err )
219 local ok, err = luci.uvl.dependencies.check( self, section )
225 print( "Error, scheme section '" .. section:sid() .. "' not found in data" )
228 if STRICT_UNKNOWN_OPTIONS and not section:section().dynamic then
229 for k, v in pairs(section:values()) do
230 if k:sub(1,1) ~= "." and not self.beenthere[
231 section:cid() .. '.' .. k
233 return false, "Option '" .. section:sid() .. '.' .. k ..
234 "' not found in scheme"
242 function UVL._validate_option( self, option, nodeps )
244 if not option:option() and
245 not ( option:section() and option:section().dynamic )
247 return false, "Option '" .. option:cid() ..
248 "' not found in scheme"
251 if option:option() then
252 if option:option().required and not option:value() then
253 return false, "Mandatory variable '" .. option:cid() ..
254 "' doesn't have a value"
257 if option:option().type == "enum" and option:value() then
258 if not option:option().values or
259 not option:option().values[option:value()]
261 return false, "Value '" .. ( option:value() or '<nil>' ) ..
262 "' of given option '" .. option:cid() ..
263 "' is not defined in enum { " ..
264 table.concat(luci.util.keys(option:option().values),", ") ..
269 if option:option().datatype and option:value() then
270 if self.datatypes[option:option().datatype] then
271 if not self.datatypes[option:option().datatype](
274 return false, "Value '" .. ( option:value() or '<nil>' ) ..
275 "' of given option '" .. option:cid() ..
276 "' doesn't validate as datatype '" ..
277 option:option().datatype .. "'"
280 return false, "Unknown datatype '" ..
281 option:option().datatype .. "' encountered"
286 return luci.uvl.dependencies.check( self, option )
289 local ok, err = luci.uvl.validation.check( self, option )
290 if not ok and STRICT_EXTERNAL_VALIDATORS then
291 return false, self.log.validator_error( option, err )
298 --- Find all parts of given scheme and construct validation tree.
299 -- This is normally done on demand, so you don't have to call this function
301 -- @param scheme Name of the scheme to parse
302 function UVL.read_scheme( self, scheme )
304 local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
307 for i, file in ipairs( files ) do
308 _assert( luci.fs.access(file), "Can't access file '%s'", file )
310 self.uci.set_confdir( luci.fs.dirname(file) )
311 self.uci.load( luci.fs.basename(file) )
313 table.insert( schemes, self.uci.get_all( luci.fs.basename(file) ) )
316 return self:_read_scheme_parts( scheme, schemes )
319 'Can\'t find scheme "' .. scheme ..
320 '" in "' .. self.schemedir .. '"'
325 -- Process all given parts and construct validation tree
326 function UVL._read_scheme_parts( self, scheme, schemes )
328 -- helper function to construct identifiers for given elements
329 local function _id( c, t )
330 if c == TYPE_SECTION then
331 return string.format(
333 scheme, t.name or '?' )
334 elseif c == TYPE_VARIABLE then
335 return string.format(
336 "variable '%s.%s.%s'",
337 scheme, t.section or '?.?', t.name or '?' )
338 elseif c == TYPE_ENUM then
339 return string.format(
341 scheme, t.variable or '?.?.?', t.value or '?' )
345 -- helper function to check for required fields
346 local function _req( c, t, r )
347 for i, v in ipairs(r) do
348 _assert( t[v], "Missing required field '%s' in %s", v, _id(c, t) )
352 -- helper function to validate references
353 local function _ref( c, t )
355 if c == TYPE_SECTION then
357 elseif c == TYPE_VARIABLE then
359 elseif c == TYPE_ENUM then
363 local r = luci.util.split( t[k], "." )
364 r[1] = ( #r[1] > 0 and r[1] or scheme )
366 _assert( #r == c, "Malformed %s reference in %s", k, _id(c, t) )
371 -- helper function to read bools
372 local function _bool( v )
373 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
376 -- Step 1: get all sections
377 for i, conf in ipairs( schemes ) do
378 for k, v in pairs( conf ) do
379 if v['.type'] == 'section' then
381 _req( TYPE_SECTION, v, { "name", "package" } )
383 local r = _ref( TYPE_SECTION, v )
385 self.packages[r[1]] =
386 self.packages[r[1]] or {
391 local p = self.packages[r[1]]
392 p.sections[v.name] = p.sections[v.name] or { }
393 p.variables[v.name] = p.variables[v.name] or { }
395 local s = p.sections[v.name]
397 for k, v2 in pairs(v) do
398 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
399 if k:match("^depends") then
400 s["depends"] = _assert(
401 self:_read_dependency( v2, s["depends"] ),
402 "Section '%s' in scheme '%s' has malformed " ..
403 "dependency specification in '%s'",
404 v.name or '<nil>', scheme or '<nil>', k
406 elseif k == "dynamic" or k == "unique" or k == "required" then
414 s.dynamic = s.dynamic or false
415 s.unique = s.unique or false
416 s.required = s.required or false
421 -- Step 2: get all variables
422 for i, conf in ipairs( schemes ) do
423 for k, v in pairs( conf ) do
424 if v['.type'] == "variable" then
426 _req( TYPE_VARIABLE, v, { "name", "section" } )
428 local r = _ref( TYPE_VARIABLE, v )
430 local p = _assert( self.packages[r[1]],
431 "Variable '%s' in scheme '%s' references unknown package '%s'",
432 v.name, scheme, r[1] )
434 local s = _assert( p.variables[r[2]],
435 "Variable '%s' in scheme '%s' references unknown section '%s'",
436 v.name, scheme, r[2] )
438 s[v.name] = s[v.name] or { }
442 for k, v2 in pairs(v) do
443 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
444 if k:match("^depends") then
445 t["depends"] = _assert(
446 self:_read_dependency( v2, t["depends"] ),
447 'Invalid reference "%s" in "%s.%s.%s"',
448 v2, v.name, scheme, k
450 elseif k:match("^validator") then
451 t["validators"] = _assert(
452 self:_read_validator( v2, t["validators"] ),
453 "Variable '%s' in scheme '%s' has malformed " ..
454 "validator specification in '%s'",
457 elseif k == "required" then
465 t.type = t.type or "variable"
466 t.datatype = t.datatype or "string"
467 t.required = t.required or false
472 -- Step 3: get all enums
473 for i, conf in ipairs( schemes ) do
474 for k, v in pairs( conf ) do
475 if v['.type'] == "enum" then
477 _req( TYPE_ENUM, v, { "value", "variable" } )
479 local r = _ref( TYPE_ENUM, v )
481 local p = _assert( self.packages[r[1]],
482 "Enum '%s' in scheme '%s' references unknown package '%s'",
483 v.value, scheme, r[1] )
485 local s = _assert( p.variables[r[2]],
486 "Enum '%s' in scheme '%s' references unknown section '%s'",
487 v.value, scheme, r[2] )
489 local t = _assert( s[r[3]],
490 "Enum '%s' in scheme '%s', section '%s' references " ..
491 "unknown variable '%s'",
492 v.value, scheme, r[2], r[3] )
494 _assert( t.type == "enum",
495 "Enum '%s' in scheme '%s', section '%s' references " ..
496 "variable '%s' with non enum type '%s'",
497 v.value, scheme, r[2], r[3], t.type )
500 t.values = { [v.value] = v.title or v.value }
502 t.values[v.value] = v.title or v.value
506 _assert( not t.default,
507 "Enum '%s' in scheme '%s', section '%s' redeclares " ..
508 "the default value of variable '%s'",
509 v.value, scheme, r[2], v.variable )
520 -- Read a dependency specification
521 function UVL._read_dependency( self, value, deps )
522 local parts = luci.util.split( value, "%s*,%s*", nil, true )
523 local condition = { }
525 for i, val in ipairs(parts) do
526 local k, v = unpack(luci.util.split( val, "%s*=%s*", nil, true ))
529 k:match("^%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+$") or
530 k:match("^%$?[a-zA-Z0-9_]+%.%$?[a-zA-Z0-9_]+$") or
531 k:match("^%$?[a-zA-Z0-9_]+$")
533 condition[k] = v or true
542 table.insert( deps, condition )
548 -- Read a validator specification
549 function UVL._read_validator( self, value, validators )
553 if value:match("^exec:") then
554 validator = value:gsub("^exec:","")
555 elseif value:match("^lua:") then
556 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
560 if not validators then
561 validators = { validator }
563 table.insert( validators, validator )
571 -- Resolve given path
572 function UVL._resolve_function( self, value )
573 local path = luci.util.split(value, ".")
576 local stat, mod = pcall(require, table.concat(path, ".", 1, i))
578 for j=i+1, #path-1 do
579 if not type(mod) == "table" then
587 mod = type(mod) == "table" and mod[path[#path]] or nil
588 if type(mod) == "function" then
596 --- Object representation of a scheme/config section.
599 -- @name luci.uvl.section
601 --- Section instance constructor.
604 -- @param scheme Scheme instance
605 -- @param co Configuration data
606 -- @param st Section type
607 -- @param c Configuration name
608 -- @param s Section name
609 -- @return Section instance
610 section = luci.util.class()
612 function section.__init__(self, scheme, co, st, c, s)
613 self.csection = co[s]
614 self.ssection = scheme.packages[c].sections[st]
616 self.sref = { c, st }
619 self.type = luci.uvl.TYPE_SECTION
622 --- Get the config path of this section.
623 -- @return String containing the identifier
624 function section.cid(self)
625 return ( self.cref[1] or '?' ) .. '.' .. ( self.cref[2] or '?' )
628 --- Get the scheme path of this section.
629 -- @return String containing the identifier
630 function section.sid(self)
631 return ( self.sref[1] or '?' ) .. '.' .. ( self.sref[2] or '?' )
634 --- Get all configuration values within this section.
635 -- @return Table containing the values
636 function section.values(self)
640 --- Get the associated section information in scheme.
641 -- @return Table containing the scheme properties
642 function section.section(self)
646 --- Get all option objects associated with this section.
647 -- @return Table containing all associated luci.uvl.option instances
648 function section.variables(self)
650 if self.scheme.packages[self.sref[1]].variables[self.sref[2]] then
652 self.scheme.packages[self.sref[1]].variables[self.sref[2]]
654 table.insert( v, luci.uvl.option(
655 self.scheme, self.config, self.sref[2],
656 self.cref[1], self.cref[2], o
664 --- Object representation of a scheme/config option.
667 -- @name luci.uvl.option
669 --- Section instance constructor.
672 -- @param scheme Scheme instance
673 -- @param co Configuration data
674 -- @param st Section type
675 -- @param c Configuration name
676 -- @param s Section name
677 -- @param o Option name
678 -- @return Option instance
679 option = luci.util.class()
681 function option.__init__(self, scheme, co, st, c, s, o)
682 self.coption = co[s] and co[s][o] or nil
683 self.soption = scheme.packages[c].variables[st][o]
684 self.cref = { c, s, o }
685 self.sref = { c, st, o }
688 self.type = luci.uvl.TYPE_OPTION
691 --- Get the config path of this option.
692 -- @return String containing the identifier
693 function option.cid(self)
694 return ( self.cref[1] or '?' ) .. '.' ..
695 ( self.cref[2] or '?' ) .. '.' ..
696 ( self.cref[3] or '?' )
699 --- Get the scheme path of this option.
700 -- @return String containing the identifier
701 function option.sid(self)
702 return ( self.sref[1] or '?' ) .. '.' ..
703 ( self.sref[2] or '?' ) .. '.' ..
704 ( self.sref[3] or '?' )
707 --- Get the value of this option.
708 -- @return The associated configuration value
709 function option.value(self)
713 --- Get the associated option information in scheme.
714 -- @return Table containing the scheme properties
715 function option.option(self)
719 --- Get the associated section information in scheme.
720 -- @return Table containing the scheme properties
721 function option.section(self)
722 return self.scheme.packages[self.sref[1]].sections[self.sref[2]]