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" then
27 elseif val == "0" or val == "no" or val == "off" 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 )
86 function macaddr( val )
88 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
89 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
91 local parts = luci.util.split( val, ":" )
94 parts[i] = tonumber( parts[i], 16 )
95 if parts[i] < 0 or parts[i] > 255 then
106 function hostname( val )
107 if val and val:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then
108 return true -- XXX: ToDo: need better solution
114 function string( val )
115 return true -- Everything qualifies as valid string
118 function directory( val, seen )
119 local s = luci.fs.stat( val )
122 if s and not seen[s.ino] then
124 if s.type == "directory" then
126 elseif s.type == "link" then
127 return directory( luci.fs.readlink(val), seen )
134 function file( val, seen )
135 local s = luci.fs.stat( val )
138 if s and not seen[s.ino] then
140 if s.type == "regular" then
142 elseif s.type == "link" then
143 return file( luci.fs.readlink(val), seen )