Make CoreValidationSchema's 1000x better
This commit is contained in:
parent
cc75f108da
commit
a081d71bed
4 changed files with 31 additions and 13 deletions
|
@ -4,9 +4,9 @@ description: Used for validating multiple CoreValidationSingles.
|
||||||
---
|
---
|
||||||
|
|
||||||
# `CoreValidationSchema`
|
# `CoreValidationSchema`
|
||||||
Validates multiple `CoreValidationSingle`s at once.
|
Validates a Dictionary against multiple `CoreValidationSingle`s.
|
||||||
|
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
### *Array[String]* <u>evaluate</u>()
|
### *Array[String]* <u>evaluate</u>()
|
||||||
Evaluates all singles and returns the names of all failed singles in an array.
|
Evaluates all singles and returns all encountered errors.
|
||||||
|
|
|
@ -9,5 +9,7 @@ description: Allows for data validation.
|
||||||
## Functions
|
## Functions
|
||||||
### *CoreValidationSingle* <u>get_single</u>(*anything you like* <u>data</u>, *Node* <u>parent</u>)
|
### *CoreValidationSingle* <u>get_single</u>(*anything you like* <u>data</u>, *Node* <u>parent</u>)
|
||||||
Returns a new [`CoreValidationSingle`](/reference/classes/validationsingle).
|
Returns a new [`CoreValidationSingle`](/reference/classes/validationsingle).
|
||||||
### *CoreValidationSchema* <u>get_schema</u>(*Dictionary* <u>schema</u>, *Node* <u>parent</u>)
|
### *CoreValidationSingle* <u>get_single_schema</u>(*Node* <u>parent</u>)
|
||||||
|
Returns a new [`CoreValidationSingle`](/reference/classes/validationsingle) for use in [`CoreValidationSchema`](/reference/classes/validationschema)s.
|
||||||
|
### *CoreValidationSchema* <u>get_schema</u>(*Dictionary* <u>schema</u>, *Dictionary* <u>data</u>, *Node* <u>parent</u>)
|
||||||
Returns a new [`CoreValidationSchema`](/reference/classes/validationschema).
|
Returns a new [`CoreValidationSchema`](/reference/classes/validationschema).
|
||||||
|
|
|
@ -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