validation

package
v0.0.0-...-a27e026 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: AGPL-3.0 Imports: 11 Imported by: 3 modules, 17 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// DefaultMaxRunes applies to text sanitization. It defines a default number of runes to truncate a string to if no value is given.
	DefaultMaxRunes = 300
	// DefaultEscapePasses defines a max number of passes to attempt to unescape a string before giving up.
	DefaultEscapePasses = 5
)

Variables

View Source
var AllowedURLSchemes = []string{"http", "https", "ftp", "mailto"}
View Source
var ErrInvalid = errors.New("object is invalid")
View Source
var ErrNilObject = errors.New("object is nil")

Functions

func IsValid

func IsValid[T any](obj T) bool

IsValid will check if an object is valid according to the validation tags on the object. It does not return any details of validation issues, only a boolean for valid (true) or invalid (false).

func ParseStructValidationErrors

func ParseStructValidationErrors(err error) error

ParseStructValidationErrors takes the underlying validation errors and formats them so that each struct field has an array of all validation errors associated with it.

func SanitizeHTML

func SanitizeHTML(str string) string

SanitizeHTML sanitizes strings containing HTML. It performs:

bluemonday -> html unescaping -> trimspace

The resulting string should still contain valid HTML, but will be much safer to use.

func SanitizeInput

func SanitizeInput(s string) string

SanitizeInput wraps SanitizeText with the DefaultMaxRunes. It should be able to sanitize most inputs that are short-form text.

func SanitizeText

func SanitizeText(s string, maxRunes int) string

SanitizeText prepares a plain-text field:

strips control bidi-override and zero-width characters -> replaces newlines -> and caps the length.

Output is still untrusted and must be escaped.

func SanitizeURLString

func SanitizeURLString(str string) (string, error)

SanitizeURLString attempts to "sanitize" a string value representing a URL. It performs:

path unescaping -> url parsing -> scheme validation

It does not perform other validation, such as checking for absolute vs relative, host, etc.

Types

type Error

type Error struct {
	Details string
	// contains filtered or unexported fields
}

Error is a map of fields and their validation errors.

func (*Error) Error

func (e *Error) Error() string

Jump to

Keyboard shortcuts

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