Changeset

4397:1378e3c79c34

util.dataforms: Add field verification logic
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 11 Aug 2011 23:58:26 +0200
parents 4396:03b59a511671
children 4398:acc37e221940
files util/dataforms.lua
diffstat 1 files changed, 81 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/util/dataforms.lua	Thu Oct 13 00:51:50 2011 +0100
+++ b/util/dataforms.lua	Thu Aug 11 23:58:26 2011 +0200
@@ -8,9 +8,10 @@
 
 local setmetatable = setmetatable;
 local pairs, ipairs = pairs, ipairs;
-local tostring, type = tostring, type;
+local tostring, type, next = tostring, type, next;
 local t_concat = table.concat;
 local st = require "util.stanza";
+local jid_prep = require "util.jid".prep;
 
 module "dataforms"
 
@@ -104,24 +105,32 @@
 end
 
 local field_readers = {};
+local field_verifiers = {};
 
 function form_t.data(layout, stanza)
 	local data = {};
-	
-	for field_tag in stanza:childtags() do
-		local field_type;
-		for n, field in ipairs(layout) do
+	local errors = {};
+
+	for _, field in ipairs(layout) do
+		local tag;
+		for field_tag in stanza:childtags() do
 			if field.name == field_tag.attr.var then
-				field_type = field.type;
+				tag = field_tag;
 				break;
 			end
 		end
-		
-		local reader = field_readers[field_type];
+
+		local reader = field_readers[field.type];
+		local verifier = field.verifier or field_verifiers[field.type];
 		if reader then
-			data[field_tag.attr.var] = reader(field_tag);
+			data[field.name] = reader(tag);
+			if verifier then
+				errors[field.name] = verifier(data[field.name], tag, field.required);
+			end
 		end
-		
+	end
+	if next(errors) then
+		return data, errors;
 	end
 	return data;
 end
@@ -134,12 +143,32 @@
 		end
 	end
 
+field_verifiers["text-single"] =
+	function (data, field_tag, required)
+		if ((not data) or (#data == 0)) and required then
+			return "Required value missing";
+		end
+	end
+
 field_readers["text-private"] =
 	field_readers["text-single"];
 
+field_verifiers["text-private"] =
+	field_verifiers["text-single"];
+
 field_readers["jid-single"] =
 	field_readers["text-single"];
 
+field_verifiers["jid-single"] =
+	function (data, field_tag, required)
+		if #data == 0 and required then
+			return "Required value missing";
+		end
+		if not jid_prep(data) then
+			return "Invalid JID";
+		end
+	end
+
 field_readers["jid-multi"] =
 	function (field_tag)
 		local result = {};
@@ -151,6 +180,19 @@
 		return result;
 	end
 
+field_verifiers["jid-multi"] =
+	function (data, field_tag, required)
+		if #data == 0 and required then
+			return "Required value missing";
+		end
+
+		for _, jid in ipairs(data) do
+			if not jid_prep(jid) then
+				return "Invalid JID";
+			end
+		end
+	end
+
 field_readers["text-multi"] =
 	function (field_tag)
 		local result = {};
@@ -162,9 +204,15 @@
 		return t_concat(result, "\n");
 	end
 
+field_verifiers["text-multi"] =
+	field_verifiers["text-single"];
+
 field_readers["list-single"] =
 	field_readers["text-single"];
 
+field_verifiers["list-single"] =
+	field_verifiers["text-single"];
+
 field_readers["list-multi"] =
 	function (field_tag)
 		local result = {};
@@ -176,6 +224,13 @@
 		return result;
 	end
 
+field_verifiers["list-multi"] =
+	function (data, field_tag, required)
+		if #data == 0 and required then
+			return "Required value missing";
+		end
+	end
+
 field_readers["boolean"] =
 	function (field_tag)
 		local value = field_tag:child_with_name("value");
@@ -188,6 +243,17 @@
 		end
 	end
 
+field_verifiers["boolean"] =
+	function (data, field_tag, required)
+		data = field_readers["text-single"](field_tag);
+		if #data == 0 and required then
+			return "Required value missing";
+		end
+		if data ~= "1" and data ~= "true" and data ~= "0" and data ~= "false" then
+			return "Invalid boolean representation";
+		end
+	end
+
 field_readers["hidden"] =
 	function (field_tag)
 		local value = field_tag:child_with_name("value");
@@ -195,6 +261,11 @@
 			return value[1];
 		end
 	end
+
+field_verifiers["hidden"] =
+	function (data, field_tag, required)
+		return nil;
+	end
 	
 return _M;