From 8f0443205ddda14e99cc1d6e74a668c4cb7bfd24 Mon Sep 17 00:00:00 2001 From: Deepak Raj <54245038+codePerfectPlus@users.noreply.github.com> Date: Thu, 30 May 2024 10:20:20 +0530 Subject: [PATCH] is_slug_added --- docs/string/string_validation.rst | 8 ++++++++ sanatio/main.py | 13 ++++--------- tests/string_case_test.py | 9 +++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/docs/string/string_validation.rst b/docs/string/string_validation.rst index 8c5fd79..29596a7 100644 --- a/docs/string/string_validation.rst +++ b/docs/string/string_validation.rst @@ -59,3 +59,11 @@ The following functions are used to validate strings. >>> val.contains("abc", "d") False +:code:`isSlug(input_string)` + Returns true if the string contains the substring. + + >>> val.contains("foo-bar") + True + >>> val.contains("foo bar) + False + diff --git a/sanatio/main.py b/sanatio/main.py index 718183f..6ac1662 100644 --- a/sanatio/main.py +++ b/sanatio/main.py @@ -20,8 +20,6 @@ def __isvalidString(self, value: str) -> bool: if isinstance(value, str): return True - return False - def __isvalidNumber(self, value: int)-> bool: """ check if the number is valid or not """ if value is None: @@ -30,8 +28,6 @@ def __isvalidNumber(self, value: int)-> bool: if isinstance(value, (int, float)): return True - return False - def __isvalidBoolean(self, value: bool)-> bool: """ check if the string is boolean or not """ if value is None: @@ -40,8 +36,6 @@ def __isvalidBoolean(self, value: bool)-> bool: if isinstance(value, bool): return True - return False - def isAadharCard(self, value)-> bool: """ check if the string is Aadhar card or not """ regex = "^[2-9]{1}[0-9]{3}[0-9]{4}[0-9]{4}$" # need to improve regex for space and hyphen @@ -55,8 +49,6 @@ def isAadharCard(self, value)-> bool: if checksum_aadhar(value): return True - return False - def isPostalCode(self, value, locale: str)-> bool: """ check if the string is postal code or not """ country_data = all_country[locale] @@ -352,7 +344,10 @@ def isPort(self, value: int) -> bool: def isSlug(self, value: str) -> bool: """ check if the string is slug or not """ - pass + regex = "^[a-z0-9-]+$" + if re.match(regex, value): + return True + return False def isStrongPassword(self, value: str) -> bool: """ check if the string is strong password or not diff --git a/tests/string_case_test.py b/tests/string_case_test.py index 1786251..3204311 100644 --- a/tests/string_case_test.py +++ b/tests/string_case_test.py @@ -58,6 +58,15 @@ def test_isEmpty_false(self): self.assertFalse(validator.isEmpty('foo')) self.assertFalse(validator.isEmpty(' foo ')) + def test_isSlug_true(self): + self.assertTrue(validator.isSlug('foo-bar')) + self.assertTrue(validator.isSlug('foo-bar-123')) + self.assertTrue(validator.isSlug('foo-bar-123-456')) + + def test_isSlug_false(self): + self.assertFalse(validator.isSlug('foo bar')) + + if __name__ == '__main__': unittest.main()