Add CoreValidationSchema code

This commit is contained in:
JeremyStar™ 2024-05-05 14:53:35 +02:00
parent 7d8c5f295a
commit 3ee429c63b
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 73 additions and 3 deletions

View file

@ -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 <https://www.gnu.org/licenses/>.
## 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

View file

@ -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)