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
+ 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
return ip4addr(val) or ip6addr(val)
end
-function neg_ipaddr(v)
- if type(v) == "string" then
- v = v:gsub("^%s*!", "")
- end
- return v and ipaddr(v)
-end
-
function ip4addr(val)
if val then
return ip.IPv4(val) and true or false
return false
end
-function neg_ip4addr(v)
- if type(v) == "string" then
- v = v:gsub("^%s*!", "")
- end
- return v and ip4addr(v)
-end
-
function ip4prefix(val)
val = tonumber(val)
return ( val and val >= 0 and val <= 32 )
end
function hostname(val)
- if val and (#val < 254) and val.match(val, "^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") then
+ if val and (#val < 254) and (
+ val:match("^[a-zA-Z]+$") or
+ (val:match("^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") and
+ val:match("[^0-9%.]"))
+ ) then
return true
end
return false
return hostname(val) or ipaddr(val)
end
+function network(val)
+ return uciname(val) or host(val)
+end
+
function wpakey(val)
if #val == 64 then
return (val:match("^[a-fA-F0-9]+$") ~= nil)
return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
end
-function neg_network_ip4addr(val)
- if type(v) == "string" then
- v = v:gsub("^%s*!", "")
- return (uciname(v) or ip4addr(v))
- end
-end
-
function range(val, min, max)
val = tonumber(val)
min = tonumber(min)
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