Skip to content

Commit

Permalink
feat: Add ean_number validation to array_validator
Browse files Browse the repository at this point in the history
  • Loading branch information
codeperfectplus committed Jun 2, 2024
1 parent f9001ea commit 13449dc
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 51 deletions.
11 changes: 0 additions & 11 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,16 @@ build:
os: ubuntu-22.04
tools:
python: "3.12"
# You can also specify other tool versions:
# nodejs: "20"
# rust: "1.70"
# golang: "1.20"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/conf.py
# You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs
# builder: "dirhtml"
# Fail on all warnings to avoid broken references
# fail_on_warning: true

# Optionally build your docs in additional formats such as PDF and ePub
formats:
- pdf
- epub

# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: docs/requirements.txt
15 changes: 14 additions & 1 deletion docs/research/ean_number.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ How to Validate an EAN Code

1. EAN numbers are 13 digits long! If the number is less than 13 digits, it's not a valid EAN code.
2. Add the odd-numbered digits together.
3. multiply the even numbers by 3 and add them together.
3. multiply the odd numbers by 3 and add the even numbers together.
4. divide the sum by 10. If the remainder is 0, the check digit is 0. Otherwise, subtract the remainder from 10 to get the check digit.
5. If the check digit is the same as the last digit of the EAN number, the EAN number is valid.

Example
-------

Ean Number: 8901030679216

1. The last digit is the check digit, so we ignore it for now.
2. Add the odd-numbered digits together: 8 + 0 + 0 + 0 + 7 + 2 = 17
3. add the even numbers together: 9 + 1+ 3+ 6 + 9 + 1 = 29
4. even numbers * 3 + odd numbers = 29 * 3 + 17 = 104
5. 104 % 10 = 4
6. 10 - 4 = 6
7. 6 == 6, so the EAN number is valid.
3 changes: 2 additions & 1 deletion docs/research/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Research for the project
license_plate.rst
mobile_number.rst
pan_card.rst
passport.rst
passport.rst
ean_number.rst
12 changes: 9 additions & 3 deletions sanatio/array_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ class ArrayValidator:
def __init__(self) -> None:
pass

def isArray(self, value) -> bool: # TODO: test cases is pending
def isArray(self, value) -> bool:
""" check if the string is array or not """
if isinstance(value, list):
return True
return False

def isLength(self, value, min: int=0, max: int=None) -> bool: # TODO: test cases is pending
def isLength(self, value, min: int=0, max: int=None) -> bool:
""" check if the array length is between min and max """
if min <= len(value) <= max:
return True
return False

def isContains(self, value, element) -> bool: # TODO: test cases is pending
def isContains(self, value, element) -> bool:
""" check if the array contains the element or not """
if element in value:
return True
return False

def isUnique(self, value) -> bool:
""" check if the array contains unique elements or not """
if len(value) == len(set(value)):
return True
return False
40 changes: 15 additions & 25 deletions sanatio/password_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PasswordValidator:
def __init__(self):
super().__init__()

def isStrongPassword(self, value: str) -> bool:
def isStrongPassword(self, value: str, min_length: int=8, special_chars: bool= True, numbers: bool= True, uppercase: bool= True, lowercase: bool= True) -> bool:
""" check if the string is strong password or not
requirements:
Expand All @@ -14,52 +14,42 @@ def isStrongPassword(self, value: str) -> bool:
4. At least one number
5. At least one special character
"""
if len(value) < 8:
if not self.isPasswordLength(value, min_length) or \
(special_chars and not re.search("[_@$]", value)) or \
(numbers and not re.search("[0-9]", value)) or \
(uppercase and not re.search("[A-Z]", value)) or \
(lowercase and not re.search("[a-z]", value)):
return False

if not re.search("[a-z]", value):
return False

if not re.search("[A-Z]", value):
return False

if not re.search("[0-9]", value):
return False

if not re.search("[_@$]", value):
return False


return True

def isPasswordLength(self, value: str, min_length: int, max_length: int) -> bool:
def isPasswordLength(self, value: str, min_length: int) -> bool:
""" check if the string length is between min and max """
if min_length <= len(value) <= max_length:
if len(value) >= min_length:
return True

return False

def isPasswordMatch(self, value: str, match_value: str) -> bool:
def isPasswordMatch(self, value: str, match_value: str, ignore_case: bool=False) -> bool:
""" check if the string matches the match_value """
if value == match_value:
return True

return False

def isPasswordNotMatch(self, value: str, match_value: str) -> bool:
def isPasswordNotMatch(self, value: str, match_value: str, ignore_case: bool=False) -> bool:
""" check if the string does not match the match_value """
if value != match_value:
return True

return False

def isPasswordNotInList(self, value: str, list_values: list) -> bool:
def isPasswordNotInList(self, value: str, list_values: list, ignore_case: bool=False) -> bool:
""" check if the string is not in the list """
if value not in list_values:
return True

return False

def isPasswordNotInFile(self, value: str, file_path: str) -> bool:
def isPasswordNotInFile(self, value: str, file_path: str, ignore_case: bool=False) -> bool:
""" check if the string is not in the file """
with open(file_path, 'r') as f:
data = f.read().splitlines()
Expand All @@ -69,7 +59,7 @@ def isPasswordNotInFile(self, value: str, file_path: str) -> bool:

