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 local fs = require "luci.fs"
18 local ip = require "luci.ip"
19 local math = require "math"
20 local util = require "luci.util"
22 local tonumber = tonumber
24 module "luci.uvl.datatypes"
27 function boolean( val )
28 if val == "1" or val == "yes" or val == "on" or val == "true" then
30 elseif val == "0" or val == "no" or val == "off" or val == "false" then
38 local n = tonumber(val)
39 if n ~= nil and math.floor(n) == n and n >= 0 then
46 function integer( val )
47 local n = tonumber(val)
48 if n ~= nil and math.floor(n) == n then
56 return ( tonumber(val) ~= nil )
59 function ipaddr( val )
60 return ip4addr(val) or ip6addr(val)
63 function ip4addr( val )
65 return ip.IPv4(val) and true or false
71 function ip4prefix( val )
73 return ( val and val >= 0 and val <= 32 )
76 function ip6addr( val )
78 return ip.IPv6(val) and true or false
84 function ip6prefix( val )
86 return ( val and val >= 0 and val <= 128 )
91 return ( val and val >= 1 and val <= 65535 )
94 function portrange( val )
95 local p1, p2 = val:match("^(%d+)%-(%d+)$")
96 if p1 and p2 and port(p1) and port(p2) then
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]+$"
108 local parts = util.split( val, ":" )
111 parts[i] = tonumber( parts[i], 16 )
112 if parts[i] < 0 or parts[i] > 255 then
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
132 return hostname(val) or ipaddr(val)
135 function string( val )
136 return true -- Everything qualifies as valid string
139 function directory( val, seen )
140 local s = fs.stat( val )
143 if s and not seen[s.ino] then
145 if s.type == "directory" then
147 elseif s.type == "link" then
148 return directory( fs.readlink(val), seen )
155 function file( val, seen )
156 local s = fs.stat( val )
159 if s and not seen[s.ino] then
161 if s.type == "regular" then
163 elseif s.type == "link" then
164 return file( fs.readlink(val), seen )