validate

package
v0.0.0-...-1caa961 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChainValidator validator.Func = func(fl validator.FieldLevel) bool {
	chain := fl.Field().Int()
	return chain >= 0 && chain <= int64(persist.MaxChainValue)
}

ChainValidator ensures the specified Chain is one we support

View Source
var CreatedCollectionsValidator validator.Func = func(fl validator.FieldLevel) bool {
	if s, ok := fl.Field().Interface().([]*model.CreateCollectionInGalleryInput); ok {
		return !slices.ContainsFunc(s, func(l *model.CreateCollectionInGalleryInput) bool {
			return l.GivenID == ""
		})
	}
	return true
}

CreatedCollectionsValidator validates that the create collection input has valid given IDs

View Source
var DefaultValidator = WithCustomValidators()
View Source
var EthValidator validator.Func = func(fl validator.FieldLevel) bool {
	addr := fl.Field().String()
	if addr == "" {
		return true
	}
	return len(addr) == 42 && strings.HasPrefix(addr, "0x")
}

EthValidator validates ethereum addresses

View Source
var HTTPValidator validator.Func = func(fl validator.FieldLevel) bool {
	s := fl.Field().String()

	if s == "" {
		return true
	}

	_, err := url.ParseRequestURI(s)
	if err != nil {
		return false
	}

	u, err := url.Parse(s)
	if err != nil || u.Scheme == "" || u.Host == "" {
		return false
	}

	return true
}

HTTPValidator validates a string ensuring it is an HTTP url

View Source
var IsOptInRole validator.Func = func(fl validator.FieldLevel) bool {
	role := persist.Role(fl.Field().String())
	return role == persist.RoleBetaTester
}

IsOptInRole designates roles that users can opt themselves into

View Source
var IsValidPersona validator.Func = func(fl validator.FieldLevel) bool {
	persona := persist.Persona(fl.Field().String())
	return persona == persist.PersonaNone ||
		persona == persist.PersonaCollector ||
		persona == persist.PersonaCreator ||
		persona == persist.PersonaBoth
}
View Source
var IsValidRole validator.Func = func(fl validator.FieldLevel) bool {
	role := persist.Role(fl.Field().String())
	return role == persist.RoleAdmin || role == persist.RoleBetaTester
}
View Source
var NonceValidator validator.Func = func(fl validator.FieldLevel) bool {
	nonce := fl.Field().String()
	if nonce == "" {
		return true
	}
	return len(nonce) >= 10 && len(nonce) <= 150
}

NonceValidator validates nonces generated by the app

View Source
var SanitizationPolicy = bluemonday.UGCPolicy()

SanitizationPolicy is a policy for sanitizing user input

View Source
var SignatureValidator validator.Func = func(fl validator.FieldLevel) bool {
	sig := fl.Field().String()
	if sig == "" {
		return true
	}
	return len(sig) >= 80 && len(sig) <= 200
}

SignatureValidator validates ethereum wallet signed messages

View Source
var SortedAscValidator validator.Func = func(fl validator.FieldLevel) bool {
	if s, ok := fl.Field().Interface().([]int); ok {
		return sort.IntsAreSorted(s)
	}
	return false
}

SortedAscValidator validates that the array is sorted in ascending order.

View Source
var UsernameValidator validator.Func = func(fl validator.FieldLevel) bool {
	s := fl.Field().String()
	if s == "" {
		return true
	}
	if _, ok := bannedUsernames[s]; ok {
		return false
	}
	return len(s) >= 2 && len(s) <= 50 &&
		alphanumericUnderscoresPeriodsRegex.MatchString(s) &&
		!consecutivePeriodsOrUnderscores(s)
}

UsernameValidator ensures that usernames are not reserved, are alphanumeric with the exception of underscores and periods, and do not contain consecutive periods or underscores

Functions

func ChainAddressValidator

func ChainAddressValidator(sl validator.StructLevel)

func CollectionTokenSettingsParamsValidator

func CollectionTokenSettingsParamsValidator(sl validator.StructLevel)

CollectionTokenSettingsParamsValidator checks that the input CollectionTokenSettingsParams struct is valid

func ConnectionPaginationParamsValidator

func ConnectionPaginationParamsValidator(sl validator.StructLevel)

func EventValidator

func EventValidator(sl validator.StructLevel)

func RegisterCustomValidators

func RegisterCustomValidators(v *validator.Validate)

func Validate

func Validate(fields ValidationMap) error

Validate validates input fields with the default validator

func ValidateFields

func ValidateFields(validator *validator.Validate, fields ValidationMap) error

ValidateFields validates input fields based on a set of predefined validation tags

func WithCustomValidators

func WithCustomValidators() *validator.Validate

Types

type CollectionTokenSettingsParams

type CollectionTokenSettingsParams struct {
	Tokens        []persist.DBID                                   `json:"tokens"`
	TokenSettings map[persist.DBID]persist.CollectionTokenSettings `json:"token_settings"`
}

CollectionTokenSettingsParams are args passed to collection create and update functions that are meant to be validated together

type ConnectionPaginationParams

type ConnectionPaginationParams struct {
	First *int
	Last  *int
}

type ErrInvalidInput

type ErrInvalidInput struct {
	Parameters []string
	Reasons    []string
}

func (*ErrInvalidInput) Append

func (e *ErrInvalidInput) Append(parameter string, reason string)

func (ErrInvalidInput) Error

func (e ErrInvalidInput) Error() string

type ValWithTags

type ValWithTags struct {
	Value interface{}
	Tag   string
}

func WithTag

func WithTag(v any, t string) ValWithTags

type ValidationMap

type ValidationMap map[string]ValWithTags

Jump to

Keyboard shortcuts

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