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