1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Copyright 2017 Dan Luedtke <mail@danrl.com>
3 -- Licensed to the public under the Apache License 2.0.
5 local fs = require "nixio.fs"
6 local ip = require "luci.ip"
7 local math = require "math"
8 local util = require "luci.util"
9 local tonumber, tostring, type, unpack, select = tonumber, tostring, type, unpack, select
12 module "luci.cbi.datatypes"
15 _M['or'] = function(v, ...)
17 for i = 1, select('#', ...), 2 do
18 local f = select(i, ...)
19 local a = select(i+1, ...)
20 if type(f) ~= "function" then
25 elseif f(v, unpack(a)) then
32 _M['and'] = function(v, ...)
34 for i = 1, select('#', ...), 2 do
35 local f = select(i, ...)
36 local a = select(i+1, ...)
37 if type(f) ~= "function" then
42 elseif not f(v, unpack(a)) then
50 return _M['or'](v:gsub("^%s*!%s*", ""), ...)
53 function list(v, subvalidator, subargs)
54 if type(subvalidator) ~= "function" then
58 for token in v:gmatch("%S+") do
59 if not subvalidator(token, unpack(subargs)) then
67 if val == "1" or val == "yes" or val == "on" or val == "true" then
69 elseif val == "0" or val == "no" or val == "off" or val == "false" then
71 elseif val == "" or val == nil then
78 function uinteger(val)
79 local n = tonumber(val)
80 if n ~= nil and math.floor(n) == n and n >= 0 then
88 local n = tonumber(val)
89 if n ~= nil and math.floor(n) == n then
97 local n = tonumber(val)
98 return ( n ~= nil and n >= 0 )
102 return ( tonumber(val) ~= nil )
106 return ip4addr(val) or ip6addr(val)
109 function ip4addr(val)
111 return ip.IPv4(val) and true or false
117 function ip4prefix(val)
119 return ( val and val >= 0 and val <= 32 )
122 function ip6addr(val)
124 return ip.IPv6(val) and true or false
130 function ip6prefix(val)
132 return ( val and val >= 0 and val <= 128 )
136 return ipmask4(val) or ipmask6(val)
139 function ipmask4(val)
140 local ip, mask = val:match("^([^/]+)/([^/]+)$")
141 local bits = tonumber(mask)
143 if bits and (bits < 0 or bits > 32) then
147 if not bits and mask and not ip4addr(mask) then
151 return ip4addr(ip or val)
154 function ipmask6(val)
155 local ip, mask = val:match("^([^/]+)/([^/]+)$")
156 local bits = tonumber(mask)
158 if bits and (bits < 0 or bits > 128) then
162 if not bits and mask and not ip6addr(mask) then
166 return ip6addr(ip or val)
169 function ip6hostid(val)
170 if val and val:match("^[a-fA-F0-9:]+$") and (#val > 2) then
171 return (ip6addr("2001:db8:0:0" .. val) or ip6addr("2001:db8:0:0:" .. val))
179 return ( val and val >= 0 and val <= 65535 )
182 function portrange(val)
183 local p1, p2 = val:match("^(%d+)%-(%d+)$")
184 if p1 and p2 and port(p1) and port(p2) then
191 function macaddr(val)
192 if val and val:match(
193 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
194 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
196 local parts = util.split( val, ":" )
199 parts[i] = tonumber( parts[i], 16 )
200 if parts[i] < 0 or parts[i] > 255 then
211 function hostname(val)
212 if val and (#val < 254) and (
213 val:match("^[a-zA-Z_]+$") or
214 (val:match("^[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*[a-zA-Z0-9]$") and
215 val:match("[^0-9%.]"))
222 function host(val, ipv4only)
223 return hostname(val) or ((ipv4only == 1) and ip4addr(val)) or ((not (ipv4only == 1)) and ipaddr(val))
226 function network(val)
227 return uciname(val) or host(val)
230 function hostport(val, ipv4only)
231 local h, p = val:match("^([^:]+):([^:]+)$")
232 return not not (h and p and host(h, ipv4only) and port(p))
235 function ip4addrport(val, bracket)
236 local h, p = val:match("^([^:]+):([^:]+)$")
237 return (h and p and ip4addr(h) and port(p))
240 function ip4addrport(val)
241 local h, p = val:match("^([^:]+):([^:]+)$")
242 return (h and p and ip4addr(h) and port(p))
245 function ipaddrport(val, bracket)
246 local h, p = val:match("^([^%[%]:]+):([^:]+)$")
247 if (h and p and ip4addr(h) and port(p)) then
249 elseif (bracket == 1) then
250 h, p = val:match("^%[(.+)%]:([^:]+)$")
251 if (h and p and ip6addr(h) and port(p)) then
255 h, p = val:match("^([^%[%]]+):([^:]+)$")
256 return (h and p and ip6addr(h) and port(p))
261 return (val:match("^[a-fA-F0-9]+$") ~= nil)
263 return (#val >= 8) and (#val <= 63)
268 if val:sub(1, 2) == "s:" then
272 if (#val == 10) or (#val == 26) then
273 return (val:match("^[a-fA-F0-9]+$") ~= nil)
275 return (#val == 5) or (#val == 13)
279 function hexstring(val)
281 return (val:match("^[a-fA-F0-9]+$") ~= nil)
288 return (val:match("^[a-zA-Z0-9/+]+=?=?$") ~= nil) and (math.fmod(#val, 4) == 0)
294 return true -- Everything qualifies as valid string
297 function directory(val, seen)
298 local s = fs.stat(val)
301 if s and not seen[s.ino] then
303 if s.type == "dir" then
305 elseif s.type == "lnk" then
306 return directory( fs.readlink(val), seen )
313 function file(val, seen)
314 local s = fs.stat(val)
317 if s and not seen[s.ino] then
319 if s.type == "reg" then
321 elseif s.type == "lnk" then
322 return file( fs.readlink(val), seen )
329 function device(val, seen)
330 local s = fs.stat(val)
333 if s and not seen[s.ino] then
335 if s.type == "chr" or s.type == "blk" then
337 elseif s.type == "lnk" then
338 return device( fs.readlink(val), seen )
345 function uciname(val)
346 return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
349 function range(val, min, max)
354 if val ~= nil and min ~= nil and max ~= nil then
355 return ((val >= min) and (val <= max))
361 function min(val, min)
365 if val ~= nil and min ~= nil then
372 function max(val, max)
376 if val ~= nil and max ~= nil then
383 function rangelength(val, min, max)
388 if val ~= nil and min ~= nil and max ~= nil then
389 return ((#val >= min) and (#val <= max))
395 function minlength(val, min)
399 if val ~= nil and min ~= nil then
406 function maxlength(val, max)
410 if val ~= nil and max ~= nil then
417 function phonedigit(val)
418 return (val:match("^[0-9\*#!%.]+$") ~= nil)
421 function timehhmmss(val)
422 return (val:match("^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$") ~= nil)
425 function dateyyyymmdd(val)
427 yearstr, monthstr, daystr = val:match("^(%d%d%d%d)-(%d%d)-(%d%d)$")
428 if (yearstr == nil) or (monthstr == nil) or (daystr == nil) then
431 year = tonumber(yearstr)
432 month = tonumber(monthstr)
433 day = tonumber(daystr)
434 if (year == nil) or (month == nil) or (day == nil) then
438 local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
440 local function is_leap_year(year)
441 return (year % 4 == 0) and ((year % 100 ~= 0) or (year % 400 == 0))
444 function get_days_in_month(month, year)
445 if (month == 2) and is_leap_year(year) then
448 return days_in_month[month]
451 if (year < 2015) then
454 if ((month == 0) or (month > 12)) then
457 if ((day == 0) or (day > get_days_in_month(month, year))) then