Skip to content

Commit

Permalink
Merge pull request #130 from TImoteoVieira/129-use-getpattern
Browse files Browse the repository at this point in the history
Resolvendo TODO em Module Flunt Regex Patterns
  • Loading branch information
fazedordecodigo authored Jan 12, 2025
2 parents ed31032 + 2b9738b commit b0e2137
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.venv/
venv
.pytest_cache/
build/
dist/
Expand All @@ -15,4 +16,4 @@ appsettings.json
.ruff_cache
.tool-versions
.tox
coverage.xml
coverage.xml
11 changes: 10 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ e este projeto adere ao [Versionamento Semântico](https://semver.org/lang/pt-BR
- Adicionar validação de Objetos [#31](https://github.com/fazedordecodigo/PyFlunt/issues/31)
- Adicionar validação de Regex [#32](https://github.com/fazedordecodigo/PyFlunt/issues/32)
- Adicionar validação de URL [#33](https://github.com/fazedordecodigo/PyFlunt/issues/33)
- Adicionar Result Pattner [#61](https://github.com/fazedordecodigo/PyFlunt/issues/61)
- Adicionar Result Pattern [#61](https://github.com/fazedordecodigo/PyFlunt/issues/61)
- Refatorar FluntRegexPatterns [#129](https://github.com/fazedordecodigo/PyFlux/issues/129)

## [2.3.1] - 2024-12-16
### Adicionado
- Refatoração para `get_pattern` em `CreditCardValidationContract` e `EmailValidationContract`.

### Corrigido
- Garantindo que os padrões regex retornem dados válidos para validações.
- Refatorar FluntRegexPatterns [#129](https://github.com/fazedordecodigo/PyFlux/issues/129)

## [2.3.0] - 2024-03-17
### Adicionado
Expand Down
9 changes: 9 additions & 0 deletions docs/CHANGELOG_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/pt-BR/
- Add Objects validation [#31](https://github.com/fazedordecodigo/PyFlunt/issues/31)
- Add Regex validation [#32](https://github.com/fazedordecodigo/PyFlunt/issues/32)
- Add URL validation [#33](https://github.com/fazedordecodigo/PyFlunt/issues/33)
- Refactor `FluntRegexPatterns` [#129](https://github.com/fazedordecodigo/PyFlunt/issues/129)

## [2.3.1] - 2024-12-16
### Added
- Refactor to use `get_pattern` in `CreditCardValidationContract` and `EmailValidationContract`.

### Fixed
- Ensure regex patterns return valid data for validations.
- Refactor `FluntRegexPatterns` [#129](https://github.com/fazedordecodigo/PyFlunt/issues/129)

## [2.2.0] - 2024-03-13
### Added
Expand Down
62 changes: 14 additions & 48 deletions flunt/localization/flunt_regex_patterns.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
"""Module Flunt Regex Patterns."""

REGEX_EMAIL = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
REGEX_PASSPORT = r"^(?!^0+$)[a-zA-Z0-9]{3,20}$"
REGEX_ONLY_NUMBERS = r"^\d+$"
REGEX_ONLY_LETTERS_AND_NUMBERS = r"[A-Za-z0-9_-]"
REGEX_URL = r"^(http|https):(\/\/www\.|\/\/www\.|\/\/|\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$|(http|https):(\/\/localhost:\d*|\/\/127\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))(:[0-9]{1,5})?(\/.*)?$"
REGEX_CPF = r"^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$"
REGEX_CNPJ = r"^\d{2}\.?\d{3}\.?\d{3}/?\d{4}-?\d{2}$"
REGEX_PATTERNS = {
"email": r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",
"passport": r"^(?!^0+$)[a-zA-Z0-9]{3,20}$",
"only_numbers": r"^\d+$",
"only_letters_and_numbers": r"[A-Za-z0-9_-]",
"url": r"^(http|https):(\/\/www\.|\/\/www\.|\/\/|\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$"
r"|(http|https):(\/\/localhost:\d*|\/\/127\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]"
r"|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))(:[0-9]{1,5})?(\/.*)?$",
"cpf": r"^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$",
"cnpj": r"^\d{2}\.?\d{3}\.?\d{3}/?\d{4}-?\d{2}$",
}


class FluntRegexPatterns:
"""
FluntRegexPatterns.
This class encapsulates commonly used regular expression patterns.
It provides attributes to access the regular expression patterns
related to CPF, CNPJ, email, URL, only numbers, only letters and numbers,
and passport.
Attributes
----------
- cpf_regex_pattern: str
The regular expression pattern for CPF.
- cnpj_regex_pattern: str
The regular expression pattern for CNPJ.
- email_regex_pattern: str
The regular expression pattern for email.
- url_regex_pattern: str
The regular expression pattern for URL.
- only_number_regex_pattern: str
The regular expression pattern for only numbers.
- only_letters_and_numbers_regex_pattern: str
The regular expression pattern for only letters and numbers.
- passport_regex_pattern: str
The regular expression pattern for passport.
TODO: Alterar de classe para outra estrutura de dados.
"""

def __init__(self) -> None:
"""Initialize a new instance of the FluntRegexPatterns class."""
self.cpf_regex_pattern = REGEX_CPF
self.cnpj_regex_pattern = REGEX_CNPJ
self.email_regex_pattern = REGEX_EMAIL
self.url_regex_pattern = REGEX_URL
self.only_number_regex_pattern = REGEX_ONLY_NUMBERS
self.only_letters_and_numbers_regex_pattern = (
REGEX_ONLY_LETTERS_AND_NUMBERS
)
self.passport_regex_pattern = REGEX_PASSPORT
def get_pattern(name: str) -> str | None:
"""Retrieve a regex pattern by its name."""
return REGEX_PATTERNS.get(name)
6 changes: 4 additions & 2 deletions flunt/validations/credit_card_validation_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing_extensions import Self

from flunt.constants.messages import IS_NOT_CREDIT_CARD
from flunt.localization.flunt_regex_patterns import FluntRegexPatterns
from flunt.localization.flunt_regex_patterns import get_pattern
from flunt.notifications.notifiable import Notifiable


Expand Down Expand Up @@ -58,8 +58,10 @@ def is_credit_card(
```
"""
only_number_pattern = get_pattern("only_numbers")

if not re.match(
FluntRegexPatterns().only_number_regex_pattern,
only_number_pattern,
value,
re.IGNORECASE,
):
Expand Down
6 changes: 4 additions & 2 deletions flunt/validations/email_validation_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing_extensions import Self

from flunt.constants.messages import IS_EMAIL, IS_NOT_EMAIL
from flunt.localization.flunt_regex_patterns import FluntRegexPatterns
from flunt.localization.flunt_regex_patterns import get_pattern
from flunt.notifications.notifiable import Notifiable


Expand All @@ -24,8 +24,10 @@ def _valid_email(value) -> Union[re.Match[str], None]:
`(Match[str] | None)`
"""
email_pattern = get_pattern("email")

return re.match(
FluntRegexPatterns().email_regex_pattern,
email_pattern,
value,
re.IGNORECASE,
)
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from flunt.localization.flunt_regex_patterns import FluntRegexPatterns
from flunt.localization.flunt_regex_patterns import get_pattern
from tests.mocks.entity.sample_entity import SampleEntity


Expand All @@ -11,6 +11,6 @@ def entity_mock() -> SampleEntity:


@pytest.fixture(scope="module")
def regex() -> FluntRegexPatterns:
"""Fixture to return a FluntRegexPatterns instance."""
return FluntRegexPatterns()
def regex() -> get_pattern:
"""Fixture to return a get_pattern instance."""
return get_pattern()
41 changes: 24 additions & 17 deletions tests/localization/test_flunt_regex_patterns.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import re

import pytest
from flunt.localization.flunt_regex_patterns import get_pattern

from faker import Faker

fake = Faker("pt_BR")


@pytest.mark.parametrize(
("value", "expect"),
[
Expand All @@ -14,8 +14,9 @@
(fake.name(), False),
],
)
def test_should_identify_a_valid_email_address(regex, value, expect):
result = re.match(regex.email_regex_pattern, value)
def test_should_identify_a_valid_email_address(value, expect):
regex = get_pattern("email")
result = re.match(regex, value)
assert isinstance(result, re.Match) is expect


Expand All @@ -28,8 +29,9 @@ def test_should_identify_a_valid_email_address(regex, value, expect):
(fake.cnpj(), False),
],
)
def test_should_identify_a_valid_cpf(regex, value, expect):
result = re.match(regex.cpf_regex_pattern, value)
def test_should_identify_a_valid_cpf(value, expect):
regex = get_pattern("cpf")
result = re.match(regex, value)
assert isinstance(result, re.Match) is expect


Expand All @@ -42,8 +44,9 @@ def test_should_identify_a_valid_cpf(regex, value, expect):
(fake.cpf(), False),
],
)
def test_should_identify_a_valid_cnpj(regex, value, expect):
result = re.match(regex.cnpj_regex_pattern, value)
def test_should_identify_a_valid_cnpj(value, expect):
regex = get_pattern("cnpj")
result = re.match(regex, value)
assert isinstance(result, re.Match) is expect


Expand All @@ -55,8 +58,9 @@ def test_should_identify_a_valid_cnpj(regex, value, expect):
(fake.name(), False),
],
)
def test_should_identify_a_valid_url(regex, value, expect):
result = re.match(regex.url_regex_pattern, value)
def test_should_identify_a_valid_url(value, expect):
regex = get_pattern("url")
result = re.match(regex, value)
assert isinstance(result, re.Match) is expect


Expand All @@ -69,8 +73,9 @@ def test_should_identify_a_valid_url(regex, value, expect):
(fake.ipv4_public(), False),
],
)
def test_should_identify_only_numbers(regex, value, expect):
result = re.match(regex.only_number_regex_pattern, value)
def test_should_identify_only_numbers(value, expect):
regex = get_pattern("only_numbers")
result = re.match(regex, value)
assert isinstance(result, re.Match) is expect


Expand All @@ -83,8 +88,9 @@ def test_should_identify_only_numbers(regex, value, expect):
("", False),
],
)
def test_should_identify_letters_and_numbers(regex, value, expect):
result = re.match(regex.only_letters_and_numbers_regex_pattern, value)
def test_should_identify_letters_and_numbers(value, expect):
regex = get_pattern("only_letters_and_numbers")
result = re.match(regex, value)
assert isinstance(result, re.Match) is expect


Expand All @@ -95,6 +101,7 @@ def test_should_identify_letters_and_numbers(regex, value, expect):
(fake.name(), False),
],
)
def test_should_identify_a_valid_passport(regex, value, expect):
result = re.match(regex.passport_regex_pattern, value)
assert isinstance(result, re.Match) is expect
def test_should_identify_a_valid_passport(value, expect):
regex = get_pattern("passport")
result = re.match(regex, value)
assert isinstance(result, re.Match) is expect

0 comments on commit b0e2137

Please sign in to comment.