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 default_savedir = "/tmp/.uvl"
59 local ERR = luci.uvl.errors
62 --- Object constructor
65 -- @param schemedir Path to the scheme directory (optional)
66 -- @return Instance object
67 UVL = luci.util.class()
69 function UVL.__init__( self, schemedir )
70 self.schemedir = schemedir or default_schemedir
74 self.uci = luci.model.uci
75 self.err = luci.uvl.errors
76 self.dep = luci.uvl.dependencies
77 self.datatypes = luci.uvl.datatypes
81 --- Parse given scheme and return the scheme tree.
82 -- @param scheme Name of the scheme to parse
83 -- @return Table containing the parsed scheme or nil on error
84 -- @return String containing the reason for errors (if any)
85 function UVL.get_scheme( self, scheme )
86 if not self.packages[scheme] then
87 local ok, err = self:read_scheme( scheme )
92 return self.packages[scheme], nil
95 --- Validate given configuration, section or option.
96 -- @param config Name of the configuration to validate
97 -- @param section Name of the section to validate (optional)
98 -- @param option Name of the option to validate (optional)
99 -- @return Boolean indicating whether the given config validates
100 -- @return String containing the reason for errors (if any)
101 function UVL.validate( self, config, section, option )
102 if config and section and option then
103 return self:validate_option( config, section, option )
104 elseif config and section then
105 return self:validate_section( config, section )
107 return self:validate_config( config )
111 --- Validate given configuration.
112 -- @param config Name of the configuration to validate
113 -- @return Boolean indicating whether the given config validates
114 -- @return String containing the reason for errors (if any)
115 function UVL.validate_config( self, config, uci )
117 if not self.packages[config] then
118 local ok, err = self:read_scheme(config)
124 local co = luci.uvl.config( self, uci or config, uci and config )
130 if not co:config() then
131 return false, co:errors()
134 local function _uci_foreach( type, func )
135 for k, v in pairs(co:config()) do
136 if v['.type'] == type then
137 sc[type] = sc[type] + 1
138 local ok, err = func( k, v )
139 if not ok then co:error(err) end
144 for k, v in pairs( self.packages[config].sections ) do
148 return self:_validate_section( co:section(s) )
153 if STRICT_UNKNOWN_SECTIONS then
154 for k, v in pairs(co:config()) do
155 local so = co:section(k)
156 if not self.beenthere[so:cid()] then
157 co:error(ERR.SECT_UNKNOWN(so))
162 for _, k in ipairs(luci.util.keys(sc)) do
163 local so = co:section(k)
164 if so:scheme('required') and sc[k] == 0 then
165 co:error(ERR.SECT_REQUIRED(so))
166 elseif so:scheme('unique') and sc[k] > 1 then
167 co:error(ERR.SECT_UNIQUE(so))
171 return co:ok(), co:errors()
174 --- Validate given config section.
175 -- @param config Name of the configuration to validate
176 -- @param section Name of the section to validate
177 -- @return Boolean indicating whether the given config validates
178 -- @return String containing the reason for errors (if any)
179 function UVL.validate_section( self, config, section, uci )
181 if not self.packages[config] then
182 local ok, err = self:read_scheme( config )
188 local co = luci.uvl.config( self, uci or config, uci and config )
189 local so = co:section( section )
194 if not co:config() then
195 return false, co:errors()
199 return self:_validate_section( so )
201 return false, ERR.SECT_NOTFOUND(so)
205 --- Validate given config option.
206 -- @param config Name of the configuration to validate
207 -- @param section Name of the section to validate
208 -- @param option Name of the option to validate
209 -- @return Boolean indicating whether the given config validates
210 -- @return String containing the reason for errors (if any)
211 function UVL.validate_option( self, config, section, option, uci )
213 if not self.packages[config] then
214 local ok, err = self:read_scheme( config )
220 local co = luci.uvl.config( self, uci or config, uci and config )
221 local so = co:section( section )
222 local oo = so:option( option )
224 if not co:config() then
225 return false, co:errors()
228 if so:config() and oo:config() then
229 return self:_validate_option( oo )
231 return false, ERR.OPT_NOTFOUND(oo)
236 function UVL._validate_section( self, section )
238 self.beenthere[section:cid()] = true
240 if section:config() then
241 if section:scheme('named') == true and
242 section:config('.anonymous') == true
244 return false, ERR.SECT_NAMED(section)
247 for _, v in ipairs(section:variables()) do
248 local ok, err = self:_validate_option( v )
250 v:scheme('required') or v:scheme('type') == "enum" or (
251 not err:is(ERR.ERR_DEP_NOTEQUAL) and
252 not err:is(ERR.ERR_DEP_NOVALUE)
259 local ok, err = luci.uvl.dependencies.check( self, section )
264 return false, ERR.SECT_NOTFOUND(section)
267 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
268 for k, v in pairs(section:config()) do
269 local oo = section:option(k)
270 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
271 section:error(ERR.OPT_UNKNOWN(oo))
276 return section:ok(), section:errors()
279 function UVL._validate_option( self, option, nodeps )
281 self.beenthere[option:cid()] = true
283 if not option:scheme() and not option:parent():scheme('dynamic') then
284 return false, option:error(ERR.OPT_UNKNOWN(option))
286 elseif option:scheme() then
287 if option:scheme('required') and not option:value() then
288 return false, option:error(ERR.OPT_REQUIRED(option))
290 elseif option:value() then
291 local val = option:value()
293 if option:scheme('type') == "reference" or
294 option:scheme('type') == "enum"
296 if not option:scheme('values') or
297 not option:scheme('values')[val]
299 return false, option:error( ERR.OPT_BADVALUE(
300 option, luci.util.serialize_data(
301 luci.util.keys(option:scheme('values') or {})
305 elseif option:scheme('type') == "list" then
306 if type(val) ~= "table" and STRICT_LIST_TYPE then
307 return false, option:error(ERR.OPT_NOTLIST(option))
309 elseif option:scheme('datatype') then
310 local dt = option:scheme('datatype')
312 if self.datatypes[dt] then
313 val = ( type(val) == "table" and val or { val } )
314 for i, v in ipairs(val) do
315 if not self.datatypes[dt]( v ) then
316 return false, option:error(
317 ERR.OPT_INVVALUE(option, dt)
322 return false, option:error(ERR.OPT_DATATYPE(option, dt))
328 local ok, err = luci.uvl.dependencies.check( self, option )
334 local ok, err = luci.uvl.validation.check( self, option )
335 if not ok and STRICT_EXTERNAL_VALIDATORS then
336 return false, option:error(err)
340 return option:ok(), option:errors()
343 --- Find all parts of given scheme and construct validation tree.
344 -- This is normally done on demand, so you don't have to call this function
346 -- @param scheme Name of the scheme to parse
347 function UVL.read_scheme( self, scheme )
349 local so = luci.uvl.scheme( self, scheme )
352 local files = luci.fs.glob(self.schemedir .. '/*/' .. scheme)
355 for i, file in ipairs( files ) do
356 if not luci.fs.access(file) then
357 return so:error(ERR.SME_READ(so,file))
360 local uci = luci.model.uci.cursor( luci.fs.dirname(file), default_savedir )
362 local sd, err = uci:get_all( luci.fs.basename(file) )
365 return false, ERR.UCILOAD(so, err)
368 table.insert( schemes, sd )
371 return self:_read_scheme_parts( so, schemes )
373 return false, so:error(ERR.SME_FIND(so, self.schemedir))
377 -- Process all given parts and construct validation tree
378 function UVL._read_scheme_parts( self, scheme, schemes )
380 -- helper function to check for required fields
381 local function _req( c, t, r )
382 for i, v in ipairs(r) do
384 return false, ERR.SME_REQFLD({c,t}, v)
390 -- helper function to validate references
391 local function _ref( c, t )
393 if c == TYPE_SECTION then
396 elseif c == TYPE_VARIABLE then
399 elseif c == TYPE_ENUM then
404 local r = luci.util.split( t[k], "." )
405 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
408 return false, ERR.SME_BADREF(scheme, k)
414 -- helper function to read bools
415 local function _bool( v )
416 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
422 -- Step 0: get package meta information
423 for i, conf in ipairs( schemes ) do
424 for k, v in pairs( conf ) do
425 if v['.type'] == 'package' then
426 self.packages[scheme:sid()] =
427 self.packages[scheme:sid()] or {
428 ["name"] = scheme:sid();
433 for k, v2 in pairs(v) do
434 if k == "title" or k == "description" then
435 self.packages[scheme:sid()][k] = v2
442 -- Step 1: get all sections
443 for i, conf in ipairs( schemes ) do
444 for k, v in pairs( conf ) do
445 if v['.type'] == 'section' then
447 ok, err = _req( TYPE_SECTION, v, { "name", "package" } )
448 if err then return false, scheme:error(err) end
450 local r, err = _ref( TYPE_SECTION, v )
451 if err then return false, scheme:error(err) end
453 self.packages[r[1]] =
454 self.packages[r[1]] or {
460 local p = self.packages[r[1]]
461 p.sections[v.name] = p.sections[v.name] or { }
462 p.variables[v.name] = p.variables[v.name] or { }
464 local s = p.sections[v.name]
465 local so = scheme:section(v.name)
467 for k, v2 in pairs(v) do
468 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
469 if k == "depends" then
470 s.depends = self:_read_dependency( v2, s.depends )
471 if not s.depends then
472 return false, scheme:error(
473 ERR.SME_BADDEP(so, luci.util.serialize_data(s.depends))
476 elseif k == "dynamic" or k == "unique" or
477 k == "required" or k == "named"
486 s.dynamic = s.dynamic or false
487 s.unique = s.unique or false
488 s.required = s.required or false
489 s.named = s.named or false
494 -- Step 2: get all variables
495 for i, conf in ipairs( schemes ) do
496 for k, v in pairs( conf ) do
497 if v['.type'] == "variable" then
499 ok, err = _req( TYPE_VARIABLE, v, { "name", "section" } )
500 if err then return false, scheme:error(err) end
502 local r, err = _ref( TYPE_VARIABLE, v )
503 if err then return false, scheme:error(err) end
505 local p = self.packages[r[1]]
507 return false, scheme:error(
508 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
512 local s = p.variables[r[2]]
514 return false, scheme:error(
515 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
519 s[v.name] = s[v.name] or { }
522 local so = scheme:section(r[2])
523 local to = so:option(v.name)
525 for k, v2 in pairs(v) do
526 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
527 if k == "depends" then
528 t.depends = self:_read_dependency( v2, t.depends )
529 if not t.depends then
530 return false, scheme:error(so:error(
531 ERR.SME_BADDEP(to, luci.util.serialize_data(v2))
534 elseif k == "validator" then
535 t.validators = self:_read_validator( v2, t.validators )
536 if not t.validators then
537 return false, scheme:error(so:error(
538 ERR.SME_BADVAL(to, luci.util.serialize_data(v2))
541 elseif k == "valueof" then
542 local values, err = self:_read_reference( v2 )
544 return false, scheme:error(so:error(
545 ERR.REFERENCE(to, luci.util.serialize_data(v2)):child(err)
550 elseif k == "required" then
558 t.type = t.type or "variable"
559 t.datatype = t.datatype or "string"
560 t.required = t.required or false
565 -- Step 3: get all enums
566 for i, conf in ipairs( schemes ) do
567 for k, v in pairs( conf ) do
568 if v['.type'] == "enum" then
570 ok, err = _req( TYPE_ENUM, v, { "value", "variable" } )
571 if err then return false, scheme:error(err) end
573 local r, err = _ref( TYPE_ENUM, v )
574 if err then return false, scheme:error(err) end
576 local p = self.packages[r[1]]
578 return false, scheme:error(
579 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
583 local s = p.variables[r[2]]
585 return false, scheme:error(
586 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
592 return false, scheme:error(
593 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
598 local so = scheme:section(r[2])
599 local oo = so:option(r[3])
600 local eo = oo:enum(v.value)
602 if t.type ~= "enum" then
603 return false, scheme:error(ERR.SME_EBADTYPE(eo))
607 t.values = { [v.value] = v.title or v.value }
609 t.values[v.value] = v.title or v.value
612 if not t.enum_depends then
618 return false, scheme:error(ERR.SME_EBADDEF(eo))
624 t.enum_depends[v.value] = self:_read_dependency(
625 v.depends, t.enum_depends[v.value]
628 if not t.enum_depends[v.value] then
629 return false, scheme:error(so:error(oo:error(
630 ERR.SME_BADDEP(eo, luci.util.serialize_data(v.depends))
641 -- Read a dependency specification
642 function UVL._read_dependency( self, values, deps )
643 local expr = "%$?[a-zA-Z0-9_]+"
645 values = ( type(values) == "table" and values or { values } )
646 for _, value in ipairs(values) do
647 local parts = luci.util.split( value, "%s*,%s*", nil, true )
648 local condition = { }
649 for i, val in ipairs(parts) do
650 local k, v = unpack(luci.util.split(val, "%s*=%s*", nil, true))
653 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
654 k:match("^"..expr.."%."..expr.."$") or
655 k:match("^"..expr.."$")
657 condition[k] = v or true
666 table.insert( deps, condition )
674 -- Read a validator specification
675 function UVL._read_validator( self, values, validators )
677 values = ( type(values) == "table" and values or { values } )
678 for _, value in ipairs(values) do
681 if value:match("^exec:") then
682 validator = value:gsub("^exec:","")
683 elseif value:match("^lua:") then
684 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
688 if not validators then
689 validators = { validator }
691 table.insert( validators, validator )
702 -- Read a reference specification (XXX: We should validate external configs too...)
703 function UVL._read_reference( self, values )
705 values = ( type(values) == "table" and values or { values } )
707 for _, value in ipairs(values) do
708 local ref = luci.util.split(value, ".")
710 if #ref == 2 or #ref == 3 then
711 local co = luci.uvl.config( self, ref[1] )
712 if not co:config() then return false, co:errors() end
714 for k, v in pairs(co:config()) do
715 if v['.type'] == ref[2] then
717 if v['.anonymous'] == true then
718 return false, ERR.SME_INVREF('', value)
720 val[k] = k -- XXX: title/description would be nice
721 elseif v[ref[3]] then
722 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
727 return false, ERR.SME_BADREF('', value)
734 -- Resolve given path
735 function UVL._resolve_function( self, value )
736 local path = luci.util.split(value, ".")
739 local stat, mod = luci.util.copcall(
740 require, table.concat(path, ".", 1, i)
744 for j=i+1, #path-1 do
745 if not type(mod) == "table" then
753 mod = type(mod) == "table" and mod[path[#path]] or nil
754 if type(mod) == "function" then
762 --- Object representation of an uvl item - base class.
763 uvlitem = luci.util.class()
765 function uvlitem.cid(self)
766 return table.concat( self.cref, '.' )
769 function uvlitem.sid(self)
770 return table.concat( self.sref, '.' )
773 function uvlitem.scheme(self, opt)
776 if #self.sref == 4 or #self.sref == 3 then
778 .packages[self.sref[1]]
779 .variables[self.sref[2]][self.sref[3]]
780 elseif #self.sref == 2 then
782 .packages[self.sref[1]]
783 .sections[self.sref[2]]
786 .packages[self.sref[1]]
796 function uvlitem.config(self, opt)
799 if #self.cref == 4 or #self.cref == 3 then
800 c = self.c and self.c[self.cref[2]] or nil
801 c = c and c[self.cref[3]] or nil
802 elseif #self.cref == 2 then
803 c = self.c and self.c[self.cref[2]] or nil
815 function uvlitem.title(self)
816 return self:scheme() and self:scheme('title') or
817 self.cref[3] or self.cref[2] or self.cref[1]
820 function uvlitem.type(self)
821 if self.t == luci.uvl.TYPE_CONFIG then
823 elseif self.t == luci.uvl.TYPE_SECTION then
825 elseif self.t == luci.uvl.TYPE_OPTION then
827 elseif self.t == luci.uvl.TYPE_ENUM then
832 function uvlitem.error(self, ...)
834 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
835 self.e = errconst[#self.cref]( self )
838 return self.e:child( ... )
841 function uvlitem.errors(self)
845 function uvlitem.ok(self)
846 return not self:errors()
849 function uvlitem.parent(self)
852 elseif #self.cref == 3 or #self.cref == 4 then
853 return luci.uvl.section( self.s, self.c, self.cref[1], self.cref[2] )
854 elseif #self.cref == 2 then
855 return luci.uvl.config( self.s, self.c, self.cref[1] )
861 function uvlitem._loadconf(self, co, c)
863 local uci, err = luci.model.uci.cursor(), nil
864 co, err = uci:get_all(c)
867 self:error(ERR.UCILOAD(self, err))
874 --- Object representation of a scheme.
877 -- @name luci.uvl.scheme
879 --- Scheme instance constructor.
882 -- @param scheme Scheme instance
883 -- @param co Configuration data
884 -- @param c Configuration name
885 -- @return Config instance
886 scheme = luci.util.class(uvlitem)
888 function scheme.__init__(self, scheme, co, c)
895 self.c = self:_loadconf(co, c)
897 self.t = luci.uvl.TYPE_SCHEME
900 --- Add an error to scheme.
901 -- @return Scheme error context
902 function scheme.error(self, ...)
903 if not self.e then self.e = ERR.SCHEME( self ) end
904 return self.e:child( ... )
907 --- Get an associated config object.
908 -- @return Config instance
909 function scheme.config(self)
910 local co = luci.uvl.config( self.s, self.cref[1] )
916 --- Get all section objects associated with this scheme.
917 -- @return Table containing all associated luci.uvl.section instances
918 function scheme.sections(self)
920 if self.s.packages[self.sref[1]].sections then
921 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
922 table.insert( v, luci.uvl.option(
923 self.s, self.c, self.cref[1], self.cref[2], o
930 --- Get an associated section object.
931 -- @param s Section to select
932 -- @return Section instance
933 function scheme.section(self, s)
934 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
941 --- Object representation of a config.
944 -- @name luci.uvl.config
946 --- Config instance constructor.
949 -- @param scheme Scheme instance
950 -- @param co Configuration data
951 -- @param c Configuration name
952 -- @return Config instance
953 config = luci.util.class(uvlitem)
955 function config.__init__(self, scheme, co, c)
962 self.c = self:_loadconf(co, c)
964 self.t = luci.uvl.TYPE_CONFIG
967 --- Get all section objects associated with this config.
968 -- @return Table containing all associated luci.uvl.section instances
969 function config.sections(self)
971 if self.s.packages[self.sref[1]].sections then
972 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
973 table.insert( v, luci.uvl.option(
974 self.s, self.c, self.cref[1], self.cref[2], o
981 --- Get an associated section object.
982 -- @param s Section to select
983 -- @return Section instance
984 function config.section(self, s)
985 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
992 --- Object representation of a scheme/config section.
995 -- @name luci.uvl.section
997 --- Section instance constructor.
1000 -- @param scheme Scheme instance
1001 -- @param co Configuration data
1002 -- @param c Configuration name
1003 -- @param s Section name
1004 -- @return Section instance
1005 section = luci.util.class(uvlitem)
1007 function section.__init__(self, scheme, co, c, s)
1008 self.cref = { c, s }
1009 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1010 self.c = self:_loadconf(co, c)
1012 self.t = luci.uvl.TYPE_SECTION
1015 --- Get all option objects associated with this section.
1016 -- @return Table containing all associated luci.uvl.option instances
1017 function section.variables(self)
1019 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1021 self.s.packages[self.sref[1]].variables[self.sref[2]]
1023 table.insert( v, luci.uvl.option(
1024 self.s, self.c, self.cref[1], self.cref[2], o
1031 --- Get an associated option object.
1032 -- @param o Option to select
1033 -- @return Option instance
1034 function section.option(self, o)
1035 local oo = luci.uvl.option( self.s, self.c, self.cref[1], self.cref[2], o )
1042 --- Object representation of a scheme/config option.
1045 -- @name luci.uvl.option
1047 --- Section instance constructor.
1050 -- @param scheme Scheme instance
1051 -- @param co Configuration data
1052 -- @param c Configuration name
1053 -- @param s Section name
1054 -- @param o Option name
1055 -- @return Option instance
1056 option = luci.util.class(uvlitem)
1058 function option.__init__(self, scheme, co, c, s, o)
1059 self.cref = { c, s, o }
1060 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1061 self.c = self:_loadconf(co, c)
1063 self.t = luci.uvl.TYPE_OPTION
1066 --- Get the value of this option.
1067 -- @return The associated configuration value
1068 function option.value(self)
1069 return self:config()
1072 --- Get the associated section information in scheme.
1073 -- @return Table containing the scheme properties
1074 function option.section(self)
1075 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1078 --- Construct an enum object instance from given or default value.
1079 -- @param v Value to select
1080 -- @return Enum instance for selected value
1081 function option.enum(self, val)
1084 self.cref[1], self.cref[2], self.cref[3],
1090 --- Object representation of a enum value.
1093 -- @name luci.uvl.enum
1095 --- Section instance constructor.
1098 -- @param scheme Scheme instance
1099 -- @param co Configuration data
1100 -- @param c Configuration name
1101 -- @param s Section name
1102 -- @param o Enum name
1103 -- @param v Enum value
1104 -- @return Enum value instance
1105 enum = luci.util.class(option)
1107 function enum.__init__(self, scheme, co, c, s, o, v)
1108 self.cref = { c, s, o, v }
1109 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1110 self.c = self:_loadconf(co, c)
1112 self.t = luci.uvl.TYPE_ENUM