Merge remote-tracking branch 'origin/dev' into zustand/io/migration

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-02-09 09:03:07 -03:00
commit 9cbe329675
6 changed files with 44 additions and 13 deletions

View file

@ -2,6 +2,7 @@ import operator
import warnings
from typing import Any, ClassVar, Optional
import emoji
from cachetools import TTLCache, cachedmethod
from fastapi import HTTPException
@ -35,6 +36,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.")
@ -79,6 +84,7 @@ class Component:
"description": self.getattr_return_str,
"beta": self.getattr_return_str,
"documentation": self.getattr_return_str,
"icon": self.validate_icon,
}
for attribute, func in attributes_func_mapping.items():
@ -87,5 +93,16 @@ class Component:
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