Skip to content

Commit 41d0e81

Browse files
committed
Add 'options_with_unique_values' validator.
1 parent f9db291 commit 41d0e81

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

dash/development/validator.py

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ def _validator_plotly_figure(self, field, value):
2020
field,
2121
"Invalid Plotly Figure:\n\n{}".format(e))
2222

23+
def _validator_options_with_unique_values(self, field, value):
24+
if not isinstance(value, list):
25+
self._error(field, "Invalid options: Not a dict!")
26+
values = set()
27+
for i, option_dict in enumerate(value):
28+
if not isinstance(option_dict, dict):
29+
self._error(
30+
field,
31+
"The option at index {} is not a dictionary!".format(i)
32+
)
33+
if 'value' not in option_dict:
34+
self._error(
35+
field,
36+
"The option at index {} does not have a 'value' key!".format(i)
37+
)
38+
curr = option_dict['value']
39+
if curr in values:
40+
self._error(
41+
field,
42+
("The options list you provided was not valid. "
43+
"More than one of the options has the value {}.".format(curr))
44+
)
45+
values.add(curr)
46+
2347
def _validate_type_list(self, value):
2448
if isinstance(value, list):
2549
return True

0 commit comments

Comments
 (0)