validator

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Predefined image content types
	ImageContentTypeJpeg = "image/jpeg"
	ImageContentTypePng  = "image/png"
	ImageContentTypeGif  = "image/gif"
)

Variables

View Source
var (
	ErrFileSizeLt             = errors.New("file size is less than")
	ErrFileSizeGt             = errors.New("file size is greater than")
	ErrInvalidFileContentType = errors.New("invalid file content type")
)

Predefined errors

View Source
var DefaultFormMaxMemory int64 = 32 << 20 // 32 MB

DefaultFormMaxMemory is the default maximum amount of memory to use when parsing a multipart form.

View Source
var ErrValidation = errors.New("validation_failed")

ErrValidation is returned when validation fails.

Functions

func ValidateEmail

func ValidateEmail(s string) error

Email is a validator that checks if the value is a valid email address.

func ValidateFile

func ValidateFile(file *multipart.FileHeader, field string, validators ...fileValidationFunc) url.Values

ValidateFile validates file size and content type. It returns url.Values if there is an error otherwise it returns nil.

func ValidateFileContentType

func ValidateFileContentType(contentTypes ...string) fileValidationFunc

ValidateFileContentType validates file content type. It returns url.Values if there is an error otherwise it returns nil.

func ValidateFileFromRequest

func ValidateFileFromRequest(r *http.Request, field string, validators ...fileValidationFunc) url.Values

ValidateFileFromRequest validates file size and content type from multipart form request. It returns url.Values if there is an error otherwise it returns nil.

func ValidateFileSize

func ValidateFileSize(min, max string) fileValidationFunc

ValidateFileSize validates file size.

func ValidateImage

func ValidateImage(file *multipart.FileHeader, field, min, max string) url.Values

ValidateImage validates image file size and content type. It returns url.Values if there is an error otherwise it returns nil.

func ValidateSolanaWalletAddr

func ValidateSolanaWalletAddr(addr string) error

ValidateSolanaWalletAddr validates a Solana wallet address. Returns an error if the address is invalid, nil otherwise.

func ValidateStruct

func ValidateStruct(s interface{}) url.Values

ValidateStruct validate struct data - s: struct pointer - return: validation errors as url.Values or nil if no errors

Types

type EmailAddress

type EmailAddress struct {
	// LocalPart usually the username of an email address.
	LocalPart string

	// Domain is the part of the email address after the last @.
	// This should be DNS resolvable to an email server.
	Domain string
}

EmailAddress is a structure that stores the address local-part@domain parts.

func (EmailAddress) String

func (e EmailAddress) String() string

func (EmailAddress) ValidateHost

func (e EmailAddress) ValidateHost() error

ValidateHost will test if the email address is actually reachable. It will first try to resolve the host and then start a mail transaction.

func (EmailAddress) ValidateIcanSuffix

func (e EmailAddress) ValidateIcanSuffix() error

ValidateIcanSuffix will test if the public suffix of the domain is managed by ICANN using the golang.org/x/net/publicsuffix package. If not it will return an error. Note that if this method returns an error it does not necessarily mean that the email address is invalid. Also the suffix list in the standard package is embedded and thereby not up to date.

type ValidationError

type ValidationError struct {
	Err    error      // The underlying error.
	Values url.Values // A map of field names to error messages.
}

ValidationError is a custom error type that contains a map of field names to error messages.

func NewValidationError

func NewValidationError(values url.Values) *ValidationError

NewValidationError returns a new ValidationError.

func (*ValidationError) AddValue

func (e *ValidationError) AddValue(key, message string) *ValidationError

AddValue adds a new error message to the map.

func (*ValidationError) AddValues

func (e *ValidationError) AddValues(values url.Values) *ValidationError

AddValues adds a new error message to the map.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error returns the error message.

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Unwrap returns the underlying error.

Jump to

Keyboard shortcuts

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