util.jsonschema: Implement "propertyNames"
authorKim Alvefur <zash@zash.se>
Tue, 09 Mar 2021 02:43:50 +0100
changeset 11450 58c534bac798
parent 11449 c73744fa3bdf
child 11451 7ad137fe665b
util.jsonschema: Implement "propertyNames" This is a bit special in Lua as tables are not limited to string keys
teal-src/util/jsonschema.tl
util/jsonschema.lua
--- 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