validation

package
v0.49.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package validation implements a functional API for consistent struct level validation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JoinErrors

func JoinErrors(b *strings.Builder, errs []error, indent string)

Types

type FieldError

type FieldError struct {
	FieldPath  string      `json:"fieldPath"`
	FieldValue interface{} `json:"value"`
	Errors     []string    `json:"errors"`
}

func NewFieldError

func NewFieldError(fieldPath string, fieldValue interface{}, errs []error) *FieldError

func (FieldError) Error

func (e FieldError) Error() string

func (FieldError) ValueString

func (e FieldError) ValueString() string

type FieldRules

type FieldRules[T, S any] struct {
	// contains filtered or unexported fields
}

FieldRules is responsible for validating a single struct field.

func RulesForField

func RulesForField[T, S any](fieldPath string, getter func(S) T) FieldRules[T, S]

RulesForField creates a typed FieldRules instance for the field which access is defined through getter function.

func (FieldRules[T, S]) If

func (r FieldRules[T, S]) If(predicate func() bool) FieldRules[T, S]

func (FieldRules[T, S]) Validate

func (r FieldRules[T, S]) Validate(st S) error

func (FieldRules[T, S]) With

func (r FieldRules[T, S]) With(rules ...Rule[T]) FieldRules[T, S]

type MultiRule

type MultiRule[T any] []Rule[T]

MultiRule allows defining Rule which aggregates multiple sub-rules.

func StringIsDNSSubdomain

func StringIsDNSSubdomain() MultiRule[string]

func (MultiRule[T]) Validate

func (r MultiRule[T]) Validate(v T) error

type Rule

type Rule[T any] interface {
	Validate(v T) error
}

Rule is the interface for all validation rules.

type SingleRule

type SingleRule[T any] func(v T) error

func StringDescription

func StringDescription() SingleRule[string]

func StringLength

func StringLength(min, max int) SingleRule[string]

func StringRequired

func StringRequired() SingleRule[string]

func (SingleRule[T]) Validate

func (r SingleRule[T]) Validate(v T) error

type StructRules

type StructRules[S any] struct {
	// contains filtered or unexported fields
}

func RulesForStruct

func RulesForStruct[S any](rules ...fieldRules[S]) StructRules[S]

func (StructRules[S]) Validate

func (r StructRules[S]) Validate(st S) []error

Jump to

Keyboard shortcuts

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