libs/web: introduce recursive expression support for datatypes, introduce "or" and...
[project/luci.git] / libs / web / luasrc / cbi / datatypes.lua
index 9f5a3eb..6d87157 100644 (file)
@@ -17,12 +17,64 @@ local fs = require "nixio.fs"
 local ip = require "luci.ip"
 local math = require "math"
 local util = require "luci.util"
-local tonumber, type = tonumber, type
+local tonumber, type, unpack, select = tonumber, type, unpack, select
 
 
 module "luci.cbi.datatypes"
 
 
+_M['or'] = function(v, ...)
+       local i
+       for i = 1, select('#', ...), 2 do
+               local f = select(i, ...)
+               local a = select(i+1, ...)
+               if type(f) ~= "function" then
+                       print("COMP", f, v)
+                       if f == v then
+                               return true
+                       end
+                       i = i - 1
+               elseif f(v, unpack(a)) then
+                       return true
+               end
+       end
+       return false
+end
+
+_M['and'] = function(v, ...)
+       local i
+       for i = 1, select('#', ...), 2 do
+               local f = select(i, ...)
+               local a = select(i+1, ...)
+               if type(f) ~= "function" then
+                       if f ~= v then
+                               return false
+                       end
+                       i = i - 1
+               elseif not f(v, unpack(a)) then
+                       return false
+               end
+       end
+       return true
+end
+
+function neg(v, ...)
+       return _M['or'](v:gsub("^%s*!%s*", ""), ...)
+end
+
+function list(v, subvalidator, subargs)
+       if type(subvalidator) ~= "function" then
+               return false
+       end
+       local token
+       for token in v:gmatch("%S+") do
+               if not subvalidator(token, unpack(subargs)) then
+                       return false
+               end
+       end
+       return true
+end
+
 function bool(val)
        if val == "1" or val == "yes" or val == "on" or val == "true" then
                return true
@@ -254,25 +306,3 @@ function max(val, max)
 
        return false
 end
-
-function neg(val, what)
-       if what and type(_M[what]) == "function" then
-               return _M[what](val:gsub("^%s*!%s*", ""))
-       end
-
-       return false
-end
-
-function list(val, what, ...)
-       if type(val) == "string" and what and type(_M[what]) == "function" then
-               for val in val:gmatch("%S+") do
-                       if not _M[what](val, ...) then
-                               return false
-                       end
-               end
-
-               return true
-       end
-
-       return false
-end