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`
|
||||
Validates multiple `CoreValidationSingle`s at once.
|
||||
Validates a Dictionary against multiple `CoreValidationSingle`s.
|
||||
|
||||
|
||||
## Functions
|
||||
### *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
|
||||
### *CoreValidationSingle* <u>get_single</u>(*anything you like* <u>data</u>, *Node* <u>parent</u>)
|
||||
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).
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
# 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.
|
||||
## 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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue