Changeset

12989:dee080c2441e

util.jsonschema: Implement 'dependentSchemas' If this object key exists then this schema must validate against the current object. Seems useful.
author Kim Alvefur <zash@zash.se>
date Sun, 26 Mar 2023 15:20:07 +0200
parents 12988:8592770be63a
children 12990:939049732317
files spec/util_jsonschema_spec.lua teal-src/prosody/util/jsonschema.tl util/jsonschema.lua
diffstat 3 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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 = {}