Comparison

util/jsonschema.lua @ 13084:87f646e353cf

util.jsonschema: Implement 'luaPatternProperties' as Lua variant of 'patternProperties' Previous version of this patch used 'patternProperties' but that would only work with simpler ECMA-262 regular expressions are also valid Lua patterns.
author Kim Alvefur <zash@zash.se>
date Sat, 22 Apr 2023 12:14:29 +0200
parent 12989:dee080c2441e
child 13085:0e17cb78264f
comparison
equal deleted inserted replaced
13083:962a746842a0 13084:87f646e353cf
224 return false 224 return false
225 end 225 end
226 end 226 end
227 end 227 end
228 228
229 local seen_properties = {}
230
229 if schema.properties then 231 if schema.properties then
230 for k, sub in pairs(schema.properties) do 232 for k, sub in pairs(schema.properties) do
231 if data[k] ~= nil and not validate(sub, data[k], root) then 233 if data[k] ~= nil and not validate(sub, data[k], root) then
232 return false 234 return false
233 end 235 end
236 seen_properties[k] = true
237 end
238 end
239
240 if schema.luaPatternProperties then
241
242 for pattern, sub in pairs(schema.luaPatternProperties) do
243 for k in pairs(data) do
244 if type(k) == "string" and k:match(pattern) then
245 if not validate(sub, data[k], root) then
246 return false
247 end
248 seen_properties[k] = true
249 end
250 end
234 end 251 end
235 end 252 end
236 253
237 if schema.additionalProperties ~= nil then 254 if schema.additionalProperties ~= nil then
238 for k, v in pairs(data) do 255 for k, v in pairs(data) do
239 if schema.properties == nil or schema.properties[k] == nil then 256 if not seen_properties[k] then
240 if not validate(schema.additionalProperties, v, root) then 257 if not validate(schema.additionalProperties, v, root) then
241 return false 258 return false
242 end 259 end
243 end 260 end
244 end 261 end