util.jsonschema: Implement "propertyNames"
This is a bit special in Lua as tables are not limited to string keys
--- a/teal-src/util/jsonschema.tl Tue Mar 09 02:41:47 2021 +0100
+++ b/teal-src/util/jsonschema.tl Tue Mar 09 02:43:50 2021 +0100
@@ -54,6 +54,7 @@
dependentRequired : { string : { string } }
additionalProperties: schema_t
patternProperties: schema_t
+ propertyNames : schema_t
-- xml
record xml_t
@@ -239,6 +240,9 @@
if schema.properties then
local additional : schema_t | boolean = schema.additionalProperties or true
for k, v in pairs(data) do
+ if schema.propertyNames and not validate(schema.propertyNames, k) then
+ return false
+ end
local s = schema.properties[k as string] or additional as schema_t
if not validate(s, v) then
return false
@@ -246,6 +250,9 @@
end
elseif schema.additionalProperties then
for k, v in pairs(data) do
+ if schema.propertyNames and not validate(schema.propertyNames, k) then
+ return false
+ end
if not validate(schema.additionalProperties, v) then
return false
end
--- a/util/jsonschema.lua Tue Mar 09 02:41:47 2021 +0100
+++ b/util/jsonschema.lua Tue Mar 09 02:43:50 2021 +0100
@@ -158,6 +158,9 @@
if schema.properties then
local additional = schema.additionalProperties or true
for k, v in pairs(data) do
+ if schema.propertyNames and not validate(schema.propertyNames, k) then
+ return false
+ end
local s = schema.properties[k] or additional
if not validate(s, v) then
return false
@@ -165,6 +168,9 @@
end
elseif schema.additionalProperties then
for k, v in pairs(data) do
+ if schema.propertyNames and not validate(schema.propertyNames, k) then
+ return false
+ end
if not validate(schema.additionalProperties, v) then
return false
end