X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi%2Fdatatypes.lua;h=d4603cf2a388403d8988d2aec3e9d91a52ab0676;hb=bb2d7517eb3d2229331e11aab5c6c900259e0742;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..d4603cf2a 100644 --- a/libs/web/luasrc/cbi/datatypes.lua +++ b/libs/web/luasrc/cbi/datatypes.lua @@ -17,8 +17,8 @@ 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 = tonumber module "luci.cbi.datatypes" @@ -35,7 +35,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 +44,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 +53,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 @@ -61,6 +66,13 @@ 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 @@ -69,6 +81,13 @@ 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 ) @@ -89,7 +108,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 +141,9 @@ 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(val, "^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") then + return true end - return false end @@ -149,7 +167,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 +222,19 @@ 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