* luci/libs/uvl: more sensitive checking of error reasons in evaluation of option...
[project/luci.git] / libs / uvl / luasrc / uvl.lua
index 471829a..3963a64 100644 (file)
@@ -8,7 +8,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
-        http://www.apache.org/licenses/LICENSE-2.0
+               http://www.apache.org/licenses/LICENSE-2.0
 
 $Id$
 
@@ -27,7 +27,7 @@ local string = require "string"
 
 local require, pcall, ipairs, pairs = require, pcall, ipairs, pairs
 local type, error, tonumber, tostring = type, error, tonumber, tostring
-local unpack = unpack
+local unpack, loadfile = unpack, loadfile
 
 module "luci.uvl"
 
@@ -292,6 +292,20 @@ function UVL._validate_option( self, option, nodeps )
                end
 
        elseif option:scheme() then
+               if not nodeps then
+                       local ok, err = dependencies.check( self, option )
+                       if not ok then
+                               if not err:is_all(ERR.ERR_DEP_NOTEQUAL) and
+                                  not err:is_all(ERR.ERR_DEP_NOVALUE)
+                               then
+                                       option:error(err)
+                                       return false, option:errors()
+                               else
+                                       return true
+                               end
+                       end
+               end
+
                if option:scheme('required') and not option:value() then
                        return false, option:error(ERR.OPT_REQUIRED(option))
 
@@ -334,12 +348,34 @@ function UVL._validate_option( self, option, nodeps )
                                        return false, option:error(ERR.OPT_DATATYPE(option, dt))
                                end
                        end
-               end
 
-               if not nodeps then
-                       local ok, err = dependencies.check( self, option )
-                       if not ok then
-                               option:error(err)
+                       val = ( type(val) == "table" and val or { val } )
+                       for _, v in ipairs(val) do
+                               if option:scheme('minlength') then
+                                       if #v < option:scheme('minlength') then
+                                               return false, option:error(ERR.OPT_RANGE(option))
+                                       end
+                               end
+
+                               if option:scheme('maxlength') then
+                                       if #v > option:scheme('maxlength') then
+                                               return false, option:error(ERR.OPT_RANGE(option))
+                                       end
+                               end
+
+                               v = tonumber(v)
+
+                               if option:scheme('minimum') then
+                                       if not v or v < option:scheme('minimum') then
+                                               return false, option:error(ERR.OPT_RANGE(option))
+                                       end
+                               end
+
+                               if option:scheme('maximum') then
+                                       if not v or v > option:scheme('maximum') then
+                                               return false, option:error(ERR.OPT_RANGE(option))
+                                       end
+                               end
                        end
                end
 
@@ -525,8 +561,7 @@ function UVL._parse_section(self, scheme, k, v)
        s.named    = s.named    or false
 end
 
-
-       -- Step 2: get all variables
+-- Step 2: get all variables
 function UVL._parse_var(self, scheme, k, v)
        local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
        if err then error(scheme:error(err)) end
@@ -579,8 +614,13 @@ function UVL._parse_var(self, scheme, k, v)
                                end
                                t.type   = "reference"
                                t.values = values
+                               t.valueof = type(v2) == "table" and v2 or {v2}
                        elseif k == "required" then
                                t[k] = _bool(v2)
+                       elseif k == "minlength" or k == "maxlength" or
+                   k == "minimum" or k == "maximum"
+            then
+                               t[k] = tonumber(v2)
                        else
                                t[k] = t[k] or v2
                        end
@@ -632,8 +672,10 @@ function UVL._parse_enum(self, scheme, k, v)
 
        if not t.values then
                t.values = { [v.value] = v.title or v.value }
+               t.valuelist = { {value = v.value, title = v.title} }
        else
                t.values[v.value] = v.title or v.value
+               t.valuelist[#t.valuelist + 1] = {value = v.value, title = v.title}
        end
 
        if not t.enum_depends then
@@ -662,21 +704,20 @@ end
 
 -- Read a dependency specification
 function UVL._read_dependency( self, values, deps )
-       local expr = "%$?[a-zA-Z0-9_]+"
+       local expr = "%$?[%w_]+"
        if values then
                values = ( type(values) == "table" and values or { values } )
                for _, value in ipairs(values) do
-                       local parts     = util.split( value, "%s*,%s*", nil, true )
                        local condition = { }
-                       for i, val in ipairs(parts) do
-                               local k, v = unpack(util.split(val, "%s*=%s*", nil, true))
+                       for val in value:gmatch("[^,]+") do
+                               local k, e, v = val:match("%s*([%w$_.]+)%s*(=?)%s*(.*)")
 
                                if k and (
                                        k:match("^"..expr.."%."..expr.."%."..expr.."$") or
                                        k:match("^"..expr.."%."..expr.."$") or
                                        k:match("^"..expr.."$")
                                ) then
-                                       condition[k] = v or true
+                                       condition[k] = (e == '=') and v or true
                                else
                                        return nil
                                end
@@ -811,7 +852,7 @@ function uvlitem.cid(self)
                local c = self.c
                if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
                        r[2] = '@' .. c[r[2]]['.type'] ..
-                              '[' .. tostring(c[r[2]]['.index']) .. ']'
+                                  '[' .. tostring(c[r[2]]['.index']) .. ']'
                end
                return table.concat( r, '.' )
        end
@@ -848,7 +889,7 @@ function uvlitem.config(self, opt)
                if #self.cref >= 3 then
                        c = c and c[self.cref[3]] or nil
                end
-       end     
+       end
 
        if c and opt then
                return c[opt]
@@ -912,7 +953,7 @@ function uvlitem._loadconf(self, co, c)
                if err then
                        self:error(ERR.UCILOAD(self, err))
                end
-               
+
                self._configcache = co
        end
        return co