aboutsummaryrefslogtreecommitdiffstats
path: root/util/dataforms.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/dataforms.lua')
-rw-r--r--util/dataforms.lua44
1 files changed, 40 insertions, 4 deletions
diff --git a/util/dataforms.lua b/util/dataforms.lua
index 052d6a55..66733895 100644
--- a/util/dataforms.lua
+++ b/util/dataforms.lua
@@ -10,9 +10,11 @@ local setmetatable = setmetatable;
local ipairs = ipairs;
local type, next = type, next;
local tonumber = tonumber;
+local tostring = tostring;
local t_concat = table.concat;
local st = require "util.stanza";
local jid_prep = require "util.jid".prep;
+local datetime = require "util.datetime";
local _ENV = nil;
-- luacheck: std none
@@ -54,6 +56,12 @@ function form_t.form(layout, data, formtype)
if formtype == "form" and field.datatype then
form:tag("validate", { xmlns = xmlns_validate, datatype = field.datatype });
+ if field.range_min or field.range_max then
+ form:tag("range", {
+ min = field.range_min and tostring(field.range_min),
+ max = field.range_max and tostring(field.range_max),
+ }):up();
+ end
-- <basic/> assumed
form:up();
end
@@ -95,8 +103,15 @@ function form_t.form(layout, data, formtype)
if value ~= nil then
if type(value) == "number" then
- -- TODO validate that this is ok somehow, eg check field.datatype
- value = ("%g"):format(value);
+ if field.datatype == "xs:dateTime" then
+ value = datetime.datetime(value);
+ elseif field_type == "boolean" then
+ value = value ~= 0;
+ elseif field.datatype == "xs:double" or field.datatype == "xs:decimal" then
+ value = ("%f"):format(value);
+ else
+ value = ("%d"):format(value);
+ end
end
-- Add value, depending on type
if field_type == "hidden" then
@@ -136,7 +151,7 @@ function form_t.form(layout, data, formtype)
local media = field.media;
if media then
- form:tag("media", { xmlns = "urn:xmpp:media-element", height = media.height, width = media.width });
+ form:tag("media", { xmlns = "urn:xmpp:media-element", height = ("%d"):format(media.height), width = ("%d"):format(media.width) });
for _, val in ipairs(media) do
form:tag("uri", { type = val.type }):text(val.uri):up()
end
@@ -290,13 +305,34 @@ field_readers["hidden"] =
end
data_validators["xs:integer"] =
- function (data)
+ function (data, field)
local n = tonumber(data);
if not n then
return false, "not a number";
elseif n % 1 ~= 0 then
return false, "not an integer";
end
+ if field.range_max and n > field.range_max then
+ return false, "out of bounds";
+ elseif field.range_min and n < field.range_min then
+ return false, "out of bounds";
+ end
+ return true, n;
+ end
+
+data_validators["pubsub:integer-or-max"] =
+ function (data, field)
+ if data == "max" then
+ return true, data;
+ else
+ return data_validators["xs:integer"](data, field);
+ end
+ end
+
+data_validators["xs:dateTime"] =
+ function(data, field) -- luacheck: ignore 212/field
+ local n = datetime.parse(data);
+ if not n then return false, "invalid timestamp"; end
return true, n;
end