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, type, unpack, select = tonumber, 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
37 elseif f(v, unpack(a)) then
44 _M['and'] = function(v, ...)
46 for i = 1, select('#', ...), 2 do
47 local f = select(i, ...)
48 local a = select(i+1, ...)
49 if type(f) ~= "function" then
54 elseif not f(v, unpack(a)) then
62 return _M['or'](v:gsub("^%s*!%s*", ""), ...)
65 function list(v, subvalidator, subargs)
66 if type(subvalidator) ~= "function" then
70 for token in v:gmatch("%S+") do
71 if not subvalidator(token, unpack(subargs)) then
79 if val == "1" or val == "yes" or val == "on" or val == "true" then
81 elseif val == "0" or val == "no" or val == "off" or val == "false" then
83 elseif val == "" or val == nil then
90 function uinteger(val)
91 local n = tonumber(val)
92 if n ~= nil and math.floor(n) == n and n >= 0 then
100 local n = tonumber(val)
101 if n ~= nil and math.floor(n) == n then
109 local n = tonumber(val)
110 return ( n ~= nil and n >= 0 )
114 return ( tonumber(val) ~= nil )
118 return ip4addr(val) or ip6addr(val)
121 function ip4addr(val)
123 return ip.IPv4(val) and true or false
129 function ip4prefix(val)
131 return ( val and val >= 0 and val <= 32 )
134 function ip6addr(val)
136 return ip.IPv6(val) and true or false
142 function ip6prefix(val)
144 return ( val and val >= 0 and val <= 128 )
149 return ( val and val >= 0 and val <= 65535 )
152 function portrange(val)
153 local p1, p2 = val:match("^(%d+)%-(%d+)$")
154 if p1 and p2 and port(p1) and port(p2) then
161 function macaddr(val)
162 if val and val:match(
163 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
164 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
166 local parts = util.split( val, ":" )
169 parts[i] = tonumber( parts[i], 16 )
170 if parts[i] < 0 or parts[i] > 255 then
181 function hostname(val)
182 if val and (#val < 254) and (
183 val:match("^[a-zA-Z]+$") or
184 (val:match("^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") and
185 val:match("[^0-9%.]"))
193 return hostname(val) or ipaddr(val)
196 function network(val)
197 return uciname(val) or host(val)
202 return (val:match("^[a-fA-F0-9]+$") ~= nil)
204 return (#val >= 8) and (#val <= 63)
209 if val:sub(1, 2) == "s:" then
213 if (#val == 10) or (#val == 26) then
214 return (val:match("^[a-fA-F0-9]+$") ~= nil)
216 return (#val == 5) or (#val == 13)
221 return true -- Everything qualifies as valid string
224 function directory( val, seen )
225 local s = fs.stat(val)
228 if s and not seen[s.ino] then
230 if s.type == "dir" then
232 elseif s.type == "lnk" then
233 return directory( fs.readlink(val), seen )
240 function file( val, seen )
241 local s = fs.stat(val)
244 if s and not seen[s.ino] then
246 if s.type == "reg" then
248 elseif s.type == "lnk" then
249 return file( fs.readlink(val), seen )
256 function device( val, seen )
257 local s = fs.stat(val)
260 if s and not seen[s.ino] then
262 if s.type == "chr" or s.type == "blk" then
264 elseif s.type == "lnk" then
265 return device( fs.readlink(val), seen )
272 function uciname(val)
273 return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
276 function range(val, min, max)
281 if val ~= nil and min ~= nil and max ~= nil then
282 return ((val >= min) and (val <= max))
288 function min(val, min)
292 if val ~= nil and min ~= nil then
299 function max(val, max)
303 if val ~= nil and max ~= nil then