Changeset

12988:8592770be63a

util.jsonschema: Implement 'dependentRequired' If this field exists, then these fields must also exist.
author Kim Alvefur <zash@zash.se>
date Sun, 26 Mar 2023 15:19:14 +0200
parents 12987:2cf8d98d8a28
children 12989:dee080c2441e
files spec/util_jsonschema_spec.lua teal-src/prosody/util/jsonschema.tl util/jsonschema.lua
diffstat 3 files changed, 24 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_jsonschema_spec.lua	Sun Mar 26 13:13:31 2023 +0200
+++ b/spec/util_jsonschema_spec.lua	Sun Mar 26 15:19:14 2023 +0200
@@ -19,7 +19,6 @@
 	["const.json:9"] = "deepcompare",
 	["contains.json:0:5"] = "distinguishing objects from arrays",
 	["defs.json"] = "need built-in meta-schema",
-	["dependentRequired.json"] = "NYI",
 	["dependentSchemas.json"] = "NYI",
 	["dynamicRef.json"] = "NYI",
 	["enum.json:1:3"] = "deepcompare",
--- a/teal-src/prosody/util/jsonschema.tl	Sun Mar 26 13:13:31 2023 +0200
+++ b/teal-src/prosody/util/jsonschema.tl	Sun Mar 26 15:19:14 2023 +0200
@@ -295,6 +295,18 @@
 			end
 		end
 
+		if schema.dependentRequired then
+			for k, reqs in pairs(schema.dependentRequired) do
+				if data[k] ~= nil then
+					for _, req in ipairs(reqs) do
+						if data[req] == nil then
+							return false
+						end
+					end
+				end
+			end
+		end
+
 		if schema.propertyNames ~= nil then
 			for k in pairs(data) do
 				if not validate(schema.propertyNames, k, root) then
--- a/util/jsonschema.lua	Sun Mar 26 13:13:31 2023 +0200
+++ b/util/jsonschema.lua	Sun Mar 26 15:19:14 2023 +0200
@@ -206,6 +206,18 @@
 			end
 		end
 
+		if schema.dependentRequired then
+			for k, reqs in pairs(schema.dependentRequired) do
+				if data[k] ~= nil then
+					for _, req in ipairs(reqs) do
+						if data[req] == nil then
+							return false
+						end
+					end
+				end
+			end
+		end
+
 		if schema.propertyNames ~= nil then
 			for k in pairs(data) do
 				if not validate(schema.propertyNames, k, root) then