return False

def isPasswordNotInDict(self, value: str, dict_values: dict) -> bool:
def isPasswordNotInDict(self, value: str, dict_values: dict, ignore_case: bool=False) -> bool:
""" check if the string is not in the dictionary """
if value not in dict_values:
return True
Expand Down
2 changes: 1 addition & 1 deletion sanatio/string_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def edit_distance(self, value1, value2):

return difference

def equals(self, value1: str, value2: str, ignoreCase: bool=False)-> bool:
def is_equals(self, value1: str, value2: str, ignoreCase: bool=False)-> bool:
""" Check if the two string are equal or not """
if not self.isvalidString(value1) or not self.isvalidString(value2):
return False
Expand Down
12 changes: 11 additions & 1 deletion tests/array_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ def test_isContains_False(self):
self.assertFalse(validator.isContains([1, 2, 3], 4))
self.assertFalse(validator.isContains([1, 2, 3], 5))
self.assertFalse(validator.isContains([1, 2, 3], 6))

def test_isUnique_True(self):
self.assertTrue(validator.isUnique([1, 2, 3]))
self.assertTrue(validator.isUnique(['a', 'b', 'c']))
self.assertTrue(validator.isUnique([1, 'a', 3]))

def test_isUnique_False(self):
self.assertFalse(validator.isUnique([1, 2, 2]))
self.assertFalse(validator.isUnique(['a', 'b', 'b']))
self.assertFalse(validator.isUnique([1, 'a', 3, 3]))


if __name__ == '__main__':
unittest.main()
unittest.main()
17 changes: 14 additions & 3 deletions tests/ean_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@

import sys
import random
import unittest
sys.path.append('.')
from sanatio import Validator
validator = Validator()

def generate_ean13_number():
""" generate ean13 number """
ean_number = ''.join(random.choices('0123456789', k=12))
total_sum = sum(int(ean_number[i]) * 3 if i % 2 != 0 else \
int(ean_number[i]) for i in range(len(ean_number)))
unit_digit = 0 if total_sum % 10 == 0 else 10 - (total_sum % 10)
return ean_number + str(unit_digit)

class EANTest(unittest.TestCase):
def test_isEAN_true(self):
self.assertTrue(validator.isEAN13('8901030679216'))
self.assertTrue(validator.isEAN13('0067238891190'))
self.assertTrue(validator.isEAN13('6921815600015'))
self.assertTrue(validator.isEAN13(generate_ean13_number()))
self.assertTrue(validator.isEAN13(generate_ean13_number()))
self.assertTrue(validator.isEAN13(generate_ean13_number()))
self.assertTrue(validator.isEAN13(generate_ean13_number()))
self.assertTrue(validator.isEAN13(generate_ean13_number()))
self.assertTrue(validator.isEAN13(generate_ean13_number()))

def test_isEAN_false(self):
self.assertFalse(validator.isEAN13('890103067921'))
Expand Down
9 changes: 9 additions & 0 deletions tests/password_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ def test_strong_password_true(self):
self.assertTrue(validator.isStrongPassword('Bar@1234'))
self.assertTrue(validator.isStrongPassword('FooBar@1234'))
self.assertTrue(validator.isStrongPassword('FooBar@1234'))
self.assertTrue(validator.isStrongPassword('FooBar1234', special_chars=False))
self.assertTrue(validator.isStrongPassword('foobar@1234', uppercase=False))
self.assertTrue(validator.isStrongPassword('1234', min_length=4,
uppercase=False,
lowercase=False,
special_chars=False))


def test_strong_password_false(self):
self.assertFalse(validator.isStrongPassword('foo'))
self.assertFalse(validator.isStrongPassword('foo123'))
self.assertFalse(validator.isStrongPassword('foo@123'))
self.assertFalse(validator.isStrongPassword('FOO@123'))
self.assertFalse(validator.isStrongPassword('Foo@1234', min_length=13))



if __name__ == '__main__':
Expand Down
10 changes: 5 additions & 5 deletions tests/string_case_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class ValidatorTest(unittest.TestCase):
""" """

def test_equals_true(self):
self.assertTrue(validator.equals('foo', 'foo'))
self.assertTrue(validator.equals('foo', 'Foo', ignoreCase=True))
self.assertTrue(validator.is_equals('foo', 'foo'))
self.assertTrue(validator.is_equals('foo', 'Foo', ignoreCase=True))

def test_equals_false(self):
self.assertFalse(validator.equals('foo', 'bar'))
self.assertFalse(validator.equals('foo', 'Foo'))
self.assertFalse(validator.equals('foo', 'Foo', ignoreCase=False))
self.assertFalse(validator.is_equals('foo', 'bar'))
self.assertFalse(validator.is_equals('foo', 'Foo'))
self.assertFalse(validator.is_equals('foo', 'Foo', ignoreCase=False))

def test_length_true(self):
self.assertTrue(validator.isLength('foo', 1, 3))
Expand Down

0 comments on commit 13449dc

Please sign in to comment.