mirror of
https://github.com/Doodle3D/doodle3d-firmware.git
synced 2024-12-22 11:03:48 +01:00
More helpfull validation errors
This commit is contained in:
parent
cd6754bfe1
commit
a85528e956
@ -1,4 +1,5 @@
|
|||||||
local log = require('util.logger')
|
local log = require('util.logger')
|
||||||
|
local utils = require('util.utils')
|
||||||
local settings = require('util.settings')
|
local settings = require('util.settings')
|
||||||
local printer = require('util.printer')
|
local printer = require('util.printer')
|
||||||
local signin = require('network.signin')
|
local signin = require('network.signin')
|
||||||
@ -19,12 +20,17 @@ function M._global_GET(request, response)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M._global_POST(request, response)
|
function M._global_POST(request, response)
|
||||||
|
--log:info("API:config:set")
|
||||||
response:setSuccess()
|
response:setSuccess()
|
||||||
for k,v in pairs(request:getAll()) do
|
for k,v in pairs(request:getAll()) do
|
||||||
|
--log:info(" "..k..": "..v);
|
||||||
local r,m = settings.set(k, v)
|
local r,m = settings.set(k, v)
|
||||||
|
|
||||||
if r then response:addData(k, "ok")
|
if r then
|
||||||
else response:addData(k, "could not set key ('" .. m .. "')")
|
response:addData(k, "ok")
|
||||||
|
else
|
||||||
|
response:addData(k, "could not save setting ('" .. m .. "')")
|
||||||
|
log:info(" m: "..utils.dump(m))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
local uci = require('uci').cursor()
|
local uci = require('uci').cursor()
|
||||||
local utils = require('util.utils')
|
local utils = require('util.utils')
|
||||||
local baseconfig = require('conf_defaults')
|
local baseconfig = require('conf_defaults')
|
||||||
|
local utils = require('util.utils')
|
||||||
|
local log = require('util.logger')
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@ -107,18 +109,25 @@ local function isValid(value, baseTable)
|
|||||||
|
|
||||||
elseif varType == 'int' or varType == 'float' then
|
elseif varType == 'int' or varType == 'float' then
|
||||||
local numValue = tonumber(value)
|
local numValue = tonumber(value)
|
||||||
local ok = numValue and true or false
|
if numValue == nil then
|
||||||
ok = ok and (varType == 'float' or math.floor(numValue) == numValue)
|
return nil, "invalid number"
|
||||||
if min then ok = ok and numValue >= min end
|
elseif varType == 'int' and math.floor(numValue) ~= numValue then
|
||||||
if max then ok = ok and numValue <= max end
|
return nil, "invalid int"
|
||||||
return ok or nil,"invalid int/float value or out of range"
|
elseif min and numValue < min then
|
||||||
|
return nil, "to low"
|
||||||
|
elseif max and numValue > max then
|
||||||
|
return nil, "to high"
|
||||||
|
end
|
||||||
|
|
||||||
elseif varType == 'string' then
|
elseif varType == 'string' then
|
||||||
local ok = true
|
local ok = true
|
||||||
if min then ok = ok and value:len() >= min end
|
if min and value:len() < min then
|
||||||
if max then ok = ok and value:len() <= max end
|
return nil,"to short"
|
||||||
if regex then ok = ok and value:match(regex) ~= nil end
|
elseif max and value:len() > max then
|
||||||
return ok or nil,"invalid string value"
|
return nil,"to long"
|
||||||
|
elseif regex and value:match(regex) == nil then
|
||||||
|
return nil,"invalid value"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@ -191,6 +200,7 @@ end
|
|||||||
-- @treturn bool|nil True if everything went well, nil in case of error.
|
-- @treturn bool|nil True if everything went well, nil in case of error.
|
||||||
-- @treturn ?string Error message in case first return value is nil (invalid key).
|
-- @treturn ?string Error message in case first return value is nil (invalid key).
|
||||||
function M.set(key, value)
|
function M.set(key, value)
|
||||||
|
--log:info("settings:set: "..utils.dump(key))
|
||||||
key = replaceDots(key)
|
key = replaceDots(key)
|
||||||
|
|
||||||
local r = utils.create(UCI_CONFIG_FILE)
|
local r = utils.create(UCI_CONFIG_FILE)
|
||||||
@ -198,11 +208,11 @@ function M.set(key, value)
|
|||||||
|
|
||||||
local base = getBaseKeyTable(key)
|
local base = getBaseKeyTable(key)
|
||||||
if not base then return nil,ERR_NO_SUCH_KEY end
|
if not base then return nil,ERR_NO_SUCH_KEY end
|
||||||
|
|
||||||
if M.isDefault(key) and value == nil then return true end -- key is default already
|
if M.isDefault(key) and value == nil then return true end -- key is default already
|
||||||
|
--log:info(" not default")
|
||||||
local current = uci:get(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key)
|
local current = uci:get(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key)
|
||||||
|
--log:info(" base.type: "..utils.dump(base.type))
|
||||||
if base.type == 'bool' then
|
if base.type == 'bool' then
|
||||||
if value ~= "" then
|
if value ~= "" then
|
||||||
value = utils.toboolean(value)
|
value = utils.toboolean(value)
|
||||||
@ -216,15 +226,16 @@ function M.set(key, value)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local valid,m = isValid(value, base)
|
||||||
|
if not valid then
|
||||||
|
return nil,m
|
||||||
|
end
|
||||||
|
|
||||||
if fromUciValue(current, base.type) == value then return true end
|
if fromUciValue(current, base.type) == value then return true end
|
||||||
|
|
||||||
if value ~= nil then
|
if value ~= nil then
|
||||||
local valid,m = isValid(value, base)
|
uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key, toUciValue(value, base.type))
|
||||||
if (valid) then
|
|
||||||
uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key, toUciValue(value, base.type))
|
|
||||||
else
|
|
||||||
return nil,m
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
uci:delete(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key)
|
uci:delete(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user