2
0
Fork 0

Update to d384216b42d1db42f050443f28993cae078883e7

This commit is contained in:
JeremyStar™ 2024-05-11 02:03:06 +02:00
parent 3ce648508c
commit 3e75e46115
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
# Check Dictionary
for key in 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 }))
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 }))
_check_dictionary_recursive(schema)
func _check_dictionary_recursive(parent_dict: Dictionary, path: String = "") -> bool:
var success: bool = false
for key in parent_dict:
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_dict[key])):
TYPE_OBJECT:
if parent_dict[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_dict[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]:
var random: String = str(randf()).repeat(50)
var failed: Array[String] = []
for key in schema:
_evaluate_recursive(random, schema)
return failed
func _evaluate_recursive(random: String, parent_dict: Dictionary, path: String = "") -> Array[String]:
var failed: Array[String] = []
for key in parent_dict:
# Check if key exists in data
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:
# 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]
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:
# Append failures from single
failed.append(key + ": " + failure)
return failed