CoreValidationSchemas are now even cooler!

(they now support nested Dictionaries)
This commit is contained in:
JeremyStar™ 2024-05-11 02:01:46 +02:00
parent a081d71bed
commit bcea9399bc
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -40,22 +40,56 @@ func _init(core_new: Core, schema_new: Dictionary, data_new: Dictionary, parent_
data = data_new data = data_new
# Check Dictionary # Check Dictionary
for key in schema: _check_dictionary_recursive(schema)
if typeof(key) != TYPE_STRING: logger.error(core.stringify_variables("Could not parse schema: Schema key %key% is not of type String", { "key": key }))
elif typeof(schema[key]) != TYPE_OBJECT: logger.error(core.stringify_variables("Could not parse schema: Schema value of %key% is not of type Object", { "key": key })) func _check_dictionary_recursive(parent: Dictionary, path: String = "") -> bool:
elif schema[key].get_class() != "Node": logger.error(core.stringify_variables("Could not parse schema: Schema value of %key% is not of type Node", { "key": key })) var success: bool = false
for key in parent:
if typeof(key) != TYPE_STRING:
logger.error(core.stringify_variables("Could not parse schema: Schema key %key% is not of type String", { "key": path + "/" + key }))
success = false
continue
match(typeof(parent[key])):
TYPE_OBJECT:
if parent[key].get_class() != "Node":
logger.error(core.stringify_variables("Could not parse schema: Schema value of %key% is not of type Node", { "key": path + "/" + key }))
success = false
continue
TYPE_DICTIONARY:
_check_dictionary_recursive(parent[key], path + "/" + key)
_:
logger.error(core.stringify_variables("Could not parse schema: Schema value of %key% is not of type CoreValidationSingle or Dictionary", { "key": path + "/" + key }))
success = false
continue
return success
func evaluate() -> Array[String]: func evaluate() -> Array[String]:
var random: String = str(randf()).repeat(50) var random: String = str(randf()).repeat(50)
var failed: Array[String] = [] var failed: Array[String] = []
for key in schema: _evaluate_recursive(random, schema)
return failed
func _evaluate_recursive(random: String, parent: Dictionary, path: String = "") -> Array[String]:
var failed: Array[String] = []
for key in parent:
# Check if key exists in data
if data.get(key, random) == random: if data.get(key, random) == random:
failed.append(core.stringify_variables("Key %key% is present in schema but missing in data", { "key": key })) # Does not exist, append error
failed.append(core.stringify_variables("Key %key% is present in schema but missing in data", { "key": path + "/" + key }))
else: else:
# Exists in data
if typeof(schema[key]) == TYPE_DICTIONARY:
# Key is of type Dictionary, allow for recursion to happen
failed.append_array(_evaluate_recursive(random, schema[key], path + "/" + key))
else:
# Key is not of type Dictionary, evaluate against data
schema[key].data = data[key] schema[key].data = data[key]
if !schema[key].evaluate(): if !schema[key].evaluate():
logger.error(core.stringify_variables("Validation for key %key% failed", { "key": key })) logger.error(core.stringify_variables("Validation for key %key% failed", { "key": path + "/" + key }))
for failure in schema[key].failures: for failure in schema[key].failures:
# Append failures from single
failed.append(key + ": " + failure) failed.append(key + ": " + failure)
return failed return failed