aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-03-09 02:41:47 +0100
committerKim Alvefur <zash@zash.se>2021-03-09 02:41:47 +0100
commitd8303be1456b03c623abc004b415add47318b234 (patch)
treea0cb29d807a10c82be82606a7e24d767bdb3d6d6
parentae408259256bfcf6a0f2daabc84241a25286ba45 (diff)
downloadprosody-d8303be1456b03c623abc004b415add47318b234.tar.gz
prosody-d8303be1456b03c623abc004b415add47318b234.zip
util.jsonschema: Restructure handling of "properties" and "additionalProperties"
This is a bit cleaner, I think
-rw-r--r--teal-src/util/jsonschema.tl30
-rw-r--r--util/jsonschema.lua30
2 files changed, 16 insertions, 44 deletions
diff --git a/teal-src/util/jsonschema.tl b/teal-src/util/jsonschema.tl
index 3aa0b6a3..d359207b 100644
--- a/teal-src/util/jsonschema.tl
+++ b/teal-src/util/jsonschema.tl
@@ -237,31 +237,17 @@ type_validators.table = function (schema : schema_t, data : any) : boolean
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
- end
- end
- end
-
- if schema.additionalProperties then
+ local additional : schema_t | boolean = schema.additionalProperties or true
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
+ local s = schema.properties[k as string] or additional as schema_t
+ if not validate(s, v) then
+ return false
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
+ elseif schema.additionalProperties then
+ for k, v in pairs(data) do
+ if not validate(schema.additionalProperties, v) then
+ return false
end
end
end
diff --git a/util/jsonschema.lua b/util/jsonschema.lua
index 170b3dd8..5ba33900 100644
--- a/util/jsonschema.lua
+++ b/util/jsonschema.lua
@@ -156,31 +156,17 @@ type_validators.table = function(schema, data)
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
- end
- end
- end
-
- if schema.additionalProperties then
+ local additional = schema.additionalProperties or true
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
+ local s = schema.properties[k] or additional
+ if not validate(s, v) then
+ return false
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
+ elseif schema.additionalProperties then
+ for k, v in pairs(data) do
+ if not validate(schema.additionalProperties, v) then
+ return false
end
end
end