* luci/libs/uvl: completed network scheme, add "uint" and "ipaddr" datatypes
[project/luci.git] / libs / uvl / luasrc / uvl / datatypes.lua
1 --[[
2
3 UCI Validation Layer - Datatype Tests
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
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
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17 module( "luci.uvl.datatypes", package.seeall )
18
19 require("luci.fs")
20 require("luci.ip")
21 require("luci.util")
22
23
24 function boolean( val )
25         if val == "1" or val == "yes" or val == "on" then
26                 return true
27         elseif val == "0" or val == "no" or val == "off" then
28                 return true
29         end
30
31         return false
32 end
33
34 function uint( val )
35         local n = tonumber(val)
36         if n ~= nil and math.floor(n) == n and n >= 0 then
37                 return true
38         end
39
40         return false
41 end
42
43 function integer( val )
44         local n = tonumber(val)
45         if n ~= nil and math.floor(n) == n then
46                 return true
47         end
48
49         return false
50 end
51
52 function float( val )
53         return ( tonumber(val) ~= nil )
54 end
55
56 function ipaddr( val )
57         return ip4addr(val) or ip6addr(val)
58 end
59
60 function ip4addr( val )
61         if val then
62                 return luci.ip.IPv4(val) and true or false
63         end
64
65         return false
66 end
67
68 function ip4prefix( val )
69         val = tonumber(val)
70         return ( val and val >= 0 and val <= 32 )
71 end
72
73 function ip6addr( val )
74         if val then
75                 return luci.ip.IPv6(val) and true or false
76         end
77
78         return false
79 end
80
81 function ip6prefix( val )
82         val = tonumber(val)
83         return ( val and val >= 0 and val <= 128 )
84 end
85
86 function macaddr( val )
87         if val and val:match(
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]+$"
90         ) then
91                 local parts = luci.util.split( val, ":" )
92
93                 for i = 1,6 do
94                         parts[i] = tonumber( parts[i], 16 )
95                         if parts[i] < 0 or parts[i] > 255 then
96                                 return false
97                         end
98                 end
99
100                 return true
101         end
102
103         return false
104 end
105
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
109         end
110
111         return false
112 end
113
114 function string( val )
115         return true             -- Everything qualifies as valid string
116 end
117
118 function directory( val, seen )
119         local s = luci.fs.stat( val )
120         seen = seen or { }
121
122         if s and not seen[s.ino] then
123                 seen[s.ino] = true
124                 if s.type == "directory" then
125                         return true
126                 elseif s.type == "link" then
127                         return directory( luci.fs.readlink(val), seen )
128                 end
129         end
130
131         return false
132 end
133
134 function file( val, seen )
135         local s = luci.fs.stat( val )
136         seen = seen or { }
137
138         if s and not seen[s.ino] then
139                 seen[s.ino] = true
140                 if s.type == "regular" then
141                         return true
142                 elseif s.type == "link" then
143                         return file( luci.fs.readlink(val), seen )
144                 end
145         end
146
147         return false
148 end