Optimized uvl.dependencies
[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 local fs = require "luci.fs"
18 local ip = require "luci.ip"
19 local math = require "math"
20 local util = require "luci.util"
21
22 local tonumber = tonumber
23
24 module "luci.uvl.datatypes"
25
26
27 function boolean( val )
28         if val == "1" or val == "yes" or val == "on" or val == "true" then
29                 return true
30         elseif val == "0" or val == "no" or val == "off" or val == "false" then
31                 return true
32         end
33
34         return false
35 end
36
37 function uint( val )
38         local n = tonumber(val)
39         if n ~= nil and math.floor(n) == n and n >= 0 then
40                 return true
41         end
42
43         return false
44 end
45
46 function integer( val )
47         local n = tonumber(val)
48         if n ~= nil and math.floor(n) == n then
49                 return true
50         end
51
52         return false
53 end
54
55 function float( val )
56         return ( tonumber(val) ~= nil )
57 end
58
59 function ipaddr( val )
60         return ip4addr(val) or ip6addr(val)
61 end
62
63 function ip4addr( val )
64         if val then
65                 return ip.IPv4(val) and true or false
66         end
67
68         return false
69 end
70
71 function ip4prefix( val )
72         val = tonumber(val)
73         return ( val and val >= 0 and val <= 32 )
74 end
75
76 function ip6addr( val )
77         if val then
78                 return ip.IPv6(val) and true or false
79         end
80
81         return false
82 end
83
84 function ip6prefix( val )
85         val = tonumber(val)
86         return ( val and val >= 0 and val <= 128 )
87 end
88
89 function port( val )
90         val = tonumber(val)
91         return ( val and val >= 1 and val <= 65535 )
92 end
93
94 function portrange( val )
95         local p1, p2 = val:match("^(%d+)%-(%d+)$")
96         if p1 and p2 and port(p1) and port(p2) then
97                 return true
98         else
99                 return port(val)
100         end
101 end
102
103 function macaddr( val )
104         if val and val:match(
105                 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
106                  "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
107         ) then
108                 local parts = util.split( val, ":" )
109
110                 for i = 1,6 do
111                         parts[i] = tonumber( parts[i], 16 )
112                         if parts[i] < 0 or parts[i] > 255 then
113                                 return false
114                         end
115                 end
116
117                 return true
118         end
119
120         return false
121 end
122
123 function hostname( val )
124         if val and val:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then
125                 return true     -- XXX: ToDo: need better solution
126         end
127
128         return false
129 end
130
131 function host( val )
132         return hostname(val) or ipaddr(val)
133 end
134
135 function string( val )
136         return true             -- Everything qualifies as valid string
137 end
138
139 function directory( val, seen )
140         local s = fs.stat( val )
141         seen = seen or { }
142
143         if s and not seen[s.ino] then
144                 seen[s.ino] = true
145                 if s.type == "directory" then
146                         return true
147                 elseif s.type == "link" then
148                         return directory( fs.readlink(val), seen )
149                 end
150         end
151
152         return false
153 end
154
155 function file( val, seen )
156         local s = fs.stat( val )
157         seen = seen or { }
158
159         if s and not seen[s.ino] then
160                 seen[s.ino] = true
161                 if s.type == "regular" then
162                         return true
163                 elseif s.type == "link" then
164                         return file( fs.readlink(val), seen )
165                 end
166         end
167
168         return false
169 end