From 3ee429c63b7b5aeea6beaec51c3faee0fdd918a2 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 5 May 2024 14:53:35 +0200 Subject: [PATCH] Add CoreValidationSchema code --- src/classes/validationschema.gd | 51 +++++++++++++++++++++++++++++++++ src/validation.gd | 25 ++++++++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 src/classes/validationschema.gd diff --git a/src/classes/validationschema.gd b/src/classes/validationschema.gd new file mode 100644 index 0000000..da345df --- /dev/null +++ b/src/classes/validationschema.gd @@ -0,0 +1,51 @@ +# CORE FRAMEWORK SOURCE FILE +# Copyright (c) 2024 The StarOpenSource Project & Contributors +# Licensed under the GNU Affero General Public License v3 +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# 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. +extends Node +class_name CoreValidationSchema + +## Internal, don't modify. +var core: Core +## Internal, don't modify. +var logger: CoreLoggerInstance +## Internal, don't modify. +var parent: Node +## Contains the schema to validate.[br] +## [b]Note: [i]Don't modify.[/i][/b] +var schema: Dictionary = {} + +func _init(core_new: Core, schema_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 + + # 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 })) + +func evaluate() -> Array[String]: + 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) + + return failed diff --git a/src/validation.gd b/src/validation.gd index feb0832..ea50adf 100644 --- a/src/validation.gd +++ b/src/validation.gd @@ -18,7 +18,7 @@ ## Allows for data validation. extends CoreBaseModule -#var schemas: Dictionary +var schemas: Array[CoreValidationSchema] var singles: Array[CoreValidationSingle] # +++ module +++ @@ -33,16 +33,35 @@ func _cleanup() -> void: single.queue_free() for single in singles_remove_enty: singles.remove_at(singles.find(single)) + + # Schemas + var schemas_remove_enty: Array[CoreLoggerInstance] = [] + for schema in schemas: + singles_remove_enty.append(schema) + if !is_instance_valid(schema): continue + if !is_instance_valid(schema.parent): + logger.diag("Removing schema '" + schema.name + "'") + schema.queue_free() + for schema in schemas_remove_enty: + schemas.remove_at(schemas.find(schema)) + func _schedule() -> void: # Singles for single in singles: if is_instance_valid(single): logger.diag("Removing single '" + single.name + "'") single.queue_free() + + # Schemas + for schema in schemas: + if is_instance_valid(schema): + logger.diag("Removing schema '" + schema.name + "'") + schema.queue_free() # +++ data validation +++ -#func get_schema(Dictionary schema) -> CoreValidationSchema: -# return CoreValidationSchema.new(core, schema) +func get_schema(schema: Dictionary, parent: Node) -> CoreValidationSchema: + schemas.append(parent) + return CoreValidationSchema.new(core, schema, parent) func get_single(data, parent: Node) -> CoreValidationSingle: singles.append(parent)