From d8303be1456b03c623abc004b415add47318b234 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 9 Mar 2021 02:41:47 +0100 Subject: util.jsonschema: Restructure handling of "properties" and "additionalProperties" This is a bit cleaner, I think --- util/jsonschema.lua | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) (limited to 'util/jsonschema.lua') diff --git a/util/jsonschema.lua b/util/jsonschema.lua index 170b3dd8..5ba33900 100644 --- a/util/jsonschema.lua +++ b/util/jsonschema.lua @@ -156,31 +156,17 @@ type_validators.table = function(schema, data) end if schema.properties then - for k, s in pairs(schema.properties) do - if data[k] ~= nil then - if not validate(s, data[k]) then - return false - end - end - end - end - - if schema.additionalProperties then + local additional = schema.additionalProperties or true for k, v in pairs(data) do - if type(k) == "string" then - if not (schema.properties and schema.properties[k]) then - if not validate(schema.additionalProperties, v) then - return false - end - end + local s = schema.properties[k] or additional + if not validate(s, v) then + return false end end - elseif schema.properties then - for k in pairs(data) do - if type(k) == "string" then - if schema.properties[k] == nil then - return false - end + elseif schema.additionalProperties then + for k, v in pairs(data) do + if not validate(schema.additionalProperties, v) then + return false end end end -- cgit v1.2.3