diff --git a/Source/Errors.swift b/Source/Errors.swift index 815ae6b..3f9e8d3 100644 --- a/Source/Errors.swift +++ b/Source/Errors.swift @@ -8,5 +8,6 @@ public enum StickersError: Error { case dataTypeMismatch case setIsEmpty case emojiIsEmpty + case emojisInvalid case telegramNotInstalled } diff --git a/Source/StickerSet.swift b/Source/StickerSet.swift index 69b54c5..9feee10 100644 --- a/Source/StickerSet.swift +++ b/Source/StickerSet.swift @@ -130,10 +130,18 @@ public class StickerSet { if emojis.isEmpty { throw StickersError.emojiIsEmpty } + if !arrayContainsEmoji(array: emojis) { + throw StickersError.emojisInvalid + } if try self.validateData(data) { self.stickers.append(Sticker(data: data, emojis: emojis)) } } + + /// Returns true if an array contains at least 1 valid emoji + func arrayContainsEmoji(array: [String]) -> Bool { + return array.contains { $0.containsEmoji } + } /** Sets a thumbnail of the sticker set using the provided data. diff --git a/Source/StringExtension.swift b/Source/StringExtension.swift new file mode 100644 index 0000000..7c25c75 --- /dev/null +++ b/Source/StringExtension.swift @@ -0,0 +1,11 @@ +extension String { + /// Checks if a string is a valid emoji + var containsEmoji: Bool { + for scalar in self.unicodeScalars { + if scalar.properties.isEmoji && (scalar.value > 0x238C) { // Exclude non-graphical symbols + return true + } + } + return false + } +}