diff --git a/querybook/server/lib/form/__init__.py b/querybook/server/lib/form/__init__.py index 212484feb..b5535784a 100644 --- a/querybook/server/lib/form/__init__.py +++ b/querybook/server/lib/form/__init__.py @@ -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 @@ -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: @@ -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" diff --git a/querybook/tests/test_lib/test_form/test__init__.py b/querybook/tests/test_lib/test_form/test__init__.py index d805df14e..c57ce8263 100644 --- a/querybook/tests/test_lib/test_form/test__init__.py +++ b/querybook/tests/test_lib/test_form/test__init__.py @@ -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(