util.jsonschema: Implement 'dependentSchemas'
authorKim Alvefur <zash@zash.se>
Sun, 26 Mar 2023 15:20:07 +0200
changeset 12993 dee080c2441e
parent 12992 8592770be63a
child 12994 939049732317
util.jsonschema: Implement 'dependentSchemas' If this object key exists then this schema must validate against the current object. Seems useful.
spec/util_jsonschema_spec.lua
teal-src/prosody/util/jsonschema.tl
util/jsonschema.lua
--- a/spec/util_jsonschema_spec.lua	Sun Mar 26 15:19:14 2023 +0200
+++ b/spec/util_jsonschema_spec.lua	Sun Mar 26 15:20:07 2023 +0200
@@ -19,7 +19,7 @@
 	["const.json:9"] = "deepcompare",
 	["contains.json:0:5"] = "distinguishing objects from arrays",
 	["defs.json"] = "need built-in meta-schema",
-	["dependentSchemas.json"] = "NYI",
+	["dependentSchemas.json:2:2"] = "NYI", -- minProperties
 	["dynamicRef.json"] = "NYI",
 	["enum.json:1:3"] = "deepcompare",
 	["id.json"] = "NYI",
--- a/teal-src/prosody/util/jsonschema.tl	Sun Mar 26 15:19:14 2023 +0200
+++ b/teal-src/prosody/util/jsonschema.tl	Sun Mar 26 15:20:07 2023 +0200
@@ -71,6 +71,7 @@
 	additionalProperties: schema_t
 	patternProperties: schema_t -- NYI
 	propertyNames : schema_t
+	dependentSchemas : { string : schema_t }
 
 	-- xml
 	record xml_t
@@ -333,6 +334,14 @@
 			end
 		end
 
+		if schema.dependentSchemas then
+			for k, sub in pairs(schema.dependentSchemas) do
+				if data[k] ~= nil and not validate(sub, data, root) then
+					return false
+				end
+			end
+		end
+
 		if schema.uniqueItems then
 			-- only works for scalars, would need to deep-compare for objects/arrays/tables
 			local values : { any : boolean } = {}
--- a/util/jsonschema.lua	Sun Mar 26 15:19:14 2023 +0200
+++ b/util/jsonschema.lua	Sun Mar 26 15:20:07 2023 +0200
@@ -244,6 +244,14 @@
 			end
 		end
 
+		if schema.dependentSchemas then
+			for k, sub in pairs(schema.dependentSchemas) do
+				if data[k] ~= nil and not validate(sub, data, root) then
+					return false
+				end
+			end
+		end
+
 		if schema.uniqueItems then
 
 			local values = {}