3 UCI Validation Layer - Datatype Tests
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 module( "luci.uvl.datatypes", package.seeall )
24 function boolean( val )
25 if val == "1" or val == "yes" or val == "on" or val == "true" then
27 elseif val == "0" or val == "no" or val == "off" or val == "false" then
35 local n = tonumber(val)
36 if n ~= nil and math.floor(n) == n and n >= 0 then
43 function integer( val )
44 local n = tonumber(val)
45 if n ~= nil and math.floor(n) == n then
53 return ( tonumber(val) ~= nil )
56 function ipaddr( val )
57 return ip4addr(val) or ip6addr(val)
60 function ip4addr( val )
62 return luci.ip.IPv4(val) and true or false
68 function ip4prefix( val )
70 return ( val and val >= 0 and val <= 32 )
73 function ip6addr( val )
75 return luci.ip.IPv6(val) and true or false
81 function ip6prefix( val )
83 return ( val and val >= 0 and val <= 128 )
88 return ( val and val >= 1 and val <= 65535 )
91 function portrange( val )
92 local p1, p2 = val:match("^(%d+)%-(%d+)$")
93 if p1 and p2 and port(p1) and port(p2) then
100 function macaddr( val )
101 if val and val:match(
102 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
103 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
105 local parts = luci.util.split( val, ":" )
108 parts[i] = tonumber( parts[i], 16 )
109 if parts[i] < 0 or parts[i] > 255 then
120 function hostname( val )
121 if val and val:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then
122 return true -- XXX: ToDo: need better solution
129 return hostname(val) or ipaddr(val)
132 function string( val )
133 return true -- Everything qualifies as valid string
136 function directory( val, seen )
137 local s = luci.fs.stat( val )
140 if s and not seen[s.ino] then
142 if s.type == "directory" then
144 elseif s.type == "link" then
145 return directory( luci.fs.readlink(val), seen )
152 function file( val, seen )
153 local s = luci.fs.stat( val )
156 if s and not seen[s.ino] then
158 if s.type == "regular" then
160 elseif s.type == "link" then
161 return file( luci.fs.readlink(val), seen )