Changeset

11874:84f4c6957d62

util.dataforms: Add support for datetime field types via XEP-0122
author Kim Alvefur <zash@zash.se>
date Mon, 25 Oct 2021 21:45:46 +0200
parents 11873:2b85e4e7d389
children 11875:210a785dfa8a
files spec/util_dataforms_spec.lua util/dataforms.lua
diffstat 2 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_dataforms_spec.lua	Mon Oct 25 21:45:06 2021 +0200
+++ b/spec/util_dataforms_spec.lua	Mon Oct 25 21:45:46 2021 +0200
@@ -446,6 +446,20 @@
 			assert.table(e);
 			assert.string(e.number);
 		end);
+
+		describe("datetime", function ()
+			local f = dataforms.new { { name = "when"; type = "text-single"; datatype = "xs:dateTime" } } -- luacheck: ignore 431
+
+			it("works", function ()
+				local x = f:form({ when = "2008-08-22T21:09:00Z" });
+				assert.equal("2008-08-22T21:09:00Z", x:find("field/value#"))
+				local d, e = f:data(x);
+				assert.is_nil(e);
+				assert.same({ when = 1219439340 }, d);
+			end);
+
+		end)
+
 	end);
 	describe("media element", function ()
 		it("produced media element correctly", function ()
--- a/util/dataforms.lua	Mon Oct 25 21:45:06 2021 +0200
+++ b/util/dataforms.lua	Mon Oct 25 21:45:46 2021 +0200
@@ -14,6 +14,7 @@
 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
@@ -321,6 +322,13 @@
 		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
+
 
 local function get_form_type(form)
 	if not st.is_stanza(form) then