X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi%2Fdatatypes.lua;h=d4603cf2a388403d8988d2aec3e9d91a52ab0676;hp=67cb63daf048ec8d4511f27560fb7c8103a280b6;hb=bb2d7517eb3d2229331e11aab5c6c900259e0742;hpb=f68906c20fa82065196d37e243c5819f7ff18ea9 diff --git a/libs/web/luasrc/cbi/datatypes.lua b/libs/web/luasrc/cbi/datatypes.lua index 67cb63daf..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" @@ -66,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 @@ -74,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 ) @@ -94,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) @@ -127,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