From 48380cd8c33172e3aeee310e14e9ee622b31d551 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 11 May 2024 02:40:08 +0200 Subject: [PATCH] Add more compatibility to is_not_empty() --- docs/docs/reference/classes/validationsingle.md | 2 +- src/classes/validationsingle.gd | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/docs/reference/classes/validationsingle.md b/docs/docs/reference/classes/validationsingle.md index 2131848..c7e61d7 100644 --- a/docs/docs/reference/classes/validationsingle.md +++ b/docs/docs/reference/classes/validationsingle.md @@ -57,7 +57,7 @@ Matches a regular expression against `data`. \ Applies to **String** & **StringName**. ### *CoreValidationSingle* is_not_empty() Ensures that `data` is not empty. \ -Applies to **String** & **StringName** (`!= ""`), **int** (`!= 0`) and **float** (`!= 0.0`). +Applies to **String** & **StringName** (`!= ""`), **int** (`!= 0`), **float** (`!= 0.0`), **Array** (`!= []`) and **Dictionary** (`!= {}`). ### *CoreValidationSingle* is_not_null() Ensures that `data` is not `null`. ### *CoreValidationSingle* is_normalized() diff --git a/src/classes/validationsingle.gd b/src/classes/validationsingle.gd index b871699..5bc337b 100644 --- a/src/classes/validationsingle.gd +++ b/src/classes/validationsingle.gd @@ -166,12 +166,14 @@ func evaluate() -> bool: if !result: failures.append(core.stringify_variables("Data doesn't match regex %regex%", { "regex": rule["regex_string"] })) CoreTypes.ValidationType.IS_NOT_EMPTY: match(typeof(data)): - Variant.Type.TYPE_STRING: if data == "": failures.append("Data string is empty") - Variant.Type.TYPE_STRING_NAME: if data == "": failures.append("Data string name is empty") - Variant.Type.TYPE_INT: if data == 0: failures.append("Data integer is zero") - Variant.Type.TYPE_FLOAT: if data == 0.0: failures.append("Data float is zero") - # If not a String, StringName, int or float, skip - _: logger.warn("Can't determine if data is null as data is not of type String, StringName, int or float") + Variant.Type.TYPE_STRING: if data == "": failures.append("Data (String) is empty") + Variant.Type.TYPE_STRING_NAME: if data == "": failures.append("Data (StringName) is empty") + Variant.Type.TYPE_INT: if data == 0: failures.append("Data (int) is zero") + Variant.Type.TYPE_FLOAT: if data == 0.0: failures.append("Data (float) is zero") + Variant.Type.TYPE_ARRAY: if data == []: failures.append("Data (Array) is empty") + Variant.Type.TYPE_DICTIONARY: if data == {}: failures.append("Data (Dictionary) is empty") + # If not supported, skip + _: logger.warn("Can't determine if data is null as data is not of type String, StringName, int, float, Array or Dictionary") CoreTypes.ValidationType.IS_NOT_NULL: # ⡴⠑⡄⠀⠀⠀⠀⠀⠀⠀ ⣀⣀⣤⣤⣤⣀⡀ # ⠸⡇⠀⠿⡀⠀⠀⠀⣀⡴⢿⣿⣿⣿⣿⣿⣿⣿⣷⣦⡀ @@ -289,7 +291,7 @@ func matches_regex(regex_string: String) -> CoreValidationSingle: # +++ 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]). +## Applies to [String] & [StringName] ([code]!= ""[/code]), [int] ([code]!= 0[/code]), [float] ([code]!= 0.0[/code]), [Array] ([code]!= [][/code]) and [Dictionary] ([code]!= {}[/code]). func is_not_empty() -> CoreValidationSingle: rules.append({ "type": CoreTypes.ValidationType.IS_NOT_EMPTY }) return self