Update to a081d71bed2298c387a888a09db5c4f2d170b276
This commit is contained in:
parent
e41505b4fc
commit
3ce648508c
2 changed files with 26 additions and 10 deletions
|
@ -15,7 +15,7 @@
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
## Validates multiple [CoreValidationSingle]s at once.
|
## Validates a Dictionary against multiple [CoreValidationSingle]s.
|
||||||
extends Node
|
extends Node
|
||||||
class_name CoreValidationSchema
|
class_name CoreValidationSchema
|
||||||
|
|
||||||
|
@ -28,12 +28,16 @@ var parent: Node
|
||||||
## Contains the schema to validate.[br]
|
## Contains the schema to validate.[br]
|
||||||
## [b]Note: [i]Don't modify.[/i][/b]
|
## [b]Note: [i]Don't modify.[/i][/b]
|
||||||
var schema: Dictionary = {}
|
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
|
core = core_new
|
||||||
logger = core.logger.get_instance(core.basepath.replace("res://", "") + "src/classes/validationschema.gd", self)
|
logger = core.logger.get_instance(core.basepath.replace("res://", "") + "src/classes/validationschema.gd", self)
|
||||||
parent = parent_new
|
parent = parent_new
|
||||||
schema = schema_new
|
schema = schema_new
|
||||||
|
data = data_new
|
||||||
|
|
||||||
# Check Dictionary
|
# Check Dictionary
|
||||||
for key in schema:
|
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 }))
|
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]:
|
func evaluate() -> Array[String]:
|
||||||
|
var random: String = str(randf()).repeat(50)
|
||||||
var failed: Array[String] = []
|
var failed: Array[String] = []
|
||||||
for single in schema:
|
for key in schema:
|
||||||
if !schema[single].evaluate():
|
if data.get(key, random) == random:
|
||||||
logger.error(core.stringify_variables("Single %single% failed", { "single": single }))
|
failed.append(core.stringify_variables("Key %key% is present in schema but missing in data", { "key": key }))
|
||||||
failed.append(single)
|
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
|
return failed
|
||||||
|
|
|
@ -59,14 +59,20 @@ func _schedule() -> void:
|
||||||
schema.queue_free()
|
schema.queue_free()
|
||||||
|
|
||||||
# +++ data validation +++
|
# +++ data validation +++
|
||||||
## Returns a new [CoreValidationSingle]
|
## Returns a new [CoreValidationSingle].
|
||||||
func get_single(data, parent: Node) -> CoreValidationSingle:
|
func get_single(data, parent: Node) -> CoreValidationSingle:
|
||||||
var single: CoreValidationSingle = CoreValidationSingle.new(core, data, parent)
|
var single: CoreValidationSingle = CoreValidationSingle.new(core, data, parent)
|
||||||
singles.append(single)
|
singles.append(single)
|
||||||
return single
|
return single
|
||||||
|
|
||||||
## Returns a new [CoreValidationSchema]
|
## Returns a new [CoreValidationSingle] for use in [CoreValidationSchema]s.
|
||||||
func get_schema(schema_dict: Dictionary, parent: Node) -> CoreValidationSchema:
|
func get_single_schema(parent: Node) -> CoreValidationSingle:
|
||||||
var schema: CoreValidationSchema = CoreValidationSchema.new(core, schema_dict, parent)
|
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)
|
schemas.append(schema)
|
||||||
return schema
|
return schema
|
||||||
|
|
Loading…
Reference in a new issue