Changeset

11445:c73744fa3bdf

util.jsonschema: Restructure handling of "properties" and "additionalProperties" This is a bit cleaner, I think
author Kim Alvefur <zash@zash.se>
date Tue, 09 Mar 2021 02:41:47 +0100
parents 11444:b3a903032611
children 11446:58c534bac798
files teal-src/util/jsonschema.tl util/jsonschema.lua
diffstat 2 files changed, 16 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/teal-src/util/jsonschema.tl	Tue Mar 09 02:38:51 2021 +0100
+++ b/teal-src/util/jsonschema.tl	Tue Mar 09 02:41:47 2021 +0100
@@ -237,31 +237,17 @@
 		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
+			local additional : schema_t | boolean = schema.additionalProperties or true
+			for k, v in pairs(data) do
+				local s = schema.properties[k as string] or additional as schema_t
+				if not validate(s, v) then
+					return false
 				end
 			end
-		end
-
-		if schema.additionalProperties then
+		elseif schema.additionalProperties then
 			for k, v in pairs(data) do
-				if k is string then
-					if not (schema.properties and schema.properties[k]) then
-						if not validate(schema.additionalProperties, v) then
-							return false
-						end
-					end
-				end
-			end
-		elseif schema.properties then
-			for k in pairs(data) do
-				if k is string then
-					if schema.properties[k] == nil then
-						return false
-					end
+				if not validate(schema.additionalProperties, v) then
+					return false
 				end
 			end
 		end
--- a/util/jsonschema.lua	Tue Mar 09 02:38:51 2021 +0100
+++ b/util/jsonschema.lua	Tue Mar 09 02:41:47 2021 +0100
@@ -156,31 +156,17 @@
 		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
+			local additional = schema.additionalProperties or true
+			for k, v in pairs(data) do
+				local s = schema.properties[k] or additional
+				if not validate(s, v) then
+					return false
 				end
 			end
-		end
-
-		if schema.additionalProperties then
+		elseif schema.additionalProperties then
 			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
-				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
+				if not validate(schema.additionalProperties, v) then
+					return false
 				end
 			end
 		end