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 1: get all sections
423 for i, conf in ipairs( schemes ) do
424 for k, v in pairs( conf ) do
425 if v['.type'] == 'section' then
427 ok, err = _req( TYPE_SECTION, v, { "name", "package" } )
428 if err then return false, scheme:error(err) end
430 local r, err = _ref( TYPE_SECTION, v )
431 if err then return false, scheme:error(err) end
433 self.packages[r[1]] =
434 self.packages[r[1]] or {
440 local p = self.packages[r[1]]
441 p.sections[v.name] = p.sections[v.name] or { }
442 p.variables[v.name] = p.variables[v.name] or { }
444 local s = p.sections[v.name]
445 local so = scheme:section(v.name)
447 for k, v2 in pairs(v) do
448 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
449 if k == "depends" then
450 s.depends = self:_read_dependency( v2, s.depends )
451 if not s.depends then
452 return false, scheme:error(
453 ERR.SME_BADDEP(so, luci.util.serialize_data(s.depends))
456 elseif k == "dynamic" or k == "unique" or
457 k == "required" or k == "named"
466 s.dynamic = s.dynamic or false
467 s.unique = s.unique or false
468 s.required = s.required or false
469 s.named = s.named or false
474 -- Step 2: get all variables
475 for i, conf in ipairs( schemes ) do
476 for k, v in pairs( conf ) do
477 if v['.type'] == "variable" then
479 ok, err = _req( TYPE_VARIABLE, v, { "name", "section" } )
480 if err then return false, scheme:error(err) end
482 local r, err = _ref( TYPE_VARIABLE, v )
483 if err then return false, scheme:error(err) end
485 local p = self.packages[r[1]]
487 return false, scheme:error(
488 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
492 local s = p.variables[r[2]]
494 return false, scheme:error(
495 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
499 s[v.name] = s[v.name] or { }
502 local so = scheme:section(r[2])
503 local to = so:option(v.name)
505 for k, v2 in pairs(v) do
506 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
507 if k == "depends" then
508 t.depends = self:_read_dependency( v2, t.depends )
509 if not t.depends then
510 return false, scheme:error(so:error(
511 ERR.SME_BADDEP(to, luci.util.serialize_data(v2))
514 elseif k == "validator" then
515 t.validators = self:_read_validator( v2, t.validators )
516 if not t.validators then
517 return false, scheme:error(so:error(
518 ERR.SME_BADVAL(to, luci.util.serialize_data(v2))
521 elseif k == "valueof" then
522 local values, err = self:_read_reference( v2 )
524 return false, scheme:error(so:error(
525 ERR.REFERENCE(to, luci.util.serialize_data(v2)):child(err)
530 elseif k == "required" then
538 t.type = t.type or "variable"
539 t.datatype = t.datatype or "string"
540 t.required = t.required or false
545 -- Step 3: get all enums
546 for i, conf in ipairs( schemes ) do
547 for k, v in pairs( conf ) do
548 if v['.type'] == "enum" then
550 ok, err = _req( TYPE_ENUM, v, { "value", "variable" } )
551 if err then return false, scheme:error(err) end
553 local r, err = _ref( TYPE_ENUM, v )
554 if err then return false, scheme:error(err) end
556 local p = self.packages[r[1]]
558 return false, scheme:error(
559 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
563 local s = p.variables[r[2]]
565 return false, scheme:error(
566 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
572 return false, scheme:error(
573 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
578 local so = scheme:section(r[2])
579 local oo = so:option(r[3])
580 local eo = oo:enum(v.value)
582 if t.type ~= "enum" then
583 return false, scheme:error(ERR.SME_EBADTYPE(eo))
587 t.values = { [v.value] = v.title or v.value }
589 t.values[v.value] = v.title or v.value
592 if not t.enum_depends then
598 return false, scheme:error(ERR.SME_EBADDEF(eo))
604 t.enum_depends[v.value] = self:_read_dependency(
605 v.depends, t.enum_depends[v.value]
608 if not t.enum_depends[v.value] then
609 return false, scheme:error(so:error(oo:error(
610 ERR.SME_BADDEP(eo, luci.util.serialize_data(v.depends))
621 -- Read a dependency specification
622 function UVL._read_dependency( self, values, deps )
623 local expr = "%$?[a-zA-Z0-9_]+"
625 values = ( type(values) == "table" and values or { values } )
626 for _, value in ipairs(values) do
627 local parts = luci.util.split( value, "%s*,%s*", nil, true )
628 local condition = { }
629 for i, val in ipairs(parts) do
630 local k, v = unpack(luci.util.split(val, "%s*=%s*", nil, true))
633 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
634 k:match("^"..expr.."%."..expr.."$") or
635 k:match("^"..expr.."$")
637 condition[k] = v or true
646 table.insert( deps, condition )
654 -- Read a validator specification
655 function UVL._read_validator( self, values, validators )
657 values = ( type(values) == "table" and values or { values } )
658 for _, value in ipairs(values) do
661 if value:match("^exec:") then
662 validator = value:gsub("^exec:","")
663 elseif value:match("^lua:") then
664 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
668 if not validators then
669 validators = { validator }
671 table.insert( validators, validator )
682 -- Read a reference specification (XXX: We should validate external configs too...)
683 function UVL._read_reference( self, values )
685 values = ( type(values) == "table" and values or { values } )
687 for _, value in ipairs(values) do
688 local ref = luci.util.split(value, ".")
690 if #ref == 2 or #ref == 3 then
691 local co = luci.uvl.config( self, ref[1] )
692 if not co:config() then return false, co:errors() end
694 for k, v in pairs(co:config()) do
695 if v['.type'] == ref[2] then
697 if v['.anonymous'] == true then
698 return false, ERR.SME_INVREF('', value)
700 val[k] = k -- XXX: title/description would be nice
701 elseif v[ref[3]] then
702 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
707 return false, ERR.SME_BADREF('', value)
714 -- Resolve given path
715 function UVL._resolve_function( self, value )
716 local path = luci.util.split(value, ".")
719 local stat, mod = luci.util.copcall(
720 require, table.concat(path, ".", 1, i)
724 for j=i+1, #path-1 do
725 if not type(mod) == "table" then
733 mod = type(mod) == "table" and mod[path[#path]] or nil
734 if type(mod) == "function" then
742 --- Object representation of an uvl item - base class.
743 uvlitem = luci.util.class()
745 function uvlitem.cid(self)
746 return table.concat( self.cref, '.' )
749 function uvlitem.sid(self)
750 return table.concat( self.sref, '.' )
753 function uvlitem.scheme(self, opt)
756 if #self.sref == 4 or #self.sref == 3 then
758 .packages[self.sref[1]]
759 .variables[self.sref[2]][self.sref[3]]
760 elseif #self.sref == 2 then
762 .packages[self.sref[1]]
763 .sections[self.sref[2]]
766 .packages[self.sref[1]]
776 function uvlitem.config(self, opt)
779 if #self.cref == 4 or #self.cref == 3 then
780 c = self.c[self.cref[2]][self.cref[3]]
781 elseif #self.cref == 2 then
782 c = self.c[self.cref[2]]
794 function uvlitem.title(self)
795 return self:scheme() and self:scheme('title') or
796 self.cref[3] or self.cref[2] or self.cref[1]
799 function uvlitem.type(self)
800 if self.t == luci.uvl.TYPE_CONFIG then
802 elseif self.t == luci.uvl.TYPE_SECTION then
804 elseif self.t == luci.uvl.TYPE_OPTION then
806 elseif self.t == luci.uvl.TYPE_ENUM then
811 function uvlitem.error(self, ...)
813 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
814 self.e = errconst[#self.cref]( self )
817 return self.e:child( ... )
820 function uvlitem.errors(self)
824 function uvlitem.ok(self)
825 return not self:errors()
828 function uvlitem.parent(self)
831 elseif #self.cref == 3 or #self.cref == 4 then
832 return luci.uvl.section( self.s, self.c, self.cref[1], self.cref[2] )
833 elseif #self.cref == 2 then
834 return luci.uvl.config( self.s, self.c, self.cref[1] )
840 function uvlitem._loadconf(self, co, c)
842 local uci, err = luci.model.uci.cursor(), nil
843 co, err = uci:get_all(c)
846 self:error(ERR.UCILOAD(self, err))
853 --- Object representation of a scheme.
856 -- @name luci.uvl.scheme
858 --- Scheme instance constructor.
861 -- @param scheme Scheme instance
862 -- @param co Configuration data
863 -- @param c Configuration name
864 -- @return Config instance
865 scheme = luci.util.class(uvlitem)
867 function scheme.__init__(self, scheme, co, c)
874 self.c = self:_loadconf(co, c)
876 self.t = luci.uvl.TYPE_SCHEME
879 --- Add an error to scheme.
880 -- @return Scheme error context
881 function scheme.error(self, ...)
882 if not self.e then self.e = ERR.SCHEME( self ) end
883 return self.e:child( ... )
886 --- Get an associated config object.
887 -- @return Config instance
888 function scheme.config(self)
889 local co = luci.uvl.config( self.s, self.cref[1] )
895 --- Get all section objects associated with this scheme.
896 -- @return Table containing all associated luci.uvl.section instances
897 function scheme.sections(self)
899 if self.s.packages[self.sref[1]].sections then
900 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
901 table.insert( v, luci.uvl.option(
902 self.s, self.c, self.cref[1], self.cref[2], o
909 --- Get an associated section object.
910 -- @param s Section to select
911 -- @return Section instance
912 function scheme.section(self, s)
913 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
920 --- Object representation of a config.
923 -- @name luci.uvl.config
925 --- Config instance constructor.
928 -- @param scheme Scheme instance
929 -- @param co Configuration data
930 -- @param c Configuration name
931 -- @return Config instance
932 config = luci.util.class(uvlitem)
934 function config.__init__(self, scheme, co, c)
941 self.c = self:_loadconf(co, c)
943 self.t = luci.uvl.TYPE_CONFIG
946 --- Get all section objects associated with this config.
947 -- @return Table containing all associated luci.uvl.section instances
948 function config.sections(self)
950 if self.s.packages[self.sref[1]].sections then
951 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
952 table.insert( v, luci.uvl.option(
953 self.s, self.c, self.cref[1], self.cref[2], o
960 --- Get an associated section object.
961 -- @param s Section to select
962 -- @return Section instance
963 function config.section(self, s)
964 local so = luci.uvl.section( self.s, self.c, self.cref[1], s )
971 --- Object representation of a scheme/config section.
974 -- @name luci.uvl.section
976 --- Section instance constructor.
979 -- @param scheme Scheme instance
980 -- @param co Configuration data
981 -- @param c Configuration name
982 -- @param s Section name
983 -- @return Section instance
984 section = luci.util.class(uvlitem)
986 function section.__init__(self, scheme, co, c, s)
988 self.sref = { c, co and co[s] and co[s]['.type'] or s }
989 self.c = self:_loadconf(co, c)
991 self.t = luci.uvl.TYPE_SECTION
994 --- Get all option objects associated with this section.
995 -- @return Table containing all associated luci.uvl.option instances
996 function section.variables(self)
998 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1000 self.s.packages[self.sref[1]].variables[self.sref[2]]
1002 table.insert( v, luci.uvl.option(
1003 self.s, self.c, self.cref[1], self.cref[2], o
1010 --- Get an associated option object.
1011 -- @param o Option to select
1012 -- @return Option instance
1013 function section.option(self, o)
1014 local oo = luci.uvl.option( self.s, self.c, self.cref[1], self.cref[2], o )
1021 --- Object representation of a scheme/config option.
1024 -- @name luci.uvl.option
1026 --- Section instance constructor.
1029 -- @param scheme Scheme instance
1030 -- @param co Configuration data
1031 -- @param c Configuration name
1032 -- @param s Section name
1033 -- @param o Option name
1034 -- @return Option instance
1035 option = luci.util.class(uvlitem)
1037 function option.__init__(self, scheme, co, c, s, o)
1038 self.cref = { c, s, o }
1039 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1040 self.c = self:_loadconf(co, c)
1042 self.t = luci.uvl.TYPE_OPTION
1045 --- Get the value of this option.
1046 -- @return The associated configuration value
1047 function option.value(self)
1048 return self:config()
1051 --- Get the associated section information in scheme.
1052 -- @return Table containing the scheme properties
1053 function option.section(self)
1054 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1057 --- Construct an enum object instance from given or default value.
1058 -- @param v Value to select
1059 -- @return Enum instance for selected value
1060 function option.enum(self, val)
1063 self.cref[1], self.cref[2], self.cref[3],
1069 --- Object representation of a enum value.
1072 -- @name luci.uvl.enum
1074 --- Section instance constructor.
1077 -- @param scheme Scheme instance
1078 -- @param co Configuration data
1079 -- @param c Configuration name
1080 -- @param s Section name
1081 -- @param o Enum name
1082 -- @param v Enum value
1083 -- @return Enum value instance
1084 enum = luci.util.class(option)
1086 function enum.__init__(self, scheme, co, c, s, o, v)
1087 self.cref = { c, s, o, v }
1088 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1089 self.c = self:_loadconf(co, c)
1091 self.t = luci.uvl.TYPE_ENUM