Skip to content

Commit d35914c

Browse files
Add support for any type in value field instead of only string
1 parent acf2184 commit d35914c

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [unreleased]
1010

11+
## [0.24.3] - 2024-09-24
12+
13+
- Adds support for form field related improvements by making fields accept any type of values
14+
- Adds support for optional fields to properly optional
15+
1116
## [0.24.2] - 2024-09-03
1217
- Makes optional input form fields truly optional instead of just being able to accept `""`.
1318

supertokens_python/recipe/emailpassword/api/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ async def validate_form_or_throw_error(
4141
input_field: Union[None, FormField] = find_first_occurrence_in_list(
4242
lambda x: x.id == field.id, inputs
4343
)
44-
is_invalid_value = input_field is None or input_field.value == ""
44+
is_invalid_value = input_field is None or (
45+
isinstance(input_field.value, str) and input_field.value == ""
46+
)
4547
if not field.optional and is_invalid_value:
4648
validation_errors.append(ErrorFormField(field.id, "Field is not optional"))
4749
continue

supertokens_python/recipe/emailpassword/types.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
from __future__ import annotations
15-
from typing import Awaitable, Callable, List, TypeVar, Union
15+
16+
from typing import Any, Awaitable, Callable, List, TypeVar, Union
1617

1718
from supertokens_python.ingredients.emaildelivery import EmailDeliveryIngredient
1819
from supertokens_python.ingredients.emaildelivery.types import (
@@ -53,9 +54,9 @@ def __init__(self, id: str, error: str): # pylint: disable=redefined-builtin
5354

5455

5556
class FormField:
56-
def __init__(self, id: str, value: str): # pylint: disable=redefined-builtin
57+
def __init__(self, id: str, value: Any): # pylint: disable=redefined-builtin
5758
self.id: str = id
58-
self.value: str = value
59+
self.value: Any = value
5960

6061

6162
class InputFormField:

supertokens_python/recipe/emailpassword/utils.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
from __future__ import annotations
1515

1616
from re import fullmatch
17-
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Union, Dict
18-
from supertokens_python.framework import BaseRequest
17+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
1918

19+
from supertokens_python.framework import BaseRequest
2020
from supertokens_python.ingredients.emaildelivery.types import (
2121
EmailDeliveryConfig,
2222
EmailDeliveryConfigWithService,
@@ -26,17 +26,14 @@
2626
)
2727

2828
from .interfaces import APIInterface, RecipeInterface
29-
from .types import InputFormField, NormalisedFormField, EmailTemplateVars
29+
from .types import EmailTemplateVars, InputFormField, NormalisedFormField
3030

3131
if TYPE_CHECKING:
3232
from supertokens_python.supertokens import AppInfo
3333

3434
from supertokens_python.utils import get_filtered_list
3535

36-
from .constants import (
37-
FORM_FIELD_EMAIL_ID,
38-
FORM_FIELD_PASSWORD_ID,
39-
)
36+
from .constants import FORM_FIELD_EMAIL_ID, FORM_FIELD_PASSWORD_ID
4037

4138

4239
async def default_validator(_: str, __: str) -> Union[str, None]:
@@ -261,10 +258,14 @@ def validate_and_normalise_user_input(
261258
email_delivery: Union[EmailDeliveryConfig[EmailTemplateVars], None] = None,
262259
) -> EmailPasswordConfig:
263260

264-
if sign_up_feature is not None and not isinstance(sign_up_feature, InputSignUpFeature): # type: ignore
261+
# type: ignore
262+
if sign_up_feature is not None and not isinstance(
263+
sign_up_feature, InputSignUpFeature
264+
):
265265
raise ValueError("sign_up_feature must be of type InputSignUpFeature or None")
266266

267-
if override is not None and not isinstance(override, InputOverrideConfig): # type: ignore
267+
# type: ignore
268+
if override is not None and not isinstance(override, InputOverrideConfig):
268269
raise ValueError("override must be of type InputOverrideConfig or None")
269270

270271
if override is None:

tests/emailpassword/test_signin.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,48 @@ async def test_optional_custom_field_without_input(driver_config_client: TestCli
719719
assert dict_response["status"] == "OK"
720720

721721

722+
@mark.asyncio
723+
async def test_non_optional_custom_field_with_boolean_value(
724+
driver_config_client: TestClient,
725+
):
726+
init(
727+
supertokens_config=SupertokensConfig("http://localhost:3567"),
728+
app_info=InputAppInfo(
729+
app_name="SuperTokens Demo",
730+
api_domain="http://api.supertokens.io",
731+
website_domain="http://supertokens.io",
732+
api_base_path="/auth",
733+
),
734+
framework="fastapi",
735+
recipe_list=[
736+
emailpassword.init(
737+
sign_up_feature=emailpassword.InputSignUpFeature(
738+
form_fields=[
739+
emailpassword.InputFormField("autoVerify", optional=False)
740+
]
741+
)
742+
),
743+
session.init(get_token_transfer_method=lambda _, __, ___: "cookie"),
744+
],
745+
)
746+
start_st()
747+
748+
response_1 = driver_config_client.post(
749+
url="/auth/signup",
750+
headers={"Content-Type": "application/json"},
751+
json={
752+
"formFields": [
753+
{"id": "email", "value": "[email protected]"},
754+
{"id": "password", "value": "validpassword123"},
755+
{"id": "autoVerify", "value": False},
756+
]
757+
},
758+
)
759+
assert response_1.status_code == 200
760+
dict_response = json.loads(response_1.text)
761+
assert dict_response["status"] == "OK"
762+
763+
722764
@mark.asyncio
723765
async def test_too_many_fields(driver_config_client: TestClient):
724766
init(

0 commit comments

Comments
 (0)