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
|
||||
# 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