From 3ce648508c4d3c0ed6aee1bebd127f81cf14723a Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 11 May 2024 01:40:47 +0200 Subject: [PATCH] Update to a081d71bed2298c387a888a09db5c4f2d170b276 --- src/classes/validationschema.gd | 22 ++++++++++++++++------ src/validation.gd | 14 ++++++++++---- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/classes/validationschema.gd b/src/classes/validationschema.gd index da345df..9c24f30 100644 --- a/src/classes/validationschema.gd +++ b/src/classes/validationschema.gd @@ -15,7 +15,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -## Validates multiple [CoreValidationSingle]s at once. +## Validates a Dictionary against multiple [CoreValidationSingle]s. extends Node class_name CoreValidationSchema @@ -28,12 +28,16 @@ var parent: Node ## Contains the schema to validate.[br] ## [b]Note: [i]Don't modify.[/i][/b] var schema: Dictionary = {} +## Contains the data to validate.[br] +## [b]Note: [i]Don't modify.[/i][/b] +var data: Dictionary = {} -func _init(core_new: Core, schema_new: Dictionary, parent_new: Node) -> void: +func _init(core_new: Core, schema_new: Dictionary, data_new: Dictionary, parent_new: Node) -> void: core = core_new logger = core.logger.get_instance(core.basepath.replace("res://", "") + "src/classes/validationschema.gd", self) parent = parent_new schema = schema_new + data = data_new # Check Dictionary for key in schema: @@ -42,10 +46,16 @@ func _init(core_new: Core, schema_new: Dictionary, parent_new: Node) -> void: 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 })) func evaluate() -> Array[String]: + var random: String = str(randf()).repeat(50) var failed: Array[String] = [] - for single in schema: - if !schema[single].evaluate(): - logger.error(core.stringify_variables("Single %single% failed", { "single": single })) - failed.append(single) + for key in schema: + if data.get(key, random) == random: + failed.append(core.stringify_variables("Key %key% is present in schema but missing in data", { "key": key })) + else: + schema[key].data = data[key] + if !schema[key].evaluate(): + logger.error(core.stringify_variables("Validation for key %key% failed", { "key": key })) + for failure in schema[key].failures: + failed.append(key + ": " + failure) return failed diff --git a/src/validation.gd b/src/validation.gd index eefdc52..cb819a9 100644 --- a/src/validation.gd +++ b/src/validation.gd @@ -59,14 +59,20 @@ func _schedule() -> void: schema.queue_free() # +++ data validation +++ -## Returns a new [CoreValidationSingle] +## Returns a new [CoreValidationSingle]. func get_single(data, parent: Node) -> CoreValidationSingle: var single: CoreValidationSingle = CoreValidationSingle.new(core, data, parent) singles.append(single) return single -## Returns a new [CoreValidationSchema] -func get_schema(schema_dict: Dictionary, parent: Node) -> CoreValidationSchema: - var schema: CoreValidationSchema = CoreValidationSchema.new(core, schema_dict, parent) +## Returns a new [CoreValidationSingle] for use in [CoreValidationSchema]s. +func get_single_schema(parent: Node) -> CoreValidationSingle: + var single: CoreValidationSingle = CoreValidationSingle.new(core, null, parent) + singles.append(single) + return single + +## Returns a new [CoreValidationSchema]. +func get_schema(schema_new: Dictionary, data_new: Dictionary, parent: Node) -> CoreValidationSchema: + var schema: CoreValidationSchema = CoreValidationSchema.new(core, schema_new, data_new, parent) schemas.append(schema) return schema