validators.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  2. # █▀█ █ █ █ █▀█ █▀▄ █
  3. # © Copyright 2022
  4. # https://t.me/hikariatama
  5. #
  6. # 🔒 Licensed under the GNU AGPLv3
  7. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  8. import re
  9. import grapheme
  10. import functools
  11. import typing
  12. from emoji import get_emoji_unicode_dict
  13. from . import utils
  14. ConfigAllowedTypes = typing.Union[tuple, list, str, int, bool, None]
  15. ALLOWED_EMOJIS = set(get_emoji_unicode_dict("en").values())
  16. class ValidationError(Exception):
  17. """
  18. Is being raised when config value passed can't be converted properly
  19. Must be raised with string, describing why value is incorrect
  20. It will be shown in .config, if user tries to set incorrect value
  21. """
  22. class Validator:
  23. """
  24. Class used as validator of config value
  25. :param validator: Sync function, which raises `ValidationError` if passed
  26. value is incorrect (with explanation) and returns converted
  27. value if it is semantically correct.
  28. ⚠️ If validator returns `None`, value will always be set to `None`
  29. :param doc: Docstrings for this validator as string, or dict in format:
  30. {
  31. "en": "docstring",
  32. "ru": "докстрингом",
  33. "ua": "докстрiнгом",
  34. "jp": "ヒント",
  35. }
  36. Use instrumental case with lowercase
  37. :param _internal_id: Do not pass anything here, or things will break
  38. """
  39. def __init__(
  40. self,
  41. validator: callable,
  42. doc: typing.Optional[typing.Union[str, dict]] = None,
  43. _internal_id: typing.Optional[int] = None,
  44. ):
  45. self.validate = validator
  46. if isinstance(doc, str):
  47. doc = {"en": doc, "ru": doc}
  48. self.doc = doc
  49. self.internal_id = _internal_id
  50. class Boolean(Validator):
  51. """
  52. Any logical value to be passed
  53. `1`, `"1"` etc. will be automatically converted to bool
  54. """
  55. def __init__(self):
  56. super().__init__(
  57. self._validate,
  58. {"en": "boolean", "ru": "логическим значением"},
  59. _internal_id="Boolean",
  60. )
  61. @staticmethod
  62. def _validate(value: ConfigAllowedTypes, /) -> bool:
  63. true_cases = ["True", "true", "1", 1, True]
  64. false_cases = ["False", "false", "0", 0, False]
  65. if value not in true_cases + false_cases:
  66. raise ValidationError("Passed value must be a boolean")
  67. return value in true_cases
  68. class Integer(Validator):
  69. """
  70. Checks whether passed argument is an integer value
  71. :param digits: Digits quantity, which must be passed
  72. :param minimum: Minimal number to be passed
  73. :param maximum: Maximum number to be passed
  74. """
  75. def __init__(
  76. self,
  77. *,
  78. digits: typing.Optional[int] = None,
  79. minimum: typing.Optional[int] = None,
  80. maximum: typing.Optional[int] = None,
  81. ):
  82. _sign_en = "positive " if minimum is not None and minimum == 0 else ""
  83. _sign_ru = "положительным " if minimum is not None and minimum == 0 else ""
  84. _sign_en = "negative " if maximum is not None and maximum == 0 else _sign_en
  85. _sign_ru = (
  86. "отрицательным " if maximum is not None and maximum == 0 else _sign_ru
  87. )
  88. _digits_en = f" with exactly {digits} digits" if digits is not None else ""
  89. _digits_ru = f", в котором ровно {digits} цифр " if digits is not None else ""
  90. if minimum is not None and minimum != 0:
  91. doc = (
  92. {
  93. "en": f"{_sign_en}integer greater than {minimum}{_digits_en}",
  94. "ru": f"{_sign_ru}целым числом больше {minimum}{_digits_ru}",
  95. }
  96. if maximum is None and maximum != 0
  97. else {
  98. "en": f"{_sign_en}integer from {minimum} to {maximum}{_digits_en}",
  99. "ru": (
  100. f"{_sign_ru}целым числом в промежутке от {minimum} до"
  101. f" {maximum}{_digits_ru}"
  102. ),
  103. }
  104. )
  105. elif maximum is None and maximum != 0:
  106. doc = {
  107. "en": f"{_sign_en}integer{_digits_en}",
  108. "ru": f"{_sign_ru}целым числом{_digits_ru}",
  109. }
  110. else:
  111. doc = {
  112. "en": f"{_sign_en}integer less than {maximum}{_digits_en}",
  113. "ru": f"{_sign_ru}целым числом меньше {maximum}{_digits_ru}",
  114. }
  115. super().__init__(
  116. functools.partial(
  117. self._validate,
  118. digits=digits,
  119. minimum=minimum,
  120. maximum=maximum,
  121. ),
  122. doc,
  123. _internal_id="Integer",
  124. )
  125. @staticmethod
  126. def _validate(
  127. value: ConfigAllowedTypes,
  128. /,
  129. *,
  130. digits: int,
  131. minimum: int,
  132. maximum: int,
  133. ) -> typing.Union[int, None]:
  134. try:
  135. value = int(str(value).strip())
  136. except ValueError:
  137. raise ValidationError(f"Passed value ({value}) must be a number")
  138. if minimum is not None and value < minimum:
  139. raise ValidationError(f"Passed value ({value}) is lower than minimum one")
  140. if maximum is not None and value > maximum:
  141. raise ValidationError(f"Passed value ({value}) is greater than maximum one")
  142. if digits is not None and len(str(value)) != digits:
  143. raise ValidationError(
  144. f"The length of passed value ({value}) is incorrect "
  145. f"(Must be exactly {digits} digits)"
  146. )
  147. return value
  148. class Choice(Validator):
  149. """
  150. Check whether entered value is in the allowed list
  151. :param possible_values: Allowed values to be passed to config param
  152. """
  153. def __init__(
  154. self,
  155. possible_values: typing.List[ConfigAllowedTypes],
  156. /,
  157. ):
  158. super().__init__(
  159. functools.partial(self._validate, possible_values=possible_values),
  160. {
  161. "en": (
  162. "one of the following:"
  163. f" {' / '.join(list(map(str, possible_values)))}"
  164. ),
  165. "ru": f"одним из: {' / '.join(list(map(str, possible_values)))}",
  166. },
  167. _internal_id="Choice",
  168. )
  169. @staticmethod
  170. def _validate(
  171. value: ConfigAllowedTypes,
  172. /,
  173. *,
  174. possible_values: typing.List[ConfigAllowedTypes],
  175. ) -> ConfigAllowedTypes:
  176. if value not in possible_values:
  177. raise ValidationError(
  178. f"Passed value ({value}) is not one of the following:"
  179. f" {' / '.join(list(map(str, possible_values)))}"
  180. )
  181. return value
  182. class MultiChoice(Validator):
  183. """
  184. Check whether every entered value is in the allowed list
  185. :param possible_values: Allowed values to be passed to config param
  186. """
  187. def __init__(
  188. self,
  189. possible_values: typing.List[ConfigAllowedTypes],
  190. /,
  191. ):
  192. super().__init__(
  193. functools.partial(self._validate, possible_values=possible_values),
  194. {
  195. "en": (
  196. "list of values, where each one must be one of:"
  197. f" {' / '.join(list(map(str, possible_values)))}"
  198. ),
  199. "ru": (
  200. "список значений, каждое из которых должно быть одним из"
  201. f" следующего: {' / '.join(list(map(str, possible_values)))}"
  202. ),
  203. },
  204. _internal_id="MultiChoice",
  205. )
  206. @staticmethod
  207. def _validate(
  208. value: typing.List[ConfigAllowedTypes],
  209. /,
  210. *,
  211. possible_values: typing.List[ConfigAllowedTypes],
  212. ) -> typing.List[ConfigAllowedTypes]:
  213. if not isinstance(value, (list, tuple)):
  214. value = [value]
  215. for item in value:
  216. if item not in possible_values:
  217. raise ValidationError(
  218. f"One of passed values ({item}) is not one of the following:"
  219. f" {' / '.join(list(map(str, possible_values)))}"
  220. )
  221. return list(set(value))
  222. class Series(Validator):
  223. """
  224. Represents the series of value (simply `list`)
  225. :param separator: With which separator values must be separated
  226. :param validator: Internal validator for each sequence value
  227. :param min_len: Minimal number of series items to be passed
  228. :param max_len: Maximum number of series items to be passed
  229. :param fixed_len: Fixed number of series items to be passed
  230. """
  231. def __init__(
  232. self,
  233. validator: typing.Optional[Validator] = None,
  234. min_len: typing.Optional[int] = None,
  235. max_len: typing.Optional[int] = None,
  236. fixed_len: typing.Optional[int] = None,
  237. ):
  238. _each_en = (
  239. f" (each must be {validator.doc['en']})" if validator is not None else ""
  240. )
  241. _each_ru = (
  242. f" (каждое должно быть {validator.doc['ru']})"
  243. if validator is not None
  244. else ""
  245. )
  246. if fixed_len is not None:
  247. _len_en = f" (exactly {fixed_len} pcs.)"
  248. _len_ru = f" (ровно {fixed_len} шт.)"
  249. elif min_len is None:
  250. if max_len is None:
  251. _len_en = ""
  252. _len_ru = ""
  253. else:
  254. _len_en = f" (up to {max_len} pcs.)"
  255. _len_ru = f" (до {max_len} шт.)"
  256. elif max_len is not None:
  257. _len_en = f" (from {min_len} to {max_len} pcs.)"
  258. _len_ru = f" (от {min_len} до {max_len} шт.)"
  259. else:
  260. _len_en = f" (at least {min_len} pcs.)"
  261. _len_ru = f" (как минимум {min_len} шт.)"
  262. doc = {
  263. "en": f"series of values{_len_en}{_each_en}, separated with «,»",
  264. "ru": f"списком значений{_len_ru}{_each_ru}, разделенных «,»",
  265. }
  266. super().__init__(
  267. functools.partial(
  268. self._validate,
  269. validator=validator,
  270. min_len=min_len,
  271. max_len=max_len,
  272. fixed_len=fixed_len,
  273. ),
  274. doc,
  275. _internal_id="Series",
  276. )
  277. @staticmethod
  278. def _validate(
  279. value: ConfigAllowedTypes,
  280. /,
  281. *,
  282. validator: typing.Optional[Validator] = None,
  283. min_len: typing.Optional[int] = None,
  284. max_len: typing.Optional[int] = None,
  285. fixed_len: typing.Optional[int] = None,
  286. ) -> typing.List[ConfigAllowedTypes]:
  287. if not isinstance(value, (list, tuple, set)):
  288. value = str(value).split(",")
  289. if isinstance(value, (tuple, set)):
  290. value = list(value)
  291. if min_len is not None and len(value) < min_len:
  292. raise ValidationError(
  293. f"Passed value ({value}) contains less than {min_len} items"
  294. )
  295. if max_len is not None and len(value) > max_len:
  296. raise ValidationError(
  297. f"Passed value ({value}) contains more than {max_len} items"
  298. )
  299. if fixed_len is not None and len(value) != fixed_len:
  300. raise ValidationError(
  301. f"Passed value ({value}) must contain exactly {fixed_len} items"
  302. )
  303. value = [item.strip() if isinstance(item, str) else item for item in value]
  304. if isinstance(validator, Validator):
  305. for i, item in enumerate(value):
  306. try:
  307. value[i] = validator.validate(item)
  308. except ValidationError:
  309. raise ValidationError(
  310. f"Passed value ({value}) contains invalid item"
  311. f" ({str(item).strip()}), which must be {validator.doc['en']}"
  312. )
  313. value = list(filter(lambda x: x, value))
  314. return value
  315. class Link(Validator):
  316. """Valid url must be specified"""
  317. def __init__(self):
  318. super().__init__(
  319. lambda value: self._validate(value),
  320. {
  321. "en": "link",
  322. "ru": "ссылкой",
  323. },
  324. _internal_id="Link",
  325. )
  326. @staticmethod
  327. def _validate(value: ConfigAllowedTypes, /) -> str:
  328. try:
  329. if not utils.check_url(value):
  330. raise Exception("Invalid URL")
  331. except Exception:
  332. raise ValidationError(f"Passed value ({value}) is not a valid URL")
  333. return value
  334. class String(Validator):
  335. """
  336. Checks for length of passed value and automatically converts it to string
  337. :param length: Exact length of string
  338. :param min_len: Minimal length of string
  339. :param max_len: Maximum length of string
  340. """
  341. def __init__(
  342. self,
  343. length: typing.Optional[int] = None,
  344. min_len: typing.Optional[int] = None,
  345. max_len: typing.Optional[int] = None,
  346. ):
  347. if length is not None:
  348. doc = {
  349. "en": f"string of length {length}",
  350. "ru": f"строкой из {length} символа(-ов)",
  351. }
  352. else:
  353. if min_len is None:
  354. if max_len is None:
  355. doc = {
  356. "en": "string",
  357. "ru": "строкой",
  358. }
  359. else:
  360. doc = {
  361. "en": f"string of length up to {max_len}",
  362. "ru": f"строкой не более чем из {max_len} символа(-ов)",
  363. }
  364. elif max_len is not None:
  365. doc = {
  366. "en": f"string of length from {min_len} to {max_len}",
  367. "ru": f"строкой из {min_len}-{max_len} символа(-ов)",
  368. }
  369. else:
  370. doc = {
  371. "en": f"string of length at least {min_len}",
  372. "ru": f"строкой не менее чем из {min_len} символа(-ов)",
  373. }
  374. super().__init__(
  375. functools.partial(
  376. self._validate,
  377. length=length,
  378. min_len=min_len,
  379. max_len=max_len,
  380. ),
  381. doc,
  382. _internal_id="String",
  383. )
  384. @staticmethod
  385. def _validate(
  386. value: ConfigAllowedTypes,
  387. /,
  388. *,
  389. length: typing.Optional[int],
  390. min_len: typing.Optional[int],
  391. max_len: typing.Optional[int],
  392. ) -> str:
  393. if (
  394. isinstance(length, int)
  395. and len(list(grapheme.graphemes(str(value)))) != length
  396. ):
  397. raise ValidationError(
  398. f"Passed value ({value}) must be a length of {length}"
  399. )
  400. if (
  401. isinstance(min_len, int)
  402. and len(list(grapheme.graphemes(str(value)))) < min_len
  403. ):
  404. raise ValidationError(
  405. f"Passed value ({value}) must be a length of at least {min_len}"
  406. )
  407. if (
  408. isinstance(max_len, int)
  409. and len(list(grapheme.graphemes(str(value)))) > max_len
  410. ):
  411. raise ValidationError(
  412. f"Passed value ({value}) must be a length of up to {max_len}"
  413. )
  414. return str(value)
  415. class RegExp(Validator):
  416. """
  417. Checks if value matches the regex
  418. :param regex: Regex to match
  419. :param flags: Flags to pass to re.compile
  420. :param description: Description of regex
  421. """
  422. def __init__(
  423. self,
  424. regex: str,
  425. flags: typing.Optional[re.RegexFlag] = None,
  426. description: typing.Optional[typing.Union[dict, str]] = None,
  427. ):
  428. if not flags:
  429. flags = 0
  430. try:
  431. re.compile(regex, flags=flags)
  432. except re.error as e:
  433. raise Exception(f"{regex} is not a valid regex") from e
  434. if description is None:
  435. doc = {
  436. "en": f"string matching pattern «{regex}»",
  437. "ru": f"строкой, соответствующей шаблону «{regex}»",
  438. }
  439. else:
  440. if isinstance(description, str):
  441. doc = {
  442. "en": description,
  443. "ru": description,
  444. }
  445. else:
  446. doc = description
  447. super().__init__(
  448. functools.partial(self._validate, regex=regex, flags=flags),
  449. doc,
  450. _internal_id="RegExp",
  451. )
  452. @staticmethod
  453. def _validate(
  454. value: ConfigAllowedTypes,
  455. /,
  456. *,
  457. regex: str,
  458. flags: typing.Optional[re.RegexFlag],
  459. ) -> str:
  460. if not re.match(regex, value, flags=flags):
  461. raise ValidationError(f"Passed value ({value}) must follow pattern {regex}")
  462. return value
  463. class Float(Validator):
  464. """
  465. Checks whether passed argument is a float value
  466. :param minimum: Minimal number to be passed
  467. :param maximum: Maximum number to be passed
  468. """
  469. def __init__(
  470. self,
  471. minimum: typing.Optional[float] = None,
  472. maximum: typing.Optional[float] = None,
  473. ):
  474. _sign_en = "positive " if minimum is not None and minimum == 0 else ""
  475. _sign_ru = "положительным " if minimum is not None and minimum == 0 else ""
  476. _sign_en = "negative " if maximum is not None and maximum == 0 else _sign_en
  477. _sign_ru = (
  478. "отрицательным " if maximum is not None and maximum == 0 else _sign_ru
  479. )
  480. if minimum is not None and minimum != 0:
  481. doc = (
  482. {
  483. "en": f"{_sign_en}float greater than {minimum}",
  484. "ru": f"{_sign_ru}дробным числом больше {minimum}",
  485. }
  486. if maximum is None and maximum != 0
  487. else {
  488. "en": f"{_sign_en}float from {minimum} to {maximum}",
  489. "ru": (
  490. f"{_sign_ru}дробным числом в промежутке от {minimum} до"
  491. f" {maximum}"
  492. ),
  493. }
  494. )
  495. elif maximum is None and maximum != 0:
  496. doc = {
  497. "en": f"{_sign_en}float",
  498. "ru": f"{_sign_ru}дробным числом",
  499. }
  500. else:
  501. doc = {
  502. "en": f"{_sign_en}float less than {maximum}",
  503. "ru": f"{_sign_ru}дробным числом меньше {maximum}",
  504. }
  505. super().__init__(
  506. functools.partial(
  507. self._validate,
  508. minimum=minimum,
  509. maximum=maximum,
  510. ),
  511. doc,
  512. _internal_id="Float",
  513. )
  514. @staticmethod
  515. def _validate(
  516. value: ConfigAllowedTypes,
  517. /,
  518. *,
  519. minimum: typing.Optional[float] = None,
  520. maximum: typing.Optional[float] = None,
  521. ) -> float:
  522. try:
  523. value = float(str(value).strip().replace(",", "."))
  524. except ValueError:
  525. raise ValidationError(f"Passed value ({value}) must be a float")
  526. if minimum is not None and value < minimum:
  527. raise ValidationError(f"Passed value ({value}) is lower than minimum one")
  528. if maximum is not None and value > maximum:
  529. raise ValidationError(f"Passed value ({value}) is greater than maximum one")
  530. return value
  531. class TelegramID(Validator):
  532. def __init__(self):
  533. super().__init__(
  534. self._validate,
  535. "Telegram ID",
  536. _internal_id="TelegramID",
  537. )
  538. @staticmethod
  539. def _validate(value: ConfigAllowedTypes, /) -> int:
  540. e = ValidationError(f"Passed value ({value}) is not a valid telegram id")
  541. try:
  542. value = int(str(value).strip())
  543. except Exception:
  544. raise e
  545. if str(value).startswith("-100"):
  546. value = int(str(value)[4:])
  547. if value > 2**64 - 1 or value < 0:
  548. raise e
  549. return value
  550. class Union(Validator):
  551. def __init__(self, *validators):
  552. doc = {
  553. "en": "one of the following:\n",
  554. "ru": "одним из следующего:\n",
  555. }
  556. def case(x: str) -> str:
  557. return x[0].upper() + x[1:]
  558. for validator in validators:
  559. doc["en"] += f"- {case(validator.doc['en'])}\n"
  560. doc["ru"] += f"- {case(validator.doc['ru'])}\n"
  561. doc["en"] = doc["en"].strip()
  562. doc["ru"] = doc["ru"].strip()
  563. super().__init__(
  564. functools.partial(self._validate, validators=validators),
  565. doc,
  566. _internal_id="Union",
  567. )
  568. @staticmethod
  569. def _validate(
  570. value: ConfigAllowedTypes,
  571. /,
  572. *,
  573. validators: list,
  574. ) -> ConfigAllowedTypes:
  575. for validator in validators:
  576. try:
  577. return validator.validate(value)
  578. except ValidationError:
  579. pass
  580. raise ValidationError(f"Passed value ({value}) is not valid")
  581. class NoneType(Validator):
  582. def __init__(self):
  583. super().__init__(
  584. self._validate,
  585. "`None`",
  586. _internal_id="NoneType",
  587. )
  588. @staticmethod
  589. def _validate(value: ConfigAllowedTypes, /) -> None:
  590. if value not in {None, False, ""}:
  591. raise ValidationError(f"Passed value ({value}) is not None")
  592. return None
  593. class Hidden(Validator):
  594. def __init__(self, validator: typing.Optional[Validator] = None):
  595. if not validator:
  596. validator = String()
  597. super().__init__(
  598. functools.partial(self._validate, validator=validator),
  599. validator.doc,
  600. _internal_id="Hidden",
  601. )
  602. @staticmethod
  603. def _validate(
  604. value: ConfigAllowedTypes,
  605. /,
  606. *,
  607. validator: Validator,
  608. ) -> ConfigAllowedTypes:
  609. return validator.validate(value)
  610. class Emoji(Validator):
  611. """
  612. Checks whether passed argument is a valid emoji
  613. :param quantity: Number of emojis to be passed
  614. :param min_len: Minimum number of emojis
  615. :param max_len: Maximum number of emojis
  616. """
  617. def __init__(
  618. self,
  619. length: typing.Optional[int] = None,
  620. min_len: typing.Optional[int] = None,
  621. max_len: typing.Optional[int] = None,
  622. ):
  623. if length is not None:
  624. doc = {
  625. "en": f"{length} emojis",
  626. "ru": f"ровно {length} эмодзи",
  627. }
  628. elif min_len is not None and max_len is not None:
  629. doc = {
  630. "en": f"{min_len} to {max_len} emojis",
  631. "ru": f"от {min_len} до {max_len} эмодзи",
  632. }
  633. elif min_len is not None:
  634. doc = {
  635. "en": f"at least {min_len} emoji",
  636. "ru": f"не менее {min_len} эмодзи",
  637. }
  638. elif max_len is not None:
  639. doc = {
  640. "en": f"no more than {max_len} emojis",
  641. "ru": f"не более {max_len} эмодзи",
  642. }
  643. else:
  644. doc = {
  645. "en": "emoji",
  646. "ru": "эмодзи",
  647. }
  648. super().__init__(
  649. functools.partial(
  650. self._validate,
  651. length=length,
  652. min_len=min_len,
  653. max_len=max_len,
  654. ),
  655. doc,
  656. _internal_id="Emoji",
  657. )
  658. @staticmethod
  659. def _validate(
  660. value: ConfigAllowedTypes,
  661. /,
  662. *,
  663. length: typing.Optional[int],
  664. min_len: typing.Optional[int],
  665. max_len: typing.Optional[int],
  666. ) -> str:
  667. value = str(value)
  668. passed_length = len(list(grapheme.graphemes(value)))
  669. if length is not None and passed_length != length:
  670. raise ValidationError(f"Passed value ({value}) is not {length} emojis long")
  671. if (
  672. min_len is not None
  673. and max_len is not None
  674. and (passed_length < min_len or passed_length > max_len)
  675. ):
  676. raise ValidationError(
  677. f"Passed value ({value}) is not between {min_len} and {max_len} emojis"
  678. " long"
  679. )
  680. if min_len is not None and passed_length < min_len:
  681. raise ValidationError(
  682. f"Passed value ({value}) is not at least {min_len} emojis long"
  683. )
  684. if max_len is not None and passed_length > max_len:
  685. raise ValidationError(
  686. f"Passed value ({value}) is not no more than {max_len} emojis long"
  687. )
  688. if any(emoji not in ALLOWED_EMOJIS for emoji in grapheme.graphemes(value)):
  689. raise ValidationError(
  690. f"Passed value ({value}) is not a valid string with emojis"
  691. )
  692. return value
  693. class EntityLike(RegExp):
  694. def __init__(self):
  695. super().__init__(
  696. regex=r"^(?:@|https?://t\.me/)?(?:[a-zA-Z0-9_]{5,32}|[a-zA-Z0-9_]{1,32}\?[a-zA-Z0-9_]{1,32})$",
  697. description={
  698. "en": "link to entity, username or Telegram ID",
  699. "ru": "ссылка на сущность, имя пользователя или Telegram ID",
  700. },
  701. )