X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi%2Fdatatypes.lua;h=32530c465ad9f1816db3fdba1b880b2dca27047a;hp=9a3b735008a289de333a35a0d88898f76c657eaa;hb=c802c941cb5892ac43ee865594d3e06b60c00ace;hpb=3a0905f21cb1afe307402cb8ea1d89096f28c141 diff --git a/libs/web/luasrc/cbi/datatypes.lua b/libs/web/luasrc/cbi/datatypes.lua index 9a3b73500..32530c465 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 ) @@ -142,8 +179,9 @@ end function hostname(val) if val and (#val < 254) and ( - val:match("^[a-zA-Z0-9]+$") or - val:match("^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") + 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 @@ -234,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) @@ -275,24 +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 list(val, what, ...) - if type(val) == "string" and what and type(_M[what]) == "function" then - for val in val:gmatch("%S+") do - if not _M[what](val, ...) then - return false - end - end +function minlength(val, min) + val = tostring(val) + min = tonumber(min) - return true + 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