Add emoji validation to icon field in custom components
This commit is contained in:
parent
f48b4468f6
commit
d8522f8236
2 changed files with 20 additions and 18 deletions
|
|
@ -3,6 +3,7 @@ import operator
|
|||
import warnings
|
||||
from typing import Any, ClassVar, Optional
|
||||
|
||||
import emoji
|
||||
from cachetools import TTLCache, cachedmethod
|
||||
from fastapi import HTTPException
|
||||
from langflow.interface.custom.code_parser import CodeParser
|
||||
|
|
@ -34,6 +35,10 @@ class Component:
|
|||
else:
|
||||
setattr(self, key, value)
|
||||
|
||||
# Validate the emoji at the icon field
|
||||
if self.icon:
|
||||
self.icon = self.validate_icon(self.icon)
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
if key == "_user_id" and hasattr(self, "_user_id"):
|
||||
warnings.warn("user_id is immutable and cannot be changed.")
|
||||
|
|
@ -82,9 +87,21 @@ class Component:
|
|||
template_config["documentation"] = ast.literal_eval(item_value)
|
||||
|
||||
elif "icon" in item_name:
|
||||
template_config["icon"] = ast.literal_eval(item_value)
|
||||
icon_str = ast.literal_eval(item_value)
|
||||
template_config["icon"] = self.validate_icon(icon_str)
|
||||
|
||||
return template_config
|
||||
|
||||
def validate_icon(self, value: str):
|
||||
# we are going to use the emoji library to validate the emoji
|
||||
# emojis can be defined using the :emoji_name: syntax
|
||||
if not value.startswith(":") or not value.endswith(":"):
|
||||
raise ValueError("Invalid emoji. Please use the :emoji_name: syntax.")
|
||||
|
||||
emoji_value = emoji.emojize(value, variant="emoji_type")
|
||||
if value == emoji_value:
|
||||
raise ValueError(f"Invalid emoji. {value} is not a valid emoji.")
|
||||
return emoji_value
|
||||
|
||||
def build(self, *args: Any, **kwargs: Any) -> Any:
|
||||
raise NotImplementedError
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue