# HG changeset patch # User Kim Alvefur # Date 1615254107 -3600 # Node ID c73744fa3bdfb270a9bee859280a9b614578a2af # Parent b3a9030326112a4ca91bfe61b4f0f6cd25cba7de util.jsonschema: Restructure handling of "properties" and "additionalProperties" This is a bit cleaner, I think diff -r b3a903032611 -r c73744fa3bdf teal-src/util/jsonschema.tl --- a/teal-src/util/jsonschema.tl Tue Mar 09 02:38:51 2021 +0100 +++ b/teal-src/util/jsonschema.tl Tue Mar 09 02:41:47 2021 +0100 @@ -237,31 +237,17 @@ 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 + local additional : schema_t | boolean = schema.additionalProperties or true + for k, v in pairs(data) do + local s = schema.properties[k as string] or additional as schema_t + if not validate(s, v) then + return false end end - end - - if schema.additionalProperties then + elseif schema.additionalProperties then 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 - 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 + if not validate(schema.additionalProperties, v) then + return false end end end diff -r b3a903032611 -r c73744fa3bdf util/jsonschema.lua --- a/util/jsonschema.lua Tue Mar 09 02:38:51 2021 +0100 +++ b/util/jsonschema.lua Tue Mar 09 02:41:47 2021 +0100 @@ -156,31 +156,17 @@ 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 + local additional = schema.additionalProperties or true + for k, v in pairs(data) do + local s = schema.properties[k] or additional + if not validate(s, v) then + return false end end - end - - if schema.additionalProperties then + elseif schema.additionalProperties then 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 - 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 + if not validate(schema.additionalProperties, v) then + return false end end end