X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi%2Fdatatypes.lua;h=48586194a6e1a1a424febb22274604abe9c2e53a;hb=6780f757d63e60f65a99ae4022f8b0d9c08fee94;hp=53a34547ba13c0f717b55860729f834e44e1f78d;hpb=ede4aca4b95c9e664e4830fd43c54b627b122538;p=project%2Fluci.git diff --git a/libs/web/luasrc/cbi/datatypes.lua b/libs/web/luasrc/cbi/datatypes.lua index 53a34547b..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, tostring, type, unpack, select = tonumber, tostring, type, unpack, select -local tonumber = tonumber 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 @@ -35,7 +86,7 @@ function bool(val) return false end -function uint(val) +function uinteger(val) local n = tonumber(val) if n ~= nil and math.floor(n) == n and n >= 0 then return true @@ -44,7 +95,7 @@ function uint(val) return false end -function int(val) +function integer(val) local n = tonumber(val) if n ~= nil and math.floor(n) == n then return true @@ -53,6 +104,11 @@ function int(val) return false end +function ufloat(val) + local n = tonumber(val) + return ( n ~= nil and n >= 0 ) +end + function float(val) return ( tonumber(val) ~= nil ) end @@ -89,7 +145,7 @@ end function port(val) val = tonumber(val) - return ( val and val >= 1 and val <= 65535 ) + return ( val and val >= 0 and val <= 65535 ) end function portrange(val) @@ -122,10 +178,13 @@ function macaddr(val) end function hostname(val) - if val and val:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then - return true -- XXX: ToDo: need better solution + 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 end @@ -133,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) @@ -149,7 +212,7 @@ function wepkey(val) if (#val == 10) or (#val == 26) then return (val:match("^[a-fA-F0-9]+$") ~= nil) else - return (#v == 5) or (#v == 13) + return (#val == 5) or (#val == 13) end end @@ -204,3 +267,79 @@ function device( val, seen ) return false end + +function uciname(val) + return (val:match("^[a-zA-Z0-9_]+$") ~= nil) +end + +function range(val, min, max) + val = tonumber(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 min(val, min) + val = tonumber(val) + min = tonumber(min) + + if val ~= nil and min ~= nil then + return (val >= min) + end + + return false +end + +function max(val, max) + val = tonumber(val) + max = tonumber(max) + + if val ~= nil and max ~= nil then + return (val <= max) + end + + return false +end + +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