From cd171230326bc38cd5f80e2e2bd099447b504664 Mon Sep 17 00:00:00 2001 From: gabino Date: Wed, 26 Feb 2025 11:42:27 -0600 Subject: [PATCH 1/5] Add RFC support to UserListsRequest validation --- cuenca_validations/types/requests.py | 10 ++++++++-- cuenca_validations/version.py | 2 +- tests/test_types.py | 24 +++++++++++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/cuenca_validations/types/requests.py b/cuenca_validations/types/requests.py index 7141876c..72d871df 100644 --- a/cuenca_validations/types/requests.py +++ b/cuenca_validations/types/requests.py @@ -631,6 +631,7 @@ class BankAccountValidationRequest(BaseModel): class UserListsRequest(BaseModel): curp: Optional[Curp] = Field(None, description='Curp to review on lists') + rfc: Optional[Rfc] = Field(None, description='Rfc to review on lists') account_number: Optional[Union[Clabe, PaymentCardNumber]] = Field( None, description='Account to review on lists' ) @@ -648,8 +649,12 @@ class UserListsRequest(BaseModel): @classmethod def check_request(cls, values): has_name = all(values.get(f) for f in ['names', 'first_surname']) - curp, account = values.get('curp'), values.get('account_number') - if not any([curp, account, has_name]): + curp, account, rfc = ( + values.get('curp'), + values.get('account_number'), + values.get('rfc'), + ) + if not any([curp, account, rfc, has_name]): raise ValueError("At least 1 param is required") return values @@ -658,6 +663,7 @@ def check_request(cls, values): json_schema_extra={ 'example': { 'curp': 'GOCG650418HVZNML08', + 'rfc': 'GOCG650418TJ1', 'account_number': '9203929392939292392', 'names': 'Pedrito', 'first_surname': 'Sola', diff --git a/cuenca_validations/version.py b/cuenca_validations/version.py index f8115612..1962abcd 100644 --- a/cuenca_validations/version.py +++ b/cuenca_validations/version.py @@ -1 +1 @@ -__version__ = '2.1.2' +__version__ = '2.1.3.dev1' diff --git a/tests/test_types.py b/tests/test_types.py index 64d6f36a..577adf1c 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -564,10 +564,28 @@ def test_bank_account_validation_clabe_request(): assert BankAccountValidationRequest(account_number='646180157098510917') -def test_user_lists_request(): - UserListsRequest(names='Pedro', first_surname='Paramo') +@pytest.mark.parametrize( + 'input_data', + [ + {'names': 'Pedro', 'first_surname': 'Paramo'}, + {'curp': 'GOCG650418HVZNML08'}, + {'rfc': 'GOCG650418TJ1'}, + {'account_number': '646180157034181180'}, + { + 'curp': 'GOCG650418HVZNML08', + 'rfc': 'GOCG650418TJ1', + 'names': 'Pedro', + 'first_surname': 'Paramo', + }, + ], +) +def test_user_lists_request_valid_params(input_data): + UserListsRequest(**input_data) + + +def test_user_lists_request_invalid_params(): with pytest.raises(ValueError): - UserListsRequest() + UserListsRequest(first_surname='Paramo', second_surname='Paramo') class IntModel(BaseModel): From 1510efbb71bb0161035352f34cd0a684358e3a7d Mon Sep 17 00:00:00 2001 From: gabino Date: Wed, 26 Feb 2025 12:01:14 -0600 Subject: [PATCH 2/5] Release version 2.1.3 --- cuenca_validations/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cuenca_validations/version.py b/cuenca_validations/version.py index 1962abcd..2d31b1c3 100644 --- a/cuenca_validations/version.py +++ b/cuenca_validations/version.py @@ -1 +1 @@ -__version__ = '2.1.3.dev1' +__version__ = '2.1.3' From f143329b677cc91a4123fb63ae8f28af254fe679 Mon Sep 17 00:00:00 2001 From: gabino Date: Mon, 3 Mar 2025 11:51:03 -0600 Subject: [PATCH 3/5] Add validation for names and surnames in UserListsRequest --- cuenca_validations/types/requests.py | 13 ++++++++ tests/test_types.py | 44 ++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/cuenca_validations/types/requests.py b/cuenca_validations/types/requests.py index 72d871df..e081f663 100644 --- a/cuenca_validations/types/requests.py +++ b/cuenca_validations/types/requests.py @@ -648,6 +648,19 @@ class UserListsRequest(BaseModel): @model_validator(mode='before') @classmethod def check_request(cls, values): + if ( + values.get('first_surname') or values.get('second_surname') + ) and not values.get('names'): + raise ValueError( + 'names is required when first_surname or second_surname ' + 'is provided' + ) + + if values.get('names') and not values.get('first_surname'): + raise ValueError( + 'first_surname is required when names is provided' + ) + has_name = all(values.get(f) for f in ['names', 'first_surname']) curp, account, rfc = ( values.get('curp'), diff --git a/tests/test_types.py b/tests/test_types.py index 577adf1c..37922f44 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -583,9 +583,47 @@ def test_user_lists_request_valid_params(input_data): UserListsRequest(**input_data) -def test_user_lists_request_invalid_params(): - with pytest.raises(ValueError): - UserListsRequest(first_surname='Paramo', second_surname='Paramo') +@pytest.mark.parametrize( + 'input_data,expected_error', + [ + ( + {'first_surname': 'Paramo'}, + ( + 'names is required when first_surname or second_surname ' + 'is provided' + ), + ), + ( + {'second_surname': 'Paramo'}, + ( + 'names is required when first_surname or second_surname ' + 'is provided' + ), + ), + ( + {'first_surname': 'Paramo', 'second_surname': 'Paramo'}, + ( + 'names is required when first_surname or second_surname ' + 'is provided' + ), + ), + ( + {'names': 'Juan'}, + 'first_surname is required when names is provided', + ), + ( + {'first_surname': 'Paramo', 'curp': 'GOCG650418HVZNML08'}, + ( + 'names is required when first_surname or second_surname ' + 'is provided' + ), + ), + ({}, 'At least 1 param is required'), + ], +) +def test_user_lists_request_invalid_params(input_data, expected_error): + with pytest.raises(ValueError, match=expected_error): + UserListsRequest(**input_data) class IntModel(BaseModel): From 8a17f19abae33f938278073fabb92e26f2760be8 Mon Sep 17 00:00:00 2001 From: gabino Date: Mon, 3 Mar 2025 12:17:04 -0600 Subject: [PATCH 4/5] Add string constraints to UserListsRequest names and surnames --- cuenca_validations/types/requests.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cuenca_validations/types/requests.py b/cuenca_validations/types/requests.py index e081f663..1fd36026 100644 --- a/cuenca_validations/types/requests.py +++ b/cuenca_validations/types/requests.py @@ -635,13 +635,15 @@ class UserListsRequest(BaseModel): account_number: Optional[Union[Clabe, PaymentCardNumber]] = Field( None, description='Account to review on lists' ) - names: Optional[str] = Field( - None, description='Names of the user to review on lists' - ) - first_surname: Optional[str] = Field( - None, description='first_surname of the user to review on lists' - ) - second_surname: Optional[str] = Field( + names: Optional[ + Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)] + ] = Field(None, description='Names of the user to review on lists') + first_surname: Optional[ + Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)] + ] = Field(None, description='first_surname of the user to review on lists') + second_surname: Optional[ + Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)] + ] = Field( None, description='second_surname of the user to review on lists' ) From 8073a48f6bc86703c3c1691e6303eb6b35f7ae02 Mon Sep 17 00:00:00 2001 From: gabino Date: Mon, 3 Mar 2025 14:00:33 -0600 Subject: [PATCH 5/5] Refactor UserListsRequest with NonEmptyStr type --- cuenca_validations/types/general.py | 4 ++++ cuenca_validations/types/requests.py | 19 +++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cuenca_validations/types/general.py b/cuenca_validations/types/general.py index 17b1f1e8..aff7af58 100644 --- a/cuenca_validations/types/general.py +++ b/cuenca_validations/types/general.py @@ -22,6 +22,10 @@ IPvAnyAddress, PlainSerializer(str, return_type=str) ] +NonEmptyStr = Annotated[ + str, StringConstraints(strip_whitespace=True, min_length=1) +] + class SantizedDict(dict): def __init__(self, *args, **kwargs): diff --git a/cuenca_validations/types/requests.py b/cuenca_validations/types/requests.py index 1fd36026..f1638c5b 100644 --- a/cuenca_validations/types/requests.py +++ b/cuenca_validations/types/requests.py @@ -55,6 +55,7 @@ ) from .general import ( LogConfig, + NonEmptyStr, SerializableAnyUrl, SerializableHttpUrl, SerializableIPvAnyAddress, @@ -635,16 +636,14 @@ class UserListsRequest(BaseModel): account_number: Optional[Union[Clabe, PaymentCardNumber]] = Field( None, description='Account to review on lists' ) - names: Optional[ - Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)] - ] = Field(None, description='Names of the user to review on lists') - first_surname: Optional[ - Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)] - ] = Field(None, description='first_surname of the user to review on lists') - second_surname: Optional[ - Annotated[str, StringConstraints(min_length=1, strip_whitespace=True)] - ] = Field( - None, description='second_surname of the user to review on lists' + names: Optional[NonEmptyStr] = Field( + None, description='Names of the user to review on lists' + ) + first_surname: Optional[NonEmptyStr] = Field( + None, description='First surname of the user to review on lists' + ) + second_surname: Optional[NonEmptyStr] = Field( + None, description='Second surname of the user to review on lists' ) @model_validator(mode='before')