X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi%2Fdatatypes.lua;h=48586194a6e1a1a424febb22274604abe9c2e53a;hb=6780f757d63e60f65a99ae4022f8b0d9c08fee94;hp=fc3048e37e40524d4c292411824e99479d219b7d;hpb=794094caa20d922d968b4615f44a189f25f48a5e;p=project%2Fluci.git diff --git a/libs/web/luasrc/cbi/datatypes.lua b/libs/web/luasrc/cbi/datatypes.lua index fc3048e37..48586194a 100644 --- a/libs/web/luasrc/cbi/datatypes.lua +++ b/libs/web/luasrc/cbi/datatypes.lua @@ -17,12 +17,63 @@ 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, tostring, type, unpack, select = tonumber, tostring, 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 @@ -66,13 +117,6 @@ function ipaddr(val) 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 @@ -81,13 +125,6 @@ function ip4addr(val) 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 ) @@ -141,7 +178,11 @@ function macaddr(val) 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 @@ -151,6 +192,10 @@ function host(val) 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) @@ -227,13 +272,6 @@ function uciname(val) 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) @@ -268,10 +306,40 @@ 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*", "")) +function rangelength(val, min, max) + val = tostring(val) + min = tonumber(min) + max = tonumber(max) + + if val ~= nil and min ~= nil and max ~= nil then + return ((#val >= min) and (#val <= max)) + end + + return false +end + +function minlength(val, min) + val = tostring(val) + min = tonumber(min) + + if val ~= nil and min ~= nil then + return (#val >= min) end return false end + +function maxlength(val, max) + val = tostring(val) + max = tonumber(max) + + if val ~= nil and max ~= nil then + return (#val <= max) + end + + return false +end + +function phonedigit(val) + return (val:match("^[0-9\*#]+$") ~= nil) +end