v

package
v2.7.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package v implements the common validation logic of Easegress.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSchema

func GetSchema(t reflect.Type) (*genjs.Schema, error)

GetSchema returns the json schema of t.

func IsOmitemptyField added in v2.6.3

func IsOmitemptyField(field *reflect.StructField) bool

Types

type FormatFunc

type FormatFunc func(v interface{}) error

FormatFunc validates the customized format in json schema. The function could panic if the types are unexpected.

type ValidateRecorder

type ValidateRecorder struct {
	// JSONSchemaErrs generated by vendor json schema.
	JSONSchemaErrs []string `json:"jsonschemaErrs,omitempty"`
	// FormatErrs generated by the format function of the single field.
	FormatErrs []string `json:"formatErrs,omitempty"`
	// GeneralErrs generated by Validate() of the Validator itself.
	GeneralErrs []string `json:"generalErrs,omitempty"`

	// SystemErr stands internal error, which often means bugs.
	SystemErr string `json:"systemErr,omitempty"`
}

ValidateRecorder records varied errors after validating.

func Validate

func Validate(v interface{}) *ValidateRecorder

Validate validates by json schema rules, custom formats and general methods.

func (*ValidateRecorder) Error

func (vr *ValidateRecorder) Error() string

func (*ValidateRecorder) String

func (vr *ValidateRecorder) String() string

func (*ValidateRecorder) Valid

func (vr *ValidateRecorder) Valid() bool

Valid represents if the result is valid.

type Validator

type Validator interface {
	Validate() error
}

Validator stands for the types which needs its own Validate function. NOTE: Please always define Validate on non-pointer level Spec.Validate instead of (*Spec).Validate . Based on the mechanism of methods visibility in Golang, if the spec looks like:

type Spec struct {
	A 	A
	B 	*B
	CC 	[]C
	DD 	[]*D
	EE 	map[string]E
	FF 	map[string]*F
}

If the methods Validate of all A-F is defined on pointer level:

A.Validate, CC[i].Validate, EE[i].Validate won't be found.
B.Validate, DD[i].Validate, FF[i].Validate will be found.

But if the methods Validate of all A-F is defined on non-pointer level. All of them will be found.

If you really want to define Validate on pointer level, please keep it appear in pointer form within spec. Otherwise, it won't be called.

Jump to

Keyboard shortcuts

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