validate

package
v0.0.0-...-71fd032 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package validate provides routines for validating tweets

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashtagIsValid

func HashtagIsValid(hashtag string) bool

Returns true if the given text represents a valid #hashtag

func ListIsValid

func ListIsValid(list string) bool

Returns true if the given text represents a valid @twitter/list

func TweetIsValid

func TweetIsValid(text string) bool

Checks whether a string is a valid tweet and returns true or false

func TweetLength

func TweetLength(text string) int

Returns the V1 length of the string as it would be displayed. This is equivalent to the length of the Unicode NFC (See: http://www.unicode.org/reports/tr15). This is needed in order to consistently calculate the length of a string no matter which actual form was transmitted. For example:

U+0065  Latin Small Letter E

+ U+0301 Combining Acute Accent ---------- = 2 bytes, 2 characters, displayed as é (1 visual glyph)

… The NFC of {U+0065, U+0301} is {U+00E9}, which is a single character and a +display_length+ of 1

The string could also contain U+00E9 already, in which case the canonicalization will not change the value. Note that per twitter-text, this method is deprecated and will always return the v1 values New clients should use ParseTweet

func UrlIsValid

func UrlIsValid(url string, requireProtocol bool, allowUnicode bool) bool

Returns true if the given text represents a valid URL

func UsernameIsValid

func UsernameIsValid(username string) bool

Returns true if the given text represents a valid @username

func ValidateTweet

func ValidateTweet(text string) error

Checks whether a string is a valid tweet. Returns nil if the string is valid. Otherwise, it returns an error in the following cases:

- The text is too long - The text is empty - The text contains invalid characters

Types

type EmptyError

type EmptyError struct{}

Validation error returned when text is empty

func (EmptyError) Error

func (e EmptyError) Error() string

type InvalidCharacterError

type InvalidCharacterError struct {
	Character rune
	Offset    int
}

Validation error returned when text contains an invalid character

This error embeds the value of the invalid character, and its byte-offset within the input string

func (InvalidCharacterError) Error

func (e InvalidCharacterError) Error() string

type TooLongError

type TooLongError int

Validation error returned when text is too long to be a valid tweet. The value of the error is the actual length of the input string

func (TooLongError) Error

func (e TooLongError) Error() string

type Tweet

type Tweet struct {
	WeightedLength int
	Permillage     int
	Valid          bool
}

func ParseTweet

func ParseTweet(text string) Tweet

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL