# HG changeset patch # User Kim Alvefur # Date 1679836807 -7200 # Node ID dee080c2441e588c59e105d6919a0e847ddddf42 # Parent 8592770be63af225842c4f77a6167ee0f8097517 util.jsonschema: Implement 'dependentSchemas' If this object key exists then this schema must validate against the current object. Seems useful. diff -r 8592770be63a -r dee080c2441e spec/util_jsonschema_spec.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", diff -r 8592770be63a -r dee080c2441e teal-src/prosody/util/jsonschema.tl --- 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 } = {} diff -r 8592770be63a -r dee080c2441e util/jsonschema.lua --- 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 = {}