Software /
code /
prosody
Comparison
util/dataforms.lua @ 4884:b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 17 May 2012 21:01:44 +0200 |
parent | 4435:ea69296afaef |
child | 4886:26d8e4665ce9 |
comparison
equal
deleted
inserted
replaced
4883:b798d59919b1 | 4884:b8d852aea3ad |
---|---|
103 end | 103 end |
104 return form; | 104 return form; |
105 end | 105 end |
106 | 106 |
107 local field_readers = {}; | 107 local field_readers = {}; |
108 local field_verifiers = {}; | |
109 | 108 |
110 function form_t.data(layout, stanza) | 109 function form_t.data(layout, stanza) |
111 local data = {}; | 110 local data = {}; |
112 local errors = {}; | 111 local errors = {}; |
113 | 112 |
124 if field.required then | 123 if field.required then |
125 errors[field.name] = "Required value missing"; | 124 errors[field.name] = "Required value missing"; |
126 end | 125 end |
127 else | 126 else |
128 local reader = field_readers[field.type]; | 127 local reader = field_readers[field.type]; |
129 local verifier = field.verifier or field_verifiers[field.type]; | |
130 if reader then | 128 if reader then |
131 data[field.name] = reader(tag); | 129 data[field.name], errors[field.name] = reader(tag, field.required); |
132 if verifier then | |
133 errors[field.name] = verifier(data[field.name], tag, field.required); | |
134 end | |
135 end | 130 end |
136 end | 131 end |
137 end | 132 end |
138 if next(errors) then | 133 if next(errors) then |
139 return data, errors; | 134 return data, errors; |
140 end | 135 end |
141 return data; | 136 return data; |
142 end | 137 end |
143 | 138 |
144 field_readers["text-single"] = | 139 field_readers["text-single"] = |
145 function (field_tag) | 140 function (field_tag, required) |
146 local value = field_tag:child_with_name("value"); | 141 local data = field_tag:get_child_text("value"); |
147 if value then | 142 if data and #data > 0 then |
148 return value[1]; | 143 return data |
149 end | 144 elseif required then |
150 end | 145 return nil, "Required value missing"; |
151 | |
152 field_verifiers["text-single"] = | |
153 function (data, field_tag, required) | |
154 if ((not data) or (#data == 0)) and required then | |
155 return "Required value missing"; | |
156 end | 146 end |
157 end | 147 end |
158 | 148 |
159 field_readers["text-private"] = | 149 field_readers["text-private"] = |
160 field_readers["text-single"]; | 150 field_readers["text-single"]; |
161 | 151 |
162 field_verifiers["text-private"] = | |
163 field_verifiers["text-single"]; | |
164 | |
165 field_readers["jid-single"] = | 152 field_readers["jid-single"] = |
166 field_readers["text-single"]; | 153 function (field_tag, required) |
167 | 154 local raw_data = field_tag:get_child_text("value") |
168 field_verifiers["jid-single"] = | 155 local data = jid_prep(raw_data); |
169 function (data, field_tag, required) | 156 if data and #data > 0 then |
170 if ((not data) or (#data == 0)) and required then | 157 return data |
171 return "Required value missing"; | 158 elseif raw_data then |
172 end | 159 return raw_data, "Invalid JID"; |
173 if not jid_prep(data) then | 160 elseif required then |
174 return "Invalid JID"; | 161 return nil, "Required value missing"; |
175 end | 162 end |
176 end | 163 end |
177 | 164 |
178 field_readers["jid-multi"] = | 165 field_readers["jid-multi"] = |
179 function (field_tag) | 166 function (field_tag, required) |
180 local result = {}; | 167 local result = {}; |
181 for value_tag in field_tag:childtags() do | 168 local err = {}; |
182 if value_tag.name == "value" then | 169 for value_tag in field_tag:childtags("value") do |
183 result[#result+1] = value_tag[1]; | 170 local raw_value = value_tag:get_text(); |
184 end | 171 local value = jid_prep(raw_value); |
185 end | 172 result[#result+1] = value; |
186 return result; | 173 if raw_value and not value then |
187 end | 174 err[#err+1] = ("Invalid JID: " .. raw_value); |
188 | 175 end |
189 field_verifiers["jid-multi"] = | 176 end |
190 function (data, field_tag, required) | 177 if #result > 0 then |
191 if #data == 0 and required then | 178 return result, (#err > 0 and t_concat(err, "\n") or nil); |
192 return "Required value missing"; | 179 elseif required then |
193 end | 180 return nil, "Required value missing"; |
194 | 181 end |
195 for _, jid in ipairs(data) do | 182 end |
196 if not jid_prep(jid) then | 183 |
197 return "Invalid JID"; | 184 field_readers["list-multi"] = |
198 end | 185 function (field_tag, required) |
199 end | 186 local result = {}; |
187 for value in field_tag:childtags("value") do | |
188 result[#result+1] = value; | |
189 end | |
190 return result, (required and #result == 0 and "Required value missing" or nil); | |
200 end | 191 end |
201 | 192 |
202 field_readers["text-multi"] = | 193 field_readers["text-multi"] = |
203 function (field_tag) | 194 function (field_tag, required) |
204 local result = {}; | 195 local data, err = field_readers["list-multi"](field_tag, required); |
205 for value_tag in field_tag:childtags() do | 196 if data then |
206 if value_tag.name == "value" then | 197 data = t_concat(data, "\n"); |
207 result[#result+1] = value_tag[1]; | 198 end |
208 end | 199 return data, err; |
209 end | 200 end |
210 return t_concat(result, "\n"); | |
211 end | |
212 | |
213 field_verifiers["text-multi"] = | |
214 field_verifiers["text-single"]; | |
215 | 201 |
216 field_readers["list-single"] = | 202 field_readers["list-single"] = |
217 field_readers["text-single"]; | 203 field_readers["text-single"]; |
218 | 204 |
219 field_verifiers["list-single"] = | 205 local boolean_values = { |
220 field_verifiers["text-single"]; | 206 ["1"] = true, ["true"] = true, |
221 | 207 ["0"] = false, ["false"] = false, |
222 field_readers["list-multi"] = | 208 }; |
223 function (field_tag) | |
224 local result = {}; | |
225 for value_tag in field_tag:childtags() do | |
226 if value_tag.name == "value" then | |
227 result[#result+1] = value_tag[1]; | |
228 end | |
229 end | |
230 return result; | |
231 end | |
232 | |
233 field_verifiers["list-multi"] = | |
234 function (data, field_tag, required) | |
235 if #data == 0 and required then | |
236 return "Required value missing"; | |
237 end | |
238 end | |
239 | 209 |
240 field_readers["boolean"] = | 210 field_readers["boolean"] = |
241 function (field_tag) | 211 function (field_tag, required) |
242 local value = field_tag:child_with_name("value"); | 212 local raw_value = field_tag:get_child_text("value"); |
243 if value then | 213 local value = boolean_values[raw_value ~= nil and raw_value]; |
244 if value[1] == "1" or value[1] == "true" then | 214 if value ~= nil then |
245 return true; | 215 return value; |
246 else | 216 elseif raw_value then |
247 return false; | |
248 end | |
249 end | |
250 end | |
251 | |
252 field_verifiers["boolean"] = | |
253 function (data, field_tag, required) | |
254 data = field_readers["text-single"](field_tag); | |
255 if ((not data) or (#data == 0)) and required then | |
256 return "Required value missing"; | |
257 end | |
258 if data ~= "1" and data ~= "true" and data ~= "0" and data ~= "false" then | |
259 return "Invalid boolean representation"; | 217 return "Invalid boolean representation"; |
218 elseif required then | |
219 return nil, "Required value missing"; | |
260 end | 220 end |
261 end | 221 end |
262 | 222 |
263 field_readers["hidden"] = | 223 field_readers["hidden"] = |
264 function (field_tag) | 224 function (field_tag) |
265 local value = field_tag:child_with_name("value"); | 225 return field_tag:get_child_text("value"); |
266 if value then | 226 end |
267 return value[1]; | 227 |
268 end | |
269 end | |
270 | |
271 field_verifiers["hidden"] = | |
272 function (data, field_tag, required) | |
273 return nil; | |
274 end | |
275 | |
276 return _M; | 228 return _M; |
277 | 229 |
278 | 230 |
279 --[=[ | 231 --[=[ |
280 | 232 |