1 -- Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local fs = require "nixio.fs"
5 local ip = require "luci.ip"
6 local math = require "math"
7 local util = require "luci.util"
8 local tonumber, tostring, type, unpack, select = tonumber, tostring, type, unpack, select
11 module "luci.cbi.datatypes"
14 _M['or'] = function(v, ...)
16 for i = 1, select('#', ...), 2 do
17 local f = select(i, ...)
18 local a = select(i+1, ...)
19 if type(f) ~= "function" then
24 elseif f(v, unpack(a)) then
31 _M['and'] = function(v, ...)
33 for i = 1, select('#', ...), 2 do
34 local f = select(i, ...)
35 local a = select(i+1, ...)
36 if type(f) ~= "function" then
41 elseif not f(v, unpack(a)) then
49 return _M['or'](v:gsub("^%s*!%s*", ""), ...)
52 function list(v, subvalidator, subargs)
53 if type(subvalidator) ~= "function" then
57 for token in v:gmatch("%S+") do
58 if not subvalidator(token, unpack(subargs)) then
66 if val == "1" or val == "yes" or val == "on" or val == "true" then
68 elseif val == "0" or val == "no" or val == "off" or val == "false" then
70 elseif val == "" or val == nil then
77 function uinteger(val)
78 local n = tonumber(val)
79 if n ~= nil and math.floor(n) == n and n >= 0 then
87 local n = tonumber(val)
88 if n ~= nil and math.floor(n) == n then
96 local n = tonumber(val)
97 return ( n ~= nil and n >= 0 )
101 return ( tonumber(val) ~= nil )
105 return ip4addr(val) or ip6addr(val)
108 function ip4addr(val)
110 return ip.IPv4(val) and true or false
116 function ip4prefix(val)
118 return ( val and val >= 0 and val <= 32 )
121 function ip6addr(val)
123 return ip.IPv6(val) and true or false
129 function ip6prefix(val)
131 return ( val and val >= 0 and val <= 128 )
136 return ( val and val >= 0 and val <= 65535 )
139 function portrange(val)
140 local p1, p2 = val:match("^(%d+)%-(%d+)$")
141 if p1 and p2 and port(p1) and port(p2) then
148 function macaddr(val)
149 if val and val:match(
150 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
151 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
153 local parts = util.split( val, ":" )
156 parts[i] = tonumber( parts[i], 16 )
157 if parts[i] < 0 or parts[i] > 255 then
168 function hostname(val)
169 if val and (#val < 254) and (
170 val:match("^[a-zA-Z_]+$") or
171 (val:match("^[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*[a-zA-Z0-9]$") and
172 val:match("[^0-9%.]"))
180 return hostname(val) or ipaddr(val)
183 function network(val)
184 return uciname(val) or host(val)
189 return (val:match("^[a-fA-F0-9]+$") ~= nil)
191 return (#val >= 8) and (#val <= 63)
196 if val:sub(1, 2) == "s:" then
200 if (#val == 10) or (#val == 26) then
201 return (val:match("^[a-fA-F0-9]+$") ~= nil)
203 return (#val == 5) or (#val == 13)
208 return true -- Everything qualifies as valid string
211 function directory( val, seen )
212 local s = fs.stat(val)
215 if s and not seen[s.ino] then
217 if s.type == "dir" then
219 elseif s.type == "lnk" then
220 return directory( fs.readlink(val), seen )
227 function file( val, seen )
228 local s = fs.stat(val)
231 if s and not seen[s.ino] then
233 if s.type == "reg" then
235 elseif s.type == "lnk" then
236 return file( fs.readlink(val), seen )
243 function device( val, seen )
244 local s = fs.stat(val)
247 if s and not seen[s.ino] then
249 if s.type == "chr" or s.type == "blk" then
251 elseif s.type == "lnk" then
252 return device( fs.readlink(val), seen )
259 function uciname(val)
260 return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
263 function range(val, min, max)
268 if val ~= nil and min ~= nil and max ~= nil then
269 return ((val >= min) and (val <= max))
275 function min(val, min)
279 if val ~= nil and min ~= nil then
286 function max(val, max)
290 if val ~= nil and max ~= nil then
297 function rangelength(val, min, max)
302 if val ~= nil and min ~= nil and max ~= nil then
303 return ((#val >= min) and (#val <= max))
309 function minlength(val, min)
313 if val ~= nil and min ~= nil then
320 function maxlength(val, max)
324 if val ~= nil and max ~= nil then
331 function phonedigit(val)
332 return (val:match("^[0-9\*#!%.]+$") ~= nil)