def validate_select_field_data( field_data: ProfileFieldData) -> Dict[str, Dict[str, str]]: """ This function is used to validate the data sent to the server while creating/editing choices of the choice field in Organization settings. """ validator = check_dict_only([ ("text", check_required_string), ("order", check_required_string), ]) # To create an array of texts of each option distinct_field_names: Set[str] = set() for key, value in field_data.items(): if not key.strip(): raise ValidationError( _("'{item}' cannot be blank.").format(item="value")) valid_value = validator("field_data", value) assert value is valid_value # To justify the unchecked cast below distinct_field_names.add(valid_value["text"]) # To show error if the options are duplicate if len(field_data) != len(distinct_field_names): raise ValidationError(_("Field must not have duplicate choices.")) return cast(Dict[str, Dict[str, str]], field_data)
def validate_external_account_field_data(field_data: ProfileFieldData) -> ProfileFieldData: field_validator = check_dict_only( [("subtype", check_required_string)], [("url_pattern", check_external_account_url_pattern)], ) field_validator("field_data", field_data) field_subtype = field_data.get("subtype") if field_subtype not in DEFAULT_EXTERNAL_ACCOUNTS.keys(): if field_subtype == "custom": if "url_pattern" not in field_data.keys(): raise ValidationError(_("Custom external account must define URL pattern")) else: raise ValidationError(_("Invalid external account type")) return field_data
def validate_external_account_field_data(field_data: ProfileFieldData) -> Optional[str]: field_validator = check_dict_only( [('subtype', check_required_string)], [('url_pattern', check_external_account_url_pattern)], ) error = field_validator('field_data', field_data) if error: return error field_subtype = field_data.get('subtype') if field_subtype not in DEFAULT_EXTERNAL_ACCOUNTS.keys(): if field_subtype == "custom": if 'url_pattern' not in field_data.keys(): return _("Custom external account must define url pattern") else: return _("Invalid external account type") return None
def validate_choice_field_data(field_data: ProfileFieldData) -> Dict[str, Dict[str, str]]: """ This function is used to validate the data sent to the server while creating/editing choices of the choice field in Organization settings. """ validator = check_dict_only([ ('text', check_required_string), ('order', check_required_string), ]) for key, value in field_data.items(): if not key.strip(): raise ValidationError(_("'{item}' cannot be blank.").format(item='value')) valid_value = validator('field_data', value) assert value is valid_value # To justify the unchecked cast below return cast(Dict[str, Dict[str, str]], field_data)
def validate_choice_field_data(field_data: ProfileFieldData) -> Optional[str]: """ This function is used to validate the data sent to the server while creating/editing choices of the choice field in Organization settings. """ validator = check_dict_only([ ('text', check_required_string), ('order', check_required_string), ]) for key, value in field_data.items(): if not key.strip(): return _("'{item}' cannot be blank.").format(item='value') error = validator('field_data', value) if error: return error return None
def validate_field_data(field_data: ProfileFieldData) -> Optional[str]: """ This function is used to validate the data sent to the server while creating/editing choices of the choice field in Organization settings. """ validator = check_dict_only([ ('text', check_required_string), ('order', check_required_string), ]) for key, value in field_data.items(): if not key.strip(): return _("'{item}' cannot be blank.").format(item='value') error = validator('field_data', value) if error: return error return None