|
|
@ -1,21 +1,3 @@
|
|
|
|
# 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 some data against a set of rules.
|
|
|
|
|
|
|
|
extends Node
|
|
|
|
extends Node
|
|
|
|
class_name CoreValidationSingle
|
|
|
|
class_name CoreValidationSingle
|
|
|
|
|
|
|
|
|
|
|
@ -30,10 +12,10 @@ var parent: Node
|
|
|
|
## [b]Note: [i]Don't modify.[/i][/b]
|
|
|
|
## [b]Note: [i]Don't modify.[/i][/b]
|
|
|
|
var data # This is the only instance where we don't want to define what type this variable should have
|
|
|
|
var data # This is the only instance where we don't want to define what type this variable should have
|
|
|
|
|
|
|
|
|
|
|
|
## All rules to evaluate in [method evaluate].[br]
|
|
|
|
## All rules to evalute in [method evaluate].[br]
|
|
|
|
## [b]Note: [i]Don't modify.[/i][/b]
|
|
|
|
## [b]Note: [i]Don't modify.[/i][/b]
|
|
|
|
var rules: Array[Dictionary]
|
|
|
|
var rules: Array[Dictionary]
|
|
|
|
## Contains error messages for failed rules.[br]
|
|
|
|
## The amount of failures.[br]
|
|
|
|
## [b]Note: [i]Don't modify.[/i][/b]
|
|
|
|
## [b]Note: [i]Don't modify.[/i][/b]
|
|
|
|
var failures: Array[String] = []
|
|
|
|
var failures: Array[String] = []
|
|
|
|
|
|
|
|
|
|
|
@ -130,13 +112,26 @@ func evaluate() -> bool:
|
|
|
|
# Perform check
|
|
|
|
# Perform check
|
|
|
|
if data > rule["maximum"]: failures.append(core.stringify_variables("Data is smaller than minimum %type% value %expected%", { "type": rule["matched_against"], "expected": rule["maximum"] }))
|
|
|
|
if data > rule["maximum"]: failures.append(core.stringify_variables("Data is smaller than minimum %type% value %expected%", { "type": rule["matched_against"], "expected": rule["maximum"] }))
|
|
|
|
CoreTypes.ValidationType.HAS_VALUES:
|
|
|
|
CoreTypes.ValidationType.HAS_VALUES:
|
|
|
|
|
|
|
|
# Used later
|
|
|
|
var success: bool = false
|
|
|
|
var success: bool = false
|
|
|
|
|
|
|
|
|
|
|
|
for value in rule["values"]:
|
|
|
|
if rule["all_required"]:
|
|
|
|
if data == value:
|
|
|
|
# Invert usage
|
|
|
|
success = true
|
|
|
|
success = true
|
|
|
|
|
|
|
|
|
|
|
|
if !success: failures.append("Data did not match a single provided value")
|
|
|
|
for value in rule["values"]:
|
|
|
|
|
|
|
|
if data != value:
|
|
|
|
|
|
|
|
# One value did not match
|
|
|
|
|
|
|
|
success = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !success: failures.append("Data did not match all provided values")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
for value in rule["values"]:
|
|
|
|
|
|
|
|
if data == value:
|
|
|
|
|
|
|
|
# One value did match
|
|
|
|
|
|
|
|
success = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !success: failures.append("Data did not match a single provided value")
|
|
|
|
CoreTypes.ValidationType.CONTAINS:
|
|
|
|
CoreTypes.ValidationType.CONTAINS:
|
|
|
|
# If not a String or StringName, skip
|
|
|
|
# If not a String or StringName, skip
|
|
|
|
if typeof(data) != Variant.Type.TYPE_STRING or typeof(data) != Variant.Type.TYPE_STRING_NAME:
|
|
|
|
if typeof(data) != Variant.Type.TYPE_STRING or typeof(data) != Variant.Type.TYPE_STRING_NAME:
|
|
|
@ -218,94 +213,68 @@ func evaluate() -> bool:
|
|
|
|
return failures.size() == 0
|
|
|
|
return failures.size() == 0
|
|
|
|
|
|
|
|
|
|
|
|
# +++ types and classes +++
|
|
|
|
# +++ types and classes +++
|
|
|
|
## Validates if [param data] matches some [enum Variant.Type].[br]
|
|
|
|
## Validates if [param data] matches some [enum Variant.Type].
|
|
|
|
## Applies to all data types (obviously).
|
|
|
|
|
|
|
|
func matches_type(types: Array[Variant.Type]) -> CoreValidationSingle:
|
|
|
|
func matches_type(types: Array[Variant.Type]) -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.MATCHES_TYPE, "types": types })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.MATCHES_TYPE, "types": types })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Validates if [param data] matches some class.[br]
|
|
|
|
## Validates if [param data] matches some class.
|
|
|
|
## Applies to [Object].
|
|
|
|
|
|
|
|
func matches_class(clazz: StringName, exact: bool) -> CoreValidationSingle:
|
|
|
|
func matches_class(clazz: StringName, exact: bool) -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.MATCHES_CLASS, "class": clazz, "exact": exact })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.MATCHES_CLASS, "class": clazz, "exact": exact })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
# +++ ranges +++
|
|
|
|
# +++ ranges +++
|
|
|
|
## Validates if [param data] contains the specified integer range.[br]
|
|
|
|
## Validates if [param data] contains
|
|
|
|
## Applies to [int].
|
|
|
|
|
|
|
|
func in_range_int(from: int, to: int) -> CoreValidationSingle:
|
|
|
|
func in_range_int(from: int, to: int) -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IN_RANGE, "matched_against": "integer", "from": from, "to": to })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IN_RANGE, "matched_against": "integer", "from": from, "to": to })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Validates if [param data] contains the specified float range.[br]
|
|
|
|
|
|
|
|
## Applies to [float].
|
|
|
|
|
|
|
|
func in_range_float(from: float, to: float) -> CoreValidationSingle:
|
|
|
|
func in_range_float(from: float, to: float) -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IN_RANGE, "matched_against": "float", "from": from, "to": to })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IN_RANGE, "matched_against": "float", "from": from, "to": to })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Ensures that [param data] is equal to or exceeds the specified integer.[br]
|
|
|
|
func has_minimum_value_int(minimum: int) -> CoreValidationSingle:
|
|
|
|
## Applies to [int].
|
|
|
|
|
|
|
|
func has_minimum_int(minimum: int) -> CoreValidationSingle:
|
|
|
|
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_MINIMUM, "matched_against": "integer", "minimum": minimum })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_MINIMUM, "matched_against": "integer", "minimum": minimum })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Ensures that [param data] is under the specified integer.[br]
|
|
|
|
func has_maximum_value_int(maximum: int) -> CoreValidationSingle:
|
|
|
|
## Applies to [int].
|
|
|
|
|
|
|
|
func has_maximum_int(maximum: int) -> CoreValidationSingle:
|
|
|
|
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_MAXIMUM, "matched_against": "integer", "maximum": maximum })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_MAXIMUM, "matched_against": "integer", "maximum": maximum })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Ensures that [param data] is equal to or exceeds the specified float.[br]
|
|
|
|
func has_minimum_value_float(minimum: float) -> CoreValidationSingle:
|
|
|
|
## Applies to [float].
|
|
|
|
|
|
|
|
func has_minimum_float(minimum: float) -> CoreValidationSingle:
|
|
|
|
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_MINIMUM, "matched_against": "float", "minimum": minimum })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_MINIMUM, "matched_against": "float", "minimum": minimum })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Ensures that [param data] is under the specified float.[br]
|
|
|
|
func has_maximum_value_float(maximum: float) -> CoreValidationSingle:
|
|
|
|
## Applies to [float].
|
|
|
|
|
|
|
|
func has_maximum_float(maximum: float) -> CoreValidationSingle:
|
|
|
|
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_MAXIMUM, "matched_against": "float", "maximum": maximum })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_MAXIMUM, "matched_against": "float", "maximum": maximum })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
# +++ values +++
|
|
|
|
# +++ values +++
|
|
|
|
## Checks whenether at least one value matches [param data].[br]
|
|
|
|
func has_values(values: Array, all_required: bool) -> CoreValidationSingle:
|
|
|
|
## Applies to all data types.
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_VALUES, "values": values, "all_required": all_required })
|
|
|
|
func has_values(values: Array) -> CoreValidationSingle:
|
|
|
|
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_VALUES, "values": values })
|
|
|
|
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Ensures that [param data] contains at least <[code]minimum_matches[/code]> values.[br]
|
|
|
|
|
|
|
|
## Applies to [String] & [StringName].
|
|
|
|
|
|
|
|
func contains(values: Array[String], minimum_matches: int = 1) -> CoreValidationSingle:
|
|
|
|
func contains(values: Array[String], minimum_matches: int = 1) -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_VALUES, "values": values, "minimum_matches": minimum_matches })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.HAS_VALUES, "values": values, "minimum_matches": minimum_matches })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Matches a regular expression against [param data].[br]
|
|
|
|
|
|
|
|
## Applies to [String] & [StringName].
|
|
|
|
|
|
|
|
func matches_regex(regex_string: String) -> CoreValidationSingle:
|
|
|
|
func matches_regex(regex_string: String) -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.MATCHES_REGEX, "regex_string": regex_string })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.MATCHES_REGEX, "regex_string": regex_string })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
# +++ empty/null and booleans +++
|
|
|
|
# +++ empty/null and booleans +++
|
|
|
|
## Ensures that [param data] is not empty.[br]
|
|
|
|
|
|
|
|
## Applies to [String] & [StringName] ([code]!= ""[/code]), [int] ([code]!= 0[/code]) and [float] ([code]!= 0.0[/code]).
|
|
|
|
|
|
|
|
func is_not_empty() -> CoreValidationSingle:
|
|
|
|
func is_not_empty() -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IS_NOT_EMPTY })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IS_NOT_EMPTY })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Ensures that [param data] is not [code]null[/code].
|
|
|
|
|
|
|
|
func is_not_null() -> CoreValidationSingle:
|
|
|
|
func is_not_null() -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IS_NOT_NULL })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IS_NOT_NULL })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Ensures that [param data] is normalized.[br]
|
|
|
|
|
|
|
|
## Applies to [Vector2], [Vector3], [Vector4], [Plane] and [Quaternion].
|
|
|
|
|
|
|
|
func is_normalized() -> CoreValidationSingle:
|
|
|
|
func is_normalized() -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IS_NORMALIZED })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IS_NORMALIZED })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
## Ensures that [param data] is orthonormalized.[br]
|
|
|
|
|
|
|
|
## Applies to [Transform2D], [Transform3D] and [Basis].
|
|
|
|
|
|
|
|
func is_orthonormalized() -> CoreValidationSingle:
|
|
|
|
func is_orthonormalized() -> CoreValidationSingle:
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IS_ORTHONORMALIZED })
|
|
|
|
rules.append({ "type": CoreTypes.ValidationType.IS_ORTHONORMALIZED })
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|