3 LuCI - Configuration Bind Interface - Datatype Tests
4 (c) 2010 Jo-Philipp Wich <xm@subsignal.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 local fs = require "nixio.fs"
17 local ip = require "luci.ip"
18 local math = require "math"
19 local util = require "luci.util"
20 local tonumber, tostring, type, unpack, select = tonumber, tostring, type, unpack, select
23 module "luci.cbi.datatypes"
26 _M['or'] = function(v, ...)
28 for i = 1, select('#', ...), 2 do
29 local f = select(i, ...)
30 local a = select(i+1, ...)
31 if type(f) ~= "function" then
36 elseif f(v, unpack(a)) then
43 _M['and'] = function(v, ...)
45 for i = 1, select('#', ...), 2 do
46 local f = select(i, ...)
47 local a = select(i+1, ...)
48 if type(f) ~= "function" then
53 elseif not f(v, unpack(a)) then
61 return _M['or'](v:gsub("^%s*!%s*", ""), ...)
64 function list(v, subvalidator, subargs)
65 if type(subvalidator) ~= "function" then
69 for token in v:gmatch("%S+") do
70 if not subvalidator(token, unpack(subargs)) then
78 if val == "1" or val == "yes" or val == "on" or val == "true" then
80 elseif val == "0" or val == "no" or val == "off" or val == "false" then
82 elseif val == "" or val == nil then
89 function uinteger(val)
90 local n = tonumber(val)
91 if n ~= nil and math.floor(n) == n and n >= 0 then
99 local n = tonumber(val)
100 if n ~= nil and math.floor(n) == n then
108 local n = tonumber(val)
109 return ( n ~= nil and n >= 0 )
113 return ( tonumber(val) ~= nil )
117 return ip4addr(val) or ip6addr(val)
120 function ip4addr(val)
122 return ip.IPv4(val) and true or false
128 function ip4prefix(val)
130 return ( val and val >= 0 and val <= 32 )
133 function ip6addr(val)
135 return ip.IPv6(val) and true or false
141 function ip6prefix(val)
143 return ( val and val >= 0 and val <= 128 )
148 return ( val and val >= 0 and val <= 65535 )
151 function portrange(val)
152 local p1, p2 = val:match("^(%d+)%-(%d+)$")
153 if p1 and p2 and port(p1) and port(p2) then
160 function macaddr(val)
161 if val and val:match(
162 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
163 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
165 local parts = util.split( val, ":" )
168 parts[i] = tonumber( parts[i], 16 )
169 if parts[i] < 0 or parts[i] > 255 then
180 function hostname(val)
181 if val and (#val < 254) and (
182 val:match("^[a-zA-Z_]+$") or
183 (val:match("^[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*[a-zA-Z0-9]$") and
184 val:match("[^0-9%.]"))
192 return hostname(val) or ipaddr(val)
195 function network(val)
196 return uciname(val) or host(val)
201 return (val:match("^[a-fA-F0-9]+$") ~= nil)
203 return (#val >= 8) and (#val <= 63)
208 if val:sub(1, 2) == "s:" then
212 if (#val == 10) or (#val == 26) then
213 return (val:match("^[a-fA-F0-9]+$") ~= nil)
215 return (#val == 5) or (#val == 13)
220 return true -- Everything qualifies as valid string
223 function directory( val, seen )
224 local s = fs.stat(val)
227 if s and not seen[s.ino] then
229 if s.type == "dir" then
231 elseif s.type == "lnk" then
232 return directory( fs.readlink(val), seen )
239 function file( val, seen )
240 local s = fs.stat(val)
243 if s and not seen[s.ino] then
245 if s.type == "reg" then
247 elseif s.type == "lnk" then
248 return file( fs.readlink(val), seen )
255 function device( val, seen )
256 local s = fs.stat(val)
259 if s and not seen[s.ino] then
261 if s.type == "chr" or s.type == "blk" then
263 elseif s.type == "lnk" then
264 return device( fs.readlink(val), seen )
271 function uciname(val)
272 return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
275 function range(val, min, max)
280 if val ~= nil and min ~= nil and max ~= nil then
281 return ((val >= min) and (val <= max))
287 function min(val, min)
291 if val ~= nil and min ~= nil then
298 function max(val, max)
302 if val ~= nil and max ~= nil then
309 function rangelength(val, min, max)
314 if val ~= nil and min ~= nil and max ~= nil then
315 return ((#val >= min) and (#val <= max))
321 function minlength(val, min)
325 if val ~= nil and min ~= nil then
332 function maxlength(val, max)
336 if val ~= nil and max ~= nil then
343 function phonedigit(val)
344 return (val:match("^[0-9\*#!%.]+$") ~= nil)