Skip to content

Commit 7e16d03

Browse files
authored
Update array_validator.py
Signed-off-by: Bandi Revanth <[email protected]>
1 parent 6ccd9d1 commit 7e16d03

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

sanatio/array_validator.py

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
1+
from typing import Any, List, Optional
12
from sanatio.base_class import BaseValidator
23

34

45
class ArrayValidator(BaseValidator):
5-
6-
def isArray(self, value) -> bool:
7-
""" check if the string is array or not """
6+
def is_array(self, value: Any) -> bool:
7+
"""Check if the value is a list (array)."""
88
return isinstance(value, list)
99

10-
def isArrayLength(self, value, min: int = 0, max: int = None) -> bool:
11-
""" check if the array length is between min and max """
12-
return min <= len(value) <= max
13-
14-
def isContains(self, value, contains) -> bool:
15-
""" check if the array contains the element or not """
16-
return contains in value
17-
18-
def isUnique(self, value) -> bool:
19-
""" check if the array contains unique elements or not """
20-
return len(value) == len(set(value))
21-
22-
def isMultidimensional(self, value) -> bool:
23-
""" check if the array is multidimensional or not """
10+
def is_array_length(self, value: List[Any], min_length: int = 0, max_length: Optional[int] = None) -> bool:
11+
"""Check if the array length is between min_length and max_length (inclusive)."""
12+
if not self.is_array(value):
13+
return False
14+
if max_length is not None:
15+
return min_length <= len(value) <= max_length
16+
return len(value) >= min_length
17+
18+
def is_contains(self, value: List[Any], item: Any) -> bool:
19+
"""Check if the array contains a given element."""
20+
if not self.is_array(value):
21+
return False
22+
return item in value
23+
24+
def is_unique(self, value: List[Any]) -> bool:
25+
"""Check if all elements in the array are unique."""
26+
if not self.is_array(value):
27+
return False
28+
try:
29+
return len(value) == len(set(value))
30+
except TypeError:
31+
# Handles case where elements are unhashable (e.g., lists)
32+
seen = []
33+
for i in value:
34+
if i in seen:
35+
return False
36+
seen.append(i)
37+
return True
38+
39+
def is_multidimensional(self, value: List[Any]) -> bool:
40+
"""Check if the array is multidimensional (contains at least one list)."""
41+
if not self.is_array(value):
42+
return False
2443
return any(isinstance(i, list) for i in value)
2544

26-
def isMinlength(self, value, min: int) -> bool:
27-
""" check if the array length is greater than min """
28-
return len(value) > min
29-
30-
def isMaxlength(self, value, max: int) -> bool:
31-
""" check if the array length is less than max """
32-
return len(value) < max
45+
def is_min_length(self, value: List[Any], min_length: int) -> bool:
46+
"""Check if the array length is greater than or equal to min_length."""
47+
if not self.is_array(value):
48+
return False
49+
return len(value) >= min_length
50+
51+
def is_max_length(self, value: List[Any], max_length: int) -> bool:
52+
"""Check if the array length is less than or equal to max_length."""
53+
if not self.is_array(value):
54+
return False
55+
return len(value) <= max_length

0 commit comments

Comments
 (0)