Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: minor typo and add comment #1506

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions querybook/server/lib/form/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from multiprocessing.dummy import Array
import re
from abc import ABCMeta, abstractmethod
from typing import Dict, Union
from typing import Dict, Union, List
from enum import Enum


Expand Down Expand Up @@ -41,7 +40,8 @@ def __init__(
# These two only applies to string field
regex: str = None,
hidden: bool = False,
options: Array = None,
# Only applies to Select field type
options: List[str] = None,
):
"""Initialize the form field
Keyword Arguments:
Expand Down Expand Up @@ -166,4 +166,8 @@ def validate_form(form: AllFormField, form_value) -> tuple[bool, str]:
if not isinstance(form_value, bool):
return False, "Field value is not a boolean"
return True, ""
elif form.field_type == FormFieldType.Select:
if form_value not in form.options:
return False, "Field value is not in options"
return True, ""
return False, "Unexpected form type"
16 changes: 16 additions & 0 deletions querybook/tests/test_lib/test_form/test__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ def test_bool_field(self):
validate_form(FormField(field_type=FormFieldType.Boolean), True), (True, "")
)

def test_select_field(self):
self.assertEqual(
validate_form(
FormField(field_type=FormFieldType.Select, options=["foo", "bar"]),
"baz",
),
(False, "Field value is not in options"),
)
self.assertEqual(
validate_form(
FormField(field_type=FormFieldType.Select, options=["foo", "bar"]),
"bar",
),
(True, ""),
)

def test_array_field(self):
form = ExpandableFormField(of=FormField(), min=2, max=4)
self.assertEqual(
Expand Down
Loading