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
34 function integer( val )
35 local n = tonumber(val)
36 if n ~= nil and math.floor(n) == n then
44 return ( tonumber(val) ~= nil )
47 function ip4addr( val )
49 return luci.ip.IPv4(val) and true or false
55 function ip4prefix( val )
57 return ( val and val >= 0 and val <= 32 )
60 function ip6addr( val )
62 return luci.ip.IPv6(val) and true or false
68 function ip6prefix( val )
70 return ( val and val >= 0 and val <= 128 )
73 function macaddr( val )
75 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
76 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
78 local parts = luci.util.split( val, ":" )
81 parts[i] = tonumber( parts[i], 16 )
82 if parts[i] < 0 or parts[i] > 255 then
93 function hostname( val )
94 if val and val:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then
95 return true -- XXX: ToDo: need better solution
101 function string( val )
102 return true -- Everything qualifies as valid string
105 function directory( val, seen )
106 local s = luci.fs.stat( val )
109 if s and not seen[s.ino] then
111 if s.type == "directory" then
113 elseif s.type == "link" then
114 return directory( luci.fs.readlink(val), seen )
121 function file( val, seen )
122 local s = luci.fs.stat( val )
125 if s and not seen[s.ino] then
127 if s.type == "regular" then
129 elseif s.type == "link" then
130 return file( luci.fs.readlink(val), seen )