validate

package
v0.0.0-...-d6528d1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2020 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package validate provides routines for validating texts

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashtagIsValid

func HashtagIsValid(hashtag string) bool

HashtagIsValid returns true if the given text represents a valid #hashtag

func TextIsValid

func TextIsValid(text string, args ValidationArgs) bool

TextIsValid checks whether a string is a valid text and returns true or false

func TextLength

func TextLength(text string) int

TextLength returns the 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.

func TextValidate

func TextValidate(text string, args ValidationArgs) error

TextValidate checks whether a string is a valid text. 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

func URLIsValid

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

URLIsValid returns true if the given text represents a valid URL

func UsernameIsValid

func UsernameIsValid(username string) bool

UsernameIsValid returns true if the given text represents a valid @username

Types

type EmptyError

type EmptyError struct{}

EmptyError is returned when text is empty

func (EmptyError) Error

func (e EmptyError) Error() string

type InvalidCharacterError

type InvalidCharacterError struct {
	Character rune
	Offset    int
}

InvalidCharacterError is 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 struct {
	// contains filtered or unexported fields
}

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

func (TooLongError) Error

func (e TooLongError) Error() string

type ValidationArgs

type ValidationArgs struct {
	MaxLength  int
	CanBeEmpty bool
}

Jump to

Keyboard shortcuts

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