3 UCI Validation Layer - Validation helper
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 os = require "os"
18 local fs = require "nixio.fs"
19 local sys = require "luci.sys"
20 local ERR = require "luci.uvl.errors".error
22 local ipairs, unpack, type, tostring = ipairs, unpack, type, tostring
24 module "luci.uvl.validation"
26 function _exec( bin, args )
27 local cmd, output = "", nil
29 for _, v in ipairs({ bin, unpack(args) }) do
30 cmd = cmd .. ("%q " % v):gsub("([%$`])","\\%1")
33 local tmpfile = "/tmp/uvl" .. sys.uniqueid(8)
34 local retval = os.execute( cmd .. " 1>" .. tmpfile .. " 2>" .. tmpfile )
36 if fs.access(tmpfile) then
37 output = fs.readfile(tmpfile)
44 function check( self, object )
45 if object:scheme('validators') then
46 for _, val in ipairs(object:scheme('validators')) do
47 local ok, err = false, nil
49 local values = object:value()
50 values = type(values) == "table" and values or { values }
53 object:scheme('type'), object:scheme('datatype'),
54 object.cref[1], object.cref[2], object.cref[3] or '',
58 if type(val) == "function" then
59 ok, err = val(unpack(args))
61 ok, err = _exec( val, args )
66 return false, ERR('SME_ERRVAL', object, {tostring(val), err})