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
49 --- Boolean; default true;
50 -- treat list values stored as options like errors
51 STRICT_LIST_TYPE = true
54 local default_schemedir = "/lib/uci/schema"
56 local function _assert( condition, fmt, ... )
58 return assert( nil, string.format( fmt, ... ) )
65 --- Object constructor
68 -- @param schemedir Path to the scheme directory (optional)
69 -- @return Instance object
70 UVL = luci.util.class()
72 function UVL.__init__( self, schemedir )
73 self.schemedir = schemedir or default_schemedir
76 self.uci = luci.model.uci
77 self.dep = luci.uvl.dependencies
78 self.log = luci.uvl.loghelper
79 self.datatypes = luci.uvl.datatypes
83 --- Parse given scheme and return the scheme tree.
84 -- @param scheme Name of the scheme to parse
85 -- @return Table containing the parsed scheme or nil on error
86 -- @return String containing the reason for errors (if any)
87 function UVL.get_scheme( self, scheme )
88 if not self.packages[scheme] then
89 local ok, err = pcall( self.read_scheme, self, scheme )
91 return nil, self.log.scheme_error( scheme, err )
94 return self.packages[scheme], nil
97 --- Return a table containing the dependencies of specified section or option.
98 -- @param config Name of the configuration or parsed scheme object
99 -- @param section Type of the section
100 -- @param option Name of the option (optional)
101 -- @return Table containing the dependencies or nil on error
102 -- @return String containing the reason for errors (if any)
103 function UVL.get_dependencies( self, config, section, option )
104 config = ( type(config) == "string" and self:get_scheme(config) or config )
109 if not config.sections[section] then return deps end
111 if option and config.variables[section][option] then
112 dt = config.variables[section][option].depends
114 dt = config.sections[section].depends
118 for _, d in ipairs(dt) do
120 for k, v in pairs(d) do
121 local r = self.dep._parse_reference( k )
125 return nil, string.format(
126 'Ambiguous dependency reference "%s" for object ' ..
128 k, config.name, section,
129 option and '.' .. option or ''
133 table.insert( deps, sdeps )
139 --- Validate given configuration, section or option.
140 -- @param config Name of the configuration to validate
141 -- @param section Name of the section to validate (optional)
142 -- @param option Name of the option to validate (optional)
143 -- @return Boolean indicating whether the given config validates
144 -- @return String containing the reason for errors (if any)
145 function UVL.validate( self, config, section, option )
146 if config and section and option then
147 return self:validate_option( config, section, option )
148 elseif config and section then
149 return self:validate_section( config, section )
151 return self:validate_config( config )
155 --- Validate given configuration.
156 -- @param config Name of the configuration to validate
157 -- @return Boolean indicating whether the given config validates
158 -- @return String containing the reason for errors (if any)
159 function UVL.validate_config( self, config )
161 if not self.packages[config] then
162 local ok, err = pcall( self.read_scheme, self, config )
164 return false, self.log.scheme_error( config, err )
168 self.uci.load_config( config )
171 local co = self.uci.get_all( config )
175 return false, 'Unable to load configuration "' .. config .. '"'
178 local function _uci_foreach( type, func )
180 for k, v in pairs(co) do
181 if co[k]['.type'] == type then
182 sc[type] = sc[type] + 1
183 ok, err = func( k, v )
185 err = self.log.config_error( config, err )
193 for k, v in pairs( self.packages[config].sections ) do
195 local ok, err = _uci_foreach( k,
197 local sect = luci.uvl.section( self, co, k, config, s )
198 return self:_validate_section( sect )
201 if not ok then return false, err end
204 if STRICT_UNKNOWN_SECTIONS then
205 for k, v in pairs(co) do
206 if not self.beenthere[config..'.'..k] then
207 return false, self.log.config_error( config,
208 "Section '" .. config .. '.' .. co[k]['.type'] ..
209 "' not found in scheme" )
214 for _, k in ipairs(luci.util.keys(sc)) do
215 local s = self.packages[config].sections[k]
217 if s.required and sc[k] == 0 then
218 return false, self.log.config_error( config,
219 'Required section "' .. k .. '" not found in config' )
220 elseif s.unique and sc[k] > 1 then
221 return false, self.log.config_error( config,
222 'Unique section "' .. k .. '" occurs multiple times in config' )
229 --- Validate given config section.
230 -- @param config Name of the configuration to validate
231 -- @param section Name of the section to validate
232 -- @return Boolean indicating whether the given config validates
233 -- @return String containing the reason for errors (if any)
234 function UVL.validate_section( self, config, section )
236 if not self.packages[config] then
237 local ok, err = pcall( self.read_scheme, self, config )
239 return false, self.log.scheme_error( config, err )
243 self.uci.load_config( config )
246 local co = self.uci.get_all( config )
249 return false, 'Unable to load configuration "' .. config .. '"'
253 return self:_validate_section( luci.uvl.section(
254 self, co, co[section]['.type'], config, section
257 return false, 'Section "' .. config .. '.' .. section ..
258 '" not found in config. Nothing to do.'
262 --- Validate given config option.
263 -- @param config Name of the configuration to validate
264 -- @param section Name of the section to validate
265 -- @param option Name of the option to validate
266 -- @return Boolean indicating whether the given config validates
267 -- @return String containing the reason for errors (if any)
268 function UVL.validate_option( self, config, section, option )
270 if not self.packages[config] then
271 local ok, err = pcall( self.read_scheme, self, config )
273 return false, self.log.scheme_error( config, err )
277 self.uci.load_config( config )
280 local co = self.uci.get_all( config )
283 return false, 'Unable to load configuration "' .. config .. '"'
286 if co[section] and co[section][option] then
287 return self:_validate_option( luci.uvl.option(
288 self, co, co[section]['.type'], config, section, option
291 return false, 'Option "' ..
292 config .. '.' .. section .. '.' .. option ..
293 '" not found in config. Nothing to do.'
298 function UVL._validate_section( self, section )
300 if section:values() then
302 for _, v in ipairs(section:variables()) do
303 local ok, err = self:_validate_option( v )
306 return ok, self.log.section_error( section, err )
310 local ok, err = luci.uvl.dependencies.check( self, section )
316 return false, 'Option "' .. section:sid() .. '" not found in config'
319 if STRICT_UNKNOWN_OPTIONS and not section:section().dynamic then
320 for k, v in pairs(section:values()) do
321 if k:sub(1,1) ~= "." and not self.beenthere[
322 section:cid() .. '.' .. k
324 return false, "Option '" .. section:sid() .. '.' .. k ..
325 "' not found in scheme"
333 function UVL._validate_option( self, option, nodeps )
335 local item = option:option()
336 local val = option:value()
338 if not item and not ( option:section() and option:section().dynamic ) then
339 return false, 'Option "' .. option:cid() ..
340 '" not found in scheme'
343 if item.required and not val then
344 return false, 'Mandatory variable "' .. option:cid() ..
345 '" does not have a value'
348 if item.type == "enum" and val then
349 if not item.values or not item.values[val] then
350 return false, 'Value "' .. ( val or '<nil>' ) ..
351 '" of given option "' .. option:cid() ..
352 '" is not defined in enum { ' ..
353 table.concat( luci.util.keys(item.values), ", " ) ..
356 elseif item.type == "list" and val then
357 if type(val) ~= "table" and STRICT_LIST_TYPE then
358 return false, 'Option "' .. option:cid() ..
359 '" is defined as list but stored as plain value'
363 if item.datatype and val then
364 if self.datatypes[item.datatype] then
365 val = ( type(val) == "table" and val or { val } )
366 for i, v in ipairs(val) do
367 if not self.datatypes[item.datatype]( v ) then
368 return false, 'Value' .. ( #val>1 and ' #'..i or '' ) ..
369 ' "' .. ( v or '<nil>' ) ..
370 '" of given option "' .. option:cid() ..
371 '" does not validate as datatype "' ..
376 return false, 'Unknown datatype "' ..
377 item.datatype .. '" encountered'
382 return luci.uvl.dependencies.check( self, option )
385 local ok, err = luci.uvl.validation.check( self, option )
386 if not ok and STRICT_EXTERNAL_VALIDATORS then
387 return false, self.log.validator_error( option, err )
394 --- Find all parts of given scheme and construct validation tree.
395 -- This is normally done on demand, so you don't have to call this function
397 -- @param scheme Name of the scheme to parse
398 function UVL.read_scheme( self, scheme )
400 local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
403 for i, file in ipairs( files ) do
404 _assert( luci.fs.access(file), "Can't access file '%s'", file )
406 self.uci.set_confdir( luci.fs.dirname(file) )
407 self.uci.load( luci.fs.basename(file) )
409 table.insert( schemes, self.uci.get_all( luci.fs.basename(file) ) )
412 return self:_read_scheme_parts( scheme, schemes )
415 'Can not find scheme "' .. scheme ..
416 '" in "' .. self.schemedir .. '"'
421 -- Process all given parts and construct validation tree
422 function UVL._read_scheme_parts( self, scheme, schemes )
424 -- helper function to construct identifiers for given elements
425 local function _id( c, t )
426 if c == TYPE_SECTION then
427 return string.format(
429 scheme, t.name or '?' )
430 elseif c == TYPE_VARIABLE then
431 return string.format(
432 'variable "%s.%s.%s"',
433 scheme, t.section or '?.?', t.name or '?' )
434 elseif c == TYPE_ENUM then
435 return string.format(
437 scheme, t.variable or '?.?.?', t.value or '?' )
441 -- helper function to check for required fields
442 local function _req( c, t, r )
443 for i, v in ipairs(r) do
444 _assert( t[v], 'Missing required field "%s" in %s', v, _id(c, t) )
448 -- helper function to validate references
449 local function _ref( c, t )
451 if c == TYPE_SECTION then
453 elseif c == TYPE_VARIABLE then
455 elseif c == TYPE_ENUM then
459 local r = luci.util.split( t[k], "." )
460 r[1] = ( #r[1] > 0 and r[1] or scheme )
462 _assert( #r == c, 'Malformed %s reference in %s', k, _id(c, t) )
467 -- helper function to read bools
468 local function _bool( v )
469 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
472 -- Step 1: get all sections
473 for i, conf in ipairs( schemes ) do
474 for k, v in pairs( conf ) do
475 if v['.type'] == 'section' then
477 _req( TYPE_SECTION, v, { "name", "package" } )
479 local r = _ref( TYPE_SECTION, v )
481 self.packages[r[1]] =
482 self.packages[r[1]] or {
488 local p = self.packages[r[1]]
489 p.sections[v.name] = p.sections[v.name] or { }
490 p.variables[v.name] = p.variables[v.name] or { }
492 local s = p.sections[v.name]
494 for k, v2 in pairs(v) do
495 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
496 if k == "depends" then
497 s["depends"] = _assert(
498 self:_read_dependency( v2, s["depends"] ),
499 'Section "%s" in scheme "%s" has malformed ' ..
500 'dependency specification in "%s"',
501 v.name or '<nil>', scheme or '<nil>', k
503 elseif k == "dynamic" or k == "unique" or k == "required" then
511 s.dynamic = s.dynamic or false
512 s.unique = s.unique or false
513 s.required = s.required or false
518 -- Step 2: get all variables
519 for i, conf in ipairs( schemes ) do
520 for k, v in pairs( conf ) do
521 if v['.type'] == "variable" then
523 _req( TYPE_VARIABLE, v, { "name", "section" } )
525 local r = _ref( TYPE_VARIABLE, v )
527 local p = _assert( self.packages[r[1]],
528 'Variable "%s" in scheme "%s" references unknown package "%s"',
529 v.name, scheme, r[1] )
531 local s = _assert( p.variables[r[2]],
532 'Variable "%s" in scheme "%s" references unknown section "%s"',
533 v.name, scheme, r[2] )
535 s[v.name] = s[v.name] or { }
539 for k, v2 in pairs(v) do
540 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
541 if k == "depends" then
542 t["depends"] = _assert(
543 self:_read_dependency( v2, t["depends"] ),
544 'Invalid reference "%s" in "%s.%s.%s"',
545 v2, v.name, scheme, k
547 elseif k == "validator" then
548 t["validators"] = _assert(
549 self:_read_validator( v2, t["validators"] ),
550 'Variable "%s" in scheme "%s" has malformed ' ..
551 'validator specification in "%s"',
554 elseif k == "required" then
562 t.type = t.type or "variable"
563 t.datatype = t.datatype or "string"
564 t.required = t.required or false
569 -- Step 3: get all enums
570 for i, conf in ipairs( schemes ) do
571 for k, v in pairs( conf ) do
572 if v['.type'] == "enum" then
574 _req( TYPE_ENUM, v, { "value", "variable" } )
576 local r = _ref( TYPE_ENUM, v )
577 local p = _assert( self.packages[r[1]],
578 'Enum "%s" in scheme "%s" references unknown package "%s"',
579 v.value, scheme, r[1] )
581 local s = _assert( p.variables[r[2]],
582 'Enum "%s" in scheme "%s" references unknown section "%s"',
583 v.value, scheme, r[2] )
585 local t = _assert( s[r[3]],
586 'Enum "%s" in scheme "%s", section "%s" references ' ..
587 'unknown variable "%s"',
588 v.value, scheme, r[2], r[3] )
590 _assert( t.type == "enum",
591 'Enum "%s" in scheme "%s", section "%s" references ' ..
592 'variable "%s" with non enum type "%s"',
593 v.value, scheme, r[2], r[3], t.type )
596 t.values = { [v.value] = v.title or v.value }
598 t.values[v.value] = v.title or v.value
602 _assert( not t.default,
603 'Enum "%s" in scheme "%s", section "%s" redeclares ' ..
604 'the default value of variable "%s"',
605 v.value, scheme, r[2], v.variable )
616 -- Read a dependency specification
617 function UVL._read_dependency( self, values, deps )
618 local expr = "%$?[a-zA-Z0-9_]+"
620 values = ( type(values) == "table" and values or { values } )
621 for _, value in ipairs(values) do
622 local parts = luci.util.split( value, "%s*,%s*", nil, true )
623 local condition = { }
624 for i, val in ipairs(parts) do
625 local k, v = unpack(luci.util.split(val, "%s*=%s*", nil, true))
628 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
629 k:match("^"..expr.."%."..expr.."$") or
630 k:match("^"..expr.."$")
632 condition[k] = v or true
641 table.insert( deps, condition )
649 -- Read a validator specification
650 function UVL._read_validator( self, values, validators )
652 values = ( type(values) == "table" and values or { values } )
653 for _, value in ipairs(values) do
656 if value:match("^exec:") then
657 validator = value:gsub("^exec:","")
658 elseif value:match("^lua:") then
659 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
663 if not validators then
664 validators = { validator }
666 table.insert( validators, validator )
677 -- Resolve given path
678 function UVL._resolve_function( self, value )
679 local path = luci.util.split(value, ".")
682 local stat, mod = pcall(require, table.concat(path, ".", 1, i))
684 for j=i+1, #path-1 do
685 if not type(mod) == "table" then
693 mod = type(mod) == "table" and mod[path[#path]] or nil
694 if type(mod) == "function" then
702 --- Object representation of a scheme/config section.
705 -- @name luci.uvl.section
707 --- Section instance constructor.
710 -- @param scheme Scheme instance
711 -- @param co Configuration data
712 -- @param st Section type
713 -- @param c Configuration name
714 -- @param s Section name
715 -- @return Section instance
716 section = luci.util.class()
718 function section.__init__(self, scheme, co, st, c, s)
719 self.csection = co[s]
720 self.ssection = scheme.packages[c].sections[st]
722 self.sref = { c, st }
725 self.type = luci.uvl.TYPE_SECTION
728 --- Get the config path of this section.
729 -- @return String containing the identifier
730 function section.cid(self)
731 return ( self.cref[1] or '?' ) .. '.' .. ( self.cref[2] or '?' )
734 --- Get the scheme path of this section.
735 -- @return String containing the identifier
736 function section.sid(self)
737 return ( self.sref[1] or '?' ) .. '.' .. ( self.sref[2] or '?' )
740 --- Get all configuration values within this section.
741 -- @return Table containing the values
742 function section.values(self)
746 --- Get the associated section information in scheme.
747 -- @return Table containing the scheme properties
748 function section.section(self)
752 --- Get all option objects associated with this section.
753 -- @return Table containing all associated luci.uvl.option instances
754 function section.variables(self)
756 if self.scheme.packages[self.sref[1]].variables[self.sref[2]] then
758 self.scheme.packages[self.sref[1]].variables[self.sref[2]]
760 table.insert( v, luci.uvl.option(
761 self.scheme, self.config, self.sref[2],
762 self.cref[1], self.cref[2], o
770 --- Object representation of a scheme/config option.
773 -- @name luci.uvl.option
775 --- Section instance constructor.
778 -- @param scheme Scheme instance
779 -- @param co Configuration data
780 -- @param st Section type
781 -- @param c Configuration name
782 -- @param s Section name
783 -- @param o Option name
784 -- @return Option instance
785 option = luci.util.class()
787 function option.__init__(self, scheme, co, st, c, s, o)
788 self.coption = co[s] and co[s][o] or nil
789 self.soption = scheme.packages[c].variables[st][o]
790 self.cref = { c, s, o }
791 self.sref = { c, st, o }
794 self.type = luci.uvl.TYPE_OPTION
797 --- Get the config path of this option.
798 -- @return String containing the identifier
799 function option.cid(self)
800 return ( self.cref[1] or '?' ) .. '.' ..
801 ( self.cref[2] or '?' ) .. '.' ..
802 ( self.cref[3] or '?' )
805 --- Get the scheme path of this option.
806 -- @return String containing the identifier
807 function option.sid(self)
808 return ( self.sref[1] or '?' ) .. '.' ..
809 ( self.sref[2] or '?' ) .. '.' ..
810 ( self.sref[3] or '?' )
813 --- Get the value of this option.
814 -- @return The associated configuration value
815 function option.value(self)
819 --- Get the associated option information in scheme.
820 -- @return Table containing the scheme properties
821 function option.option(self)
825 --- Get the associated section information in scheme.
826 -- @return Table containing the scheme properties
827 function option.section(self)
828 return self.scheme.packages[self.sref[1]].sections[self.sref[2]]