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.errors")
28 require("luci.uvl.datatypes")
29 require("luci.uvl.validation")
30 require("luci.uvl.dependencies")
40 --- Boolean; default true;
41 -- treat sections found in config but not in scheme as error
42 STRICT_UNKNOWN_SECTIONS = true
44 --- Boolean; default true;
45 -- treat options found in config but not in scheme as error
46 STRICT_UNKNOWN_OPTIONS = true
48 --- Boolean; default true;
49 -- treat failed external validators as error
50 STRICT_EXTERNAL_VALIDATORS = true
52 --- Boolean; default true;
53 -- treat list values stored as options like errors
54 STRICT_LIST_TYPE = true
57 local default_schemedir = "/lib/uci/schema"
58 local ERR = luci.uvl.errors
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.err = luci.uvl.errors
74 self.dep = luci.uvl.dependencies
75 self.datatypes = luci.uvl.datatypes
79 --- Parse given scheme and return the scheme tree.
80 -- @param scheme Name of the scheme to parse
81 -- @return Table containing the parsed scheme or nil on error
82 -- @return String containing the reason for errors (if any)
83 function UVL.get_scheme( self, scheme )
84 if not self.packages[scheme] then
85 local ok, err = self:read_scheme( scheme )
90 return self.packages[scheme], nil
93 --- Validate given configuration, section or option.
94 -- @param config Name of the configuration to validate
95 -- @param section Name of the section to validate (optional)
96 -- @param option Name of the option to validate (optional)
97 -- @return Boolean indicating whether the given config validates
98 -- @return String containing the reason for errors (if any)
99 function UVL.validate( self, config, section, option )
100 if config and section and option then
101 return self:validate_option( config, section, option )
102 elseif config and section then
103 return self:validate_section( config, section )
105 return self:validate_config( config )
109 --- Validate given configuration.
110 -- @param config Name of the configuration to validate
111 -- @return Boolean indicating whether the given config validates
112 -- @return String containing the reason for errors (if any)
113 function UVL.validate_config( self, config, uci )
115 if not self.packages[config] then
116 local ok, err = self:read_scheme(config)
122 local co = luci.uvl.config( self, uci or config, uci and config )
127 if not co:config() then
128 return false, ERR.UCILOAD(co)
131 local function _uci_foreach( type, func )
132 for k, v in pairs(co:config()) do
133 if v['.type'] == type then
134 sc[type] = sc[type] + 1
135 local ok, err = func( k, v )
136 if not ok then co:error(err) end
141 for k, v in pairs( self.packages[config].sections ) do
145 return self:_validate_section( co:section(s) )
150 if STRICT_UNKNOWN_SECTIONS then
151 for k, v in pairs(co:config()) do
152 local so = co:section(k)
153 if not self.beenthere[so:cid()] then
154 co:error(ERR.SECT_UNKNOWN(so))
159 for _, k in ipairs(luci.util.keys(sc)) do
160 local so = co:section(k)
161 if so:scheme('required') and sc[k] == 0 then
162 co:error(ERR.SECT_REQUIRED(so))
163 elseif so:scheme('unique') and sc[k] > 1 then
164 co:error(ERR.SECT_UNIQUE(so))
168 return co:ok(), co:errors()
171 --- Validate given config section.
172 -- @param config Name of the configuration to validate
173 -- @param section Name of the section to validate
174 -- @return Boolean indicating whether the given config validates
175 -- @return String containing the reason for errors (if any)
176 function UVL.validate_section( self, config, section, uci )
178 if not self.packages[config] then
179 local ok, err = self:read_scheme( config )
185 local co = luci.uvl.config( self, uci or config, uci and config )
186 local so = co:section( section )
190 if not co:config() then
191 return false, ERR.UCILOAD(co)
195 return self:_validate_section( so )
197 return false, ERR.SECT_NOTFOUND(so)
201 --- Validate given config option.
202 -- @param config Name of the configuration to validate
203 -- @param section Name of the section to validate
204 -- @param option Name of the option to validate
205 -- @return Boolean indicating whether the given config validates
206 -- @return String containing the reason for errors (if any)
207 function UVL.validate_option( self, config, section, option, uci )
209 if not self.packages[config] then
210 local ok, err = self:read_scheme( config )
216 local co = luci.uvl.config( self, uci or config, uci and config )
217 local so = co:section( section )
218 local oo = so:option( option )
221 return false, oerr:child(ERR.UCILOAD(config))
224 if so:config() and oo:config() then
225 return self:_validate_option( oo )
227 return false, ERR.OPT_NOTFOUND(oo)
232 function UVL._validate_section( self, section )
234 if section:config() then
235 if section:scheme('named') == true and
236 section:config('.anonymous') == true
238 return false, ERR.SECT_NAMED(section)
241 for _, v in ipairs(section:variables()) do
242 local ok, err = self:_validate_option( v )
248 local ok, err = luci.uvl.dependencies.check( self, section )
253 return false, ERR.SECT_NOTFOUND(section)
256 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
257 for k, v in pairs(section:config()) do
258 local oo = section:option(k)
259 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
260 section:error(ERR.OPT_NOTFOUND(oo))
265 return section:ok(), section:errors()
268 function UVL._validate_option( self, option, nodeps )
270 if not option:scheme() and not option:parent():scheme('dynamic') then
271 return false, option:error(ERR.OPT_UNKNOWN(option))
273 elseif option:scheme() then
274 if option:scheme('required') and not option:value() then
275 return false, option:error(ERR.OPT_REQUIRED(option))
277 elseif option:value() then
278 local val = option:value()
280 if option:scheme('type') == "reference" or
281 option:scheme('type') == "enum"
283 if not option:scheme('values') or
284 not option:scheme('values')[val]
286 return false, option:error( ERR.OPT_BADVALUE(
287 option, { val, table.concat(
288 luci.util.keys(option:scheme('values') or {}), ", "
292 elseif option:scheme('type') == "list" then
293 if type(val) ~= "table" and STRICT_LIST_TYPE then
294 return false, option:error(ERR.OPT_NOTLIST(option))
296 elseif option:scheme('datatype') then
297 local dt = option:scheme('datatype')
299 if self.datatypes[dt] then
300 val = ( type(val) == "table" and val or { val } )
301 for i, v in ipairs(val) do
302 if not self.datatypes[dt]( v ) then
303 return false, option:error(
304 ERR.OPT_INVVALUE(option, {v, dt})
309 return false, option:error(ERR.OPT_DATATYPE(option, dt))
315 local ok, err = luci.uvl.dependencies.check( self, option )
321 local ok, err = luci.uvl.validation.check( self, option )
322 if not ok and STRICT_EXTERNAL_VALIDATORS then
323 return false, option:error(err)
327 return option:ok(), option:errors()
330 --- Find all parts of given scheme and construct validation tree.
331 -- This is normally done on demand, so you don't have to call this function
333 -- @param scheme Name of the scheme to parse
334 function UVL.read_scheme( self, scheme )
336 local so = luci.uvl.scheme( self, scheme )
339 local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
342 for i, file in ipairs( files ) do
343 if not luci.fs.access(file) then
344 return so:error(ERR.SME_READ(so,file))
347 local uci = luci.model.uci.cursor()
348 uci:set_confdir( luci.fs.dirname(file) )
350 local sd = uci:get_all( luci.fs.basename(file) )
353 return false, ERR.UCILOAD(so)
356 table.insert( schemes, sd )
359 return self:_read_scheme_parts( so, schemes )
361 return false, so:error(ERR.SME_FIND(so, self.schemedir))
365 -- Process all given parts and construct validation tree
366 function UVL._read_scheme_parts( self, scheme, schemes )
368 -- helper function to check for required fields
369 local function _req( c, t, r )
370 for i, v in ipairs(r) do
372 return false, ERR.SME_REQFLD({c,t}, v)
378 -- helper function to validate references
379 local function _ref( c, t )
381 if c == TYPE_SECTION then
384 elseif c == TYPE_VARIABLE then
387 elseif c == TYPE_ENUM then
392 local r = luci.util.split( t[k], "." )
393 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
396 return false, ERR.SME_BADREF(scheme, k)
402 -- helper function to read bools
403 local function _bool( v )
404 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
410 -- Step 1: get all sections
411 for i, conf in ipairs( schemes ) do
412 for k, v in pairs( conf ) do
413 if v['.type'] == 'section' then
415 ok, err = _req( TYPE_SECTION, v, { "name", "package" } )
416 if err then return false, scheme:error(err) end
418 local r, err = _ref( TYPE_SECTION, v )
419 if err then return false, scheme:error(err) end
421 self.packages[r[1]] =
422 self.packages[r[1]] or {
428 local p = self.packages[r[1]]
429 p.sections[v.name] = p.sections[v.name] or { }
430 p.variables[v.name] = p.variables[v.name] or { }
432 local s = p.sections[v.name]
433 local so = scheme:section(v.name)
435 for k, v2 in pairs(v) do
436 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
437 if k == "depends" then
438 s.depends = self:_read_dependency( v2, s.depends )
439 if not s.depends then
440 return false, scheme:error(
441 ERR.SME_BADDEP(so, luci.util.serialize_data(s.depends))
444 elseif k == "dynamic" or k == "unique" or
445 k == "required" or k == "named"
454 s.dynamic = s.dynamic or false
455 s.unique = s.unique or false
456 s.required = s.required or false
457 s.named = s.named or false
462 -- Step 2: get all variables
463 for i, conf in ipairs( schemes ) do
464 for k, v in pairs( conf ) do
465 if v['.type'] == "variable" then
467 ok, err = _req( TYPE_VARIABLE, v, { "name", "section" } )
468 if err then return false, scheme:error(err) end
470 local r, err = _ref( TYPE_VARIABLE, v )
471 if err then return false, scheme:error(err) end
473 local p = self.packages[r[1]]
475 return false, scheme:error(
476 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
480 local s = p.variables[r[2]]
482 return false, scheme:error(
483 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
487 s[v.name] = s[v.name] or { }
490 local so = scheme:section(r[2])
491 local to = so:option(v.name)
493 for k, v2 in pairs(v) do
494 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
495 if k == "depends" then
496 t.depends = self:_read_dependency( v2, t.depends )
497 if not t.depends then
498 return false, scheme:error(so:error(
499 ERR.SME_BADDEP(to, luci.util.serialize_data(v2))
502 elseif k == "validator" then
503 t.validators = self:_read_validator( v2, t.validators )
504 if not t.validators then
505 return false, scheme:error(so:error(
506 ERR.SME_BADVAL(to, luci.util.serialize_data(v2))
509 elseif k == "valueof" then
510 local values, err = self:_read_reference( v2 )
512 return false, scheme:error(so:error(
513 ERR.REFERENCE(to, luci.util.serialize_data(v2)):child(err)
518 elseif k == "required" then
526 t.type = t.type or "variable"
527 t.datatype = t.datatype or "string"
528 t.required = t.required or false
533 -- Step 3: get all enums
534 for i, conf in ipairs( schemes ) do
535 for k, v in pairs( conf ) do
536 if v['.type'] == "enum" then
538 ok, err = _req( TYPE_ENUM, v, { "value", "variable" } )
539 if err then return false, scheme:error(err) end
541 local r, err = _ref( TYPE_ENUM, v )
542 if err then return false, scheme:error(err) end
544 local p = self.packages[r[1]]
546 return false, scheme:error(
547 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
551 local s = p.variables[r[2]]
553 return false, scheme:error(
554 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
560 return false, scheme:error(
561 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
566 local so = scheme:section(r[2])
567 local oo = so:option(r[3])
568 local eo = oo:enum(v.value)
570 if t.type ~= "enum" then
571 return false, scheme:error(ERR.SME_EBADTYPE(eo))
575 t.values = { [v.value] = v.title or v.value }
577 t.values[v.value] = v.title or v.value
580 if not t.enum_depends then
586 return false, scheme:error(ERR.SME_EBADDEF(eo))
592 t.enum_depends[v.value] = self:_read_dependency(
593 v.depends, t.enum_depends[v.value]
596 if not t.enum_depends[v.value] then
597 return false, scheme:error(so:error(oo:error(
598 ERR.SME_BADDEP(eo, luci.util.serialize_data(v.depends))
609 -- Read a dependency specification
610 function UVL._read_dependency( self, values, deps )
611 local expr = "%$?[a-zA-Z0-9_]+"
613 values = ( type(values) == "table" and values or { values } )
614 for _, value in ipairs(values) do
615 local parts = luci.util.split( value, "%s*,%s*", nil, true )
616 local condition = { }
617 for i, val in ipairs(parts) do
618 local k, v = unpack(luci.util.split(val, "%s*=%s*", nil, true))
621 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
622 k:match("^"..expr.."%."..expr.."$") or
623 k:match("^"..expr.."$")
625 condition[k] = v or true
634 table.insert( deps, condition )
642 -- Read a validator specification
643 function UVL._read_validator( self, values, validators )
645 values = ( type(values) == "table" and values or { values } )
646 for _, value in ipairs(values) do
649 if value:match("^exec:") then
650 validator = value:gsub("^exec:","")
651 elseif value:match("^lua:") then
652 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
656 if not validators then
657 validators = { validator }
659 table.insert( validators, validator )
670 -- Read a reference specification (XXX: We should validate external configs too...)
671 function UVL._read_reference( self, values )
673 values = ( type(values) == "table" and values or { values } )
675 for _, value in ipairs(values) do
676 local ref = luci.util.split(value, ".")
678 if #ref == 2 or #ref == 3 then
679 local co = luci.uvl.config( self, ref[1] )
680 if not co:config() then return false, ERR.UCILOAD(ref[1]) end
682 for k, v in pairs(co:config()) do
683 if v['.type'] == ref[2] then
685 if v['.anonymous'] == true then
686 return false, ERR.SME_INVREF('', value)
688 val[k] = k -- XXX: title/description would be nice
689 elseif v[ref[3]] then
690 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
695 return false, ERR.SME_BADREF('', value)
702 -- Resolve given path
703 function UVL._resolve_function( self, value )
704 local path = luci.util.split(value, ".")
707 local stat, mod = luci.util.copcall(
708 require, table.concat(path, ".", 1, i)
712 for j=i+1, #path-1 do
713 if not type(mod) == "table" then
721 mod = type(mod) == "table" and mod[path[#path]] or nil
722 if type(mod) == "function" then
730 --- Object representation of an uvl item - base class.
731 uvlitem = luci.util.class()
733 function uvlitem.cid(self)
734 return table.concat( self.cref, '.' )
737 function uvlitem.sid(self)
738 return table.concat( self.sref, '.' )
741 function uvlitem.scheme(self, opt)
744 if #self.sref == 4 or #self.sref == 3 then
746 .packages[self.sref[1]]
747 .variables[self.sref[2]][self.sref[3]]
748 elseif #self.sref == 2 then
750 .packages[self.sref[1]]
751 .sections[self.sref[2]]
754 .packages[self.sref[1]]
764 function uvlitem.config(self, opt)
767 if #self.cref == 4 or #self.cref == 3 then
768 c = self.c[self.cref[2]][self.cref[3]]
769 elseif #self.cref == 2 then
770 c = self.c[self.cref[2]]
782 function uvlitem.title(self)
783 return self:scheme() and self:scheme('title') or
784 self.cref[3] or self.cref[2] or self.cref[1]
787 function uvlitem.type(self)
788 if self.t == luci.uvl.TYPE_CONFIG then
790 elseif self.t == luci.uvl.TYPE_SECTION then
792 elseif self.t == luci.uvl.TYPE_OPTION then
794 elseif self.t == luci.uvl.TYPE_ENUM then
799 function uvlitem.error(self, ...)
801 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
802 self.e = errconst[#self.cref]( self )
805 return self.e:child( ... )
808 function uvlitem.errors(self)
812 function uvlitem.ok(self)
813 return not self:errors()
816 function uvlitem.parent(self)
819 elseif #self.cref == 3 or #self.cref == 4 then
820 return luci.uvl.section( self.s, self.c, self.cref[1], self.cref[2] )
821 elseif #self.cref == 2 then
822 return luci.uvl.config( self.s, self.c, self.cref[1] )
829 --- Object representation of a scheme.
832 -- @name luci.uvl.scheme
834 --- Scheme instance constructor.
837 -- @param scheme Scheme instance
838 -- @param co Configuration data
839 -- @param c Configuration name
840 -- @return Config instance
841 scheme = luci.util.class(uvlitem)
843 function scheme.__init__(self, scheme, co, c)
849 local uci = luci.model.uci.cursor()
857 self.t = luci.uvl.TYPE_SCHEME
860 --- Add an error to scheme.
861 -- @return Scheme error context
862 function scheme.error(self, ...)
863 if not self.e then self.e = ERR.SCHEME( self ) end
864 return self.e:child( ... )
867 --- Get an associated config object.
868 -- @return Config instance
869 function scheme.config(self)
870 local co = luci.uvl.config( self.s, self.cref[1] )
876 --- Get all section objects associated with this scheme.
877 -- @return Table containing all associated luci.uvl.section instances
878 function scheme.sections(self)
880 if self.s.packages[self.sref[1]].sections then
881 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
882 table.insert( v, luci.uvl.option(
883 self.s, self.c, self.cref[1], self.cref[2], o
890 --- Get an associated section object.
891 -- @param s Section to select
892 -- @return Section instance
893 function scheme.section(self, s)
894 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
901 --- Object representation of a config.
904 -- @name luci.uvl.config
906 --- Config instance constructor.
909 -- @param scheme Scheme instance
910 -- @param co Configuration data
911 -- @param c Configuration name
912 -- @return Config instance
913 config = luci.util.class(uvlitem)
915 function config.__init__(self, scheme, co, c)
921 local uci = luci.model.uci.cursor()
929 self.t = luci.uvl.TYPE_CONFIG
932 --- Get all section objects associated with this config.
933 -- @return Table containing all associated luci.uvl.section instances
934 function config.sections(self)
936 if self.s.packages[self.sref[1]].sections then
937 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
938 table.insert( v, luci.uvl.option(
939 self.s, self.c, self.cref[1], self.cref[2], o
946 --- Get an associated section object.
947 -- @param s Section to select
948 -- @return Section instance
949 function config.section(self, s)
950 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
957 --- Object representation of a scheme/config section.
960 -- @name luci.uvl.section
962 --- Section instance constructor.
965 -- @param scheme Scheme instance
966 -- @param co Configuration data
967 -- @param c Configuration name
968 -- @param s Section name
969 -- @return Section instance
970 section = luci.util.class(uvlitem)
972 function section.__init__(self, scheme, co, c, s)
974 self.sref = { c, co and co[s] and co[s]['.type'] or s }
977 self.t = luci.uvl.TYPE_SECTION
980 --- Get all option objects associated with this section.
981 -- @return Table containing all associated luci.uvl.option instances
982 function section.variables(self)
984 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
986 self.s.packages[self.sref[1]].variables[self.sref[2]]
988 table.insert( v, luci.uvl.option(
989 self.s, self.c, self.cref[1], self.cref[2], o
996 --- Get an associated option object.
997 -- @param o Option to select
998 -- @return Option instance
999 function section.option(self, o)
1000 local oo = luci.uvl.option( self.s, self.c, self.cref[1], self.cref[2], o )
1007 --- Object representation of a scheme/config option.
1010 -- @name luci.uvl.option
1012 --- Section instance constructor.
1015 -- @param scheme Scheme instance
1016 -- @param co Configuration data
1017 -- @param c Configuration name
1018 -- @param s Section name
1019 -- @param o Option name
1020 -- @return Option instance
1021 option = luci.util.class(uvlitem)
1023 function option.__init__(self, scheme, co, c, s, o)
1024 self.cref = { c, s, o }
1025 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1028 self.t = luci.uvl.TYPE_OPTION
1031 --- Get the value of this option.
1032 -- @return The associated configuration value
1033 function option.value(self)
1034 return self:config()
1037 --- Get the associated section information in scheme.
1038 -- @return Table containing the scheme properties
1039 function option.section(self)
1040 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1043 --- Construct an enum object instance from given or default value.
1044 -- @param v Value to select
1045 -- @return Enum instance for selected value
1046 function option.enum(self, val)
1049 self.cref[1], self.cref[2], self.cref[3],
1055 --- Object representation of a enum value.
1058 -- @name luci.uvl.enum
1060 --- Section instance constructor.
1063 -- @param scheme Scheme instance
1064 -- @param co Configuration data
1065 -- @param c Configuration name
1066 -- @param s Section name
1067 -- @param o Enum name
1068 -- @param v Enum value
1069 -- @return Enum value instance
1070 enum = luci.util.class(option)
1072 function enum.__init__(self, scheme, co, c, s, o, v)
1073 self.cref = { c, s, o, v }
1074 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1077 self.t = luci.uvl.TYPE_ENUM