Fix: incorrect array element validation in SegmentType (#23289)

This commit is contained in:
Yongtao Huang
2025-08-01 22:46:50 +08:00
committed by GitHub
parent ec488a4c43
commit be914438a5
2 changed files with 27 additions and 4 deletions

View File

@@ -109,7 +109,7 @@ class SegmentType(StrEnum):
elif array_validation == ArrayValidation.FIRST:
return element_type.is_valid(value[0])
else:
return all([element_type.is_valid(i, array_validation=ArrayValidation.NONE)] for i in value)
return all(element_type.is_valid(i, array_validation=ArrayValidation.NONE) for i in value)
def is_valid(self, value: Any, array_validation: ArrayValidation = ArrayValidation.FIRST) -> bool:
"""
@@ -152,7 +152,7 @@ class SegmentType(StrEnum):
_ARRAY_ELEMENT_TYPES_MAPPING: Mapping[SegmentType, SegmentType] = {
# ARRAY_ANY does not have correpond element type.
# ARRAY_ANY does not have corresponding element type.
SegmentType.ARRAY_STRING: SegmentType.STRING,
SegmentType.ARRAY_NUMBER: SegmentType.NUMBER,
SegmentType.ARRAY_OBJECT: SegmentType.OBJECT,

View File

@@ -1,4 +1,4 @@
from core.variables.types import SegmentType
from core.variables.types import ArrayValidation, SegmentType
class TestSegmentTypeIsArrayType:
@@ -17,7 +17,6 @@ class TestSegmentTypeIsArrayType:
value is tested for the is_array_type method.
"""
# Arrange
all_segment_types = set(SegmentType)
expected_array_types = [
SegmentType.ARRAY_ANY,
SegmentType.ARRAY_STRING,
@@ -58,3 +57,27 @@ class TestSegmentTypeIsArrayType:
for seg_type in enum_values:
is_array = seg_type.is_array_type()
assert isinstance(is_array, bool), f"is_array_type does not return a boolean for segment type {seg_type}"
class TestSegmentTypeIsValidArrayValidation:
"""
Test SegmentType.is_valid with array types using different validation strategies.
"""
def test_array_validation_all_success(self):
value = ["hello", "world", "foo"]
assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.ALL)
def test_array_validation_all_fail(self):
value = ["hello", 123, "world"]
# Should return False, since 123 is not a string
assert not SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.ALL)
def test_array_validation_first(self):
value = ["hello", 123, None]
assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.FIRST)
def test_array_validation_none(self):
value = [1, 2, 3]
# validation is None, skip
assert SegmentType.ARRAY_STRING.is_valid(value, array_validation=ArrayValidation.NONE)