Skip to content

Commit

Permalink
Fix: minor typo and add comment (#1506)
Browse files Browse the repository at this point in the history
* Fix: minor typo and add comment

* Add select form field validation logic and test
  • Loading branch information
czgu authored Nov 4, 2024
1 parent 5363bee commit c304fb1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit c304fb1

Please sign in to comment.