constraints

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: BSD-3-Clause Imports: 17 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayOptions added in v1.0.3

type ArrayOptions struct {
	Options []any
}

func (ArrayOptions) Validate added in v1.0.3

func (c ArrayOptions) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Options{
	Options: []any{"Red", "Green", "Blue", 100},
}

values := []any{"Red", "Green", 100}
errs := validator.Validate(values, con)

type ArrayRange added in v1.0.3

type ArrayRange struct {
	Min float64
	Max float64
}

func (ArrayRange) Validate added in v1.0.3

func (c ArrayRange) Validate(value any) error

Validate function for validate value

Example:

con := constrains.ArrayRange{
	Min: 5,
	Max: 20,
}

value := []any{5, 15, 19}
errs := validator.Validate(value, con)

type Blank

type Blank struct{}

func (Blank) Validate

func (c Blank) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Blank{}
errs := validator.Validate(value, con)

type Composite added in v1.0.6

type Composite struct {
	Constraints []validator.Constraint
}

func (Composite) Validate added in v1.0.6

func (c Composite) Validate(value any) error

Validate function for validate value

Example:

	nestedValidator := constraints.Field{
	   Constraints: map[string]validator.Constraint{
	       "Email": constraints.Composite{
	           Constraints: []validator.Constraint{
	               constraints.NotBlank{},
	               constraints.Length{Min: 5, Max: 50},
	               constraints.Email{},
	           },
	       },
	       "Name": constraints.Composite{
	           Constraints: []validator.Constraint{
	               constraints.NotBlank{},
	               constraints.Length{Min: 3, Max: 100},
	           },
	       },
	   },
	}

 errs := validator.Validate(value, nestedValidator)

type Contains added in v1.3.0

type Contains struct {
	Substring string
}

func (Contains) Validate added in v1.3.0

func (c Contains) Validate(value any) error

type Country added in v1.1.0

type Country struct{}

func (Country) Validate added in v1.1.0

func (c Country) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Country{}
value := "CZ"
errs := validator.Validate(value, con)

type Currency

type Currency struct{}

func (Currency) Validate

func (c Currency) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Currency{}
value := "USD"
errs := validator.Validate(value, con)

type Custom added in v1.3.0

type Custom struct {
	Fn func(value any) error
}

Custom allows validating a value using a user-defined function. If the function returns an error, the constraint fails.

func (Custom) Validate added in v1.3.0

func (c Custom) Validate(value any) error

type Date added in v1.0.4

type Date struct {
	Min      time.Time
	Max      time.Time
	Format   string
	AllowNil bool
}

func (Date) Validate added in v1.0.4

func (c Date) Validate(value any) error

Validate function for validate value

Example:

dateFormat := "2006-01-02"
minDate, _ := time.Parse(dateFormat, "2023-01-01")
maxDate, _ := time.Parse(dateFormat, "2023-12-31")

con := constraints.Date{
	Min:    minDate,
	Max:    maxDate,
	Format: dateFormat,
}

value := "01-05-2023"

errs := validator.Validate(value, con)

type Email

type Email struct{}

func (Email) Validate

func (c Email) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Email{}
errs := validator.Validate(value, con)

type EqualTo added in v1.0.5

type EqualTo struct {
	Value any
}

func (EqualTo) Validate added in v1.0.5

func (r EqualTo) Validate(value any) error

Validate function for validate value

Example:

con := constraints.EqualTo{
	Value: "master"
}
errs := validator.Validate(value, con)

type Field added in v1.0.6

type Field struct {
	Constraints map[string]validator.Constraint
}

func (Field) Validate added in v1.0.6

func (n Field) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Field{
   Constraints: map[string]validator.Constraint{
       "Name":  constraints.NotBlank{},
       "Email": constraints.Email{},
       "Address": constraints.Field{
           Constraints: map[string]validator.Constraint{
               "City":    constraints.NotBlank{},
               "Country": constraints.NotBlank{},
           },
       },
   },
}

errs := validator.Validate(value, nestedValidator)

type GreaterOrEqual added in v1.0.5

type GreaterOrEqual struct {
	Value float64
}

func (GreaterOrEqual) Validate added in v1.0.5

func (r GreaterOrEqual) Validate(value any) error

Validate function for validate value

Example:

con := constraints.GreaterOrEqual{
	Value: 35
}
errs := validator.Validate(value, con)

type GreaterThen added in v1.0.5

type GreaterThen struct {
	Value float64
}

func (GreaterThen) Validate added in v1.0.5

func (r GreaterThen) Validate(value any) error

Validate function for validate value

Example:

con := constraints.GreaterThen{
	Value: 35
}
errs := validator.Validate(value, con)

type HasPrefix added in v1.3.0

type HasPrefix struct {
	Prefix string
}

func (HasPrefix) Validate added in v1.3.0

func (c HasPrefix) Validate(value any) error

type HasSuffix added in v1.3.0

type HasSuffix struct {
	Suffix string
}

func (HasSuffix) Validate added in v1.3.0

func (c HasSuffix) Validate(value any) error

type IsBase32 added in v1.3.0

type IsBase32 struct{}

func (IsBase32) Validate added in v1.3.0

func (c IsBase32) Validate(value any) error

type IsBase64 added in v1.3.0

type IsBase64 struct{}

func (IsBase64) Validate added in v1.3.0

func (c IsBase64) Validate(value any) error

type IsBool

type IsBool struct{}

func (IsBool) Validate

func (c IsBool) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsBool{}
errs := validator.Validate(value, con)

type IsCamelCase added in v1.3.0

type IsCamelCase struct{}

func (IsCamelCase) Validate added in v1.3.0

func (c IsCamelCase) Validate(value any) error

type IsEven added in v1.3.0

type IsEven struct{}

func (IsEven) Validate added in v1.3.0

func (c IsEven) Validate(value any) error

type IsFalse

type IsFalse struct{}

func (IsFalse) Validate

func (c IsFalse) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsFalse{}
errs := validator.Validate(value, con)

type IsFloat added in v1.0.2

type IsFloat struct{}

func (IsFloat) Validate added in v1.0.2

func (c IsFloat) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsFloat{}
errs := validator.Validate(value, con)

type IsFloat32 added in v1.0.2

type IsFloat32 struct{}

func (IsFloat32) Validate added in v1.0.2

func (c IsFloat32) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsFloat32{}
errs := validator.Validate(value, con)

type IsFloat64 added in v1.0.2

type IsFloat64 struct{}

func (IsFloat64) Validate added in v1.0.2

func (c IsFloat64) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsFloat64{}
errs := validator.Validate(value, con)

type IsHSLColor added in v1.3.0

type IsHSLColor struct{}

func (IsHSLColor) Validate added in v1.3.0

func (c IsHSLColor) Validate(value any) error

type IsHex added in v1.3.0

type IsHex struct{}

func (IsHex) Validate added in v1.3.0

func (c IsHex) Validate(value any) error

type IsHexColor added in v1.3.0

type IsHexColor struct{}

func (IsHexColor) Validate added in v1.3.0

func (c IsHexColor) Validate(value any) error

type IsIPv4 added in v1.2.0

type IsIPv4 struct{}

func (IsIPv4) Validate added in v1.2.0

func (c IsIPv4) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsIPv4{}
errs := validator.Validate(value, con)

type IsIPv6 added in v1.2.0

type IsIPv6 struct{}

func (IsIPv6) Validate added in v1.2.0

func (c IsIPv6) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsIPv6{}
errs := validator.Validate(value, con)

type IsInt added in v1.0.2

type IsInt struct{}

func (IsInt) Validate added in v1.0.2

func (c IsInt) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsInt{}
errs := validator.Validate(value, con)

type IsInt16 added in v1.0.2

type IsInt16 struct{}

func (IsInt16) Validate added in v1.0.2

func (c IsInt16) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsInt16{}
errs := validator.Validate(value, con)

type IsInt32 added in v1.0.2

type IsInt32 struct{}

func (IsInt32) Validate added in v1.0.2

func (c IsInt32) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsInt32{}
errs := validator.Validate(value, con)

type IsInt64 added in v1.0.2

type IsInt64 struct{}

func (IsInt64) Validate added in v1.0.2

func (c IsInt64) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsInt64{}
errs := validator.Validate(value, con)

type IsInt8 added in v1.0.2

type IsInt8 struct{}

func (IsInt8) Validate added in v1.0.2

func (c IsInt8) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsInt8{}
errs := validator.Validate(value, con)

type IsJSON added in v1.3.0

type IsJSON struct{}

func (IsJSON) Validate added in v1.3.0

func (c IsJSON) Validate(value any) error

type IsJWT added in v1.3.0

type IsJWT struct{}

func (IsJWT) Validate added in v1.3.0

func (c IsJWT) Validate(value any) error

type IsLowercase added in v1.3.0

type IsLowercase struct{}

func (IsLowercase) Validate added in v1.3.0

func (c IsLowercase) Validate(value any) error

type IsMAC added in v1.2.0

type IsMAC struct{}

func (IsMAC) Validate added in v1.2.0

func (c IsMAC) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsMAC{}
errs := validator.Validate(value, con)

type IsMultipleOf added in v1.3.0

type IsMultipleOf struct {
	Divisor int
}

func (IsMultipleOf) Validate added in v1.3.0

func (c IsMultipleOf) Validate(value any) error

type IsOdd added in v1.3.0

type IsOdd struct{}

func (IsOdd) Validate added in v1.3.0

func (c IsOdd) Validate(value any) error

type IsPascalCase added in v1.3.0

type IsPascalCase struct{}

func (IsPascalCase) Validate added in v1.3.0

func (c IsPascalCase) Validate(value any) error

type IsRGBColor added in v1.3.0

type IsRGBColor struct{}

func (IsRGBColor) Validate added in v1.3.0

func (c IsRGBColor) Validate(value any) error

type IsSHA256 added in v1.3.0

type IsSHA256 struct{}

func (IsSHA256) Validate added in v1.3.0

func (c IsSHA256) Validate(value any) error

type IsString added in v1.0.2

type IsString struct{}

func (IsString) Validate added in v1.0.2

func (c IsString) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsString{}
errs := validator.Validate(value, con)

type IsStruct added in v1.0.2

type IsStruct struct{}

func (IsStruct) Validate added in v1.0.2

func (c IsStruct) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsStruct{}
errs := validator.Validate(value, con)

type IsTitleCase added in v1.3.0

type IsTitleCase struct{}

func (IsTitleCase) Validate added in v1.3.0

func (c IsTitleCase) Validate(value any) error

type IsTrue

type IsTrue struct{}

func (IsTrue) Validate

func (c IsTrue) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsTrue{}
errs := validator.Validate(value, con)

type IsUUID added in v1.3.0

type IsUUID struct {
	Version int
}

func (IsUUID) Validate added in v1.3.0

func (c IsUUID) Validate(value any) error

type IsUUIDv1 added in v1.3.0

type IsUUIDv1 struct{}

func (IsUUIDv1) Validate added in v1.3.0

func (c IsUUIDv1) Validate(v any) error

type IsUUIDv2 added in v1.3.0

type IsUUIDv2 struct{}

func (IsUUIDv2) Validate added in v1.3.0

func (c IsUUIDv2) Validate(v any) error

type IsUUIDv3 added in v1.3.0

type IsUUIDv3 struct{}

func (IsUUIDv3) Validate added in v1.3.0

func (c IsUUIDv3) Validate(v any) error

type IsUUIDv4 added in v1.3.0

type IsUUIDv4 struct{}

func (IsUUIDv4) Validate added in v1.3.0

func (c IsUUIDv4) Validate(v any) error

type IsUUIDv5 added in v1.3.0

type IsUUIDv5 struct{}

func (IsUUIDv5) Validate added in v1.3.0

func (c IsUUIDv5) Validate(v any) error

type IsUint added in v1.0.2

type IsUint struct{}

func (IsUint) Validate added in v1.0.2

func (c IsUint) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsUint{}
errs := validator.Validate(value, con)

type IsUint16 added in v1.0.2

type IsUint16 struct{}

func (IsUint16) Validate added in v1.0.2

func (c IsUint16) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsUint16{}
errs := validator.Validate(value, con)

type IsUint32 added in v1.0.2

type IsUint32 struct{}

func (IsUint32) Validate added in v1.0.2

func (c IsUint32) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsUint32{}
errs := validator.Validate(value, con)

type IsUint64 added in v1.0.2

type IsUint64 struct{}

func (IsUint64) Validate added in v1.0.2

func (c IsUint64) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsUint64{}
errs := validator.Validate(value, con)

type IsUint8 added in v1.0.2

type IsUint8 struct{}

func (IsUint8) Validate added in v1.0.2

func (c IsUint8) Validate(value any) error

Validate function for validate value

Example:

con := constraints.IsUint8{}
errs := validator.Validate(value, con)

type IsUppercase added in v1.3.0

type IsUppercase struct{}

func (IsUppercase) Validate added in v1.3.0

func (c IsUppercase) Validate(value any) error

type Language

type Language struct{}

func (Language) Validate

func (c Language) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Language{}
value := "en"
errs := validator.Validate(value, con)

type LanguageWithRegion

type LanguageWithRegion struct{}

func (LanguageWithRegion) Validate

func (c LanguageWithRegion) Validate(value any) error

Validate function for validate value

con := constraints.Language{}
value := "en-US"
errs := validator.Validate(value, con)

type Length added in v1.0.6

type Length struct {
	Min int
	Max int
}

func (Length) Validate added in v1.0.6

func (c Length) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Length{
	Max: 4,
}

value := map[string]int{"a": 1, "b": 2, "c": 3}
errs := validator.Validate(value, v)

 // Example 2

con := constraints.Length{
	Min: 4,
}

value := "hello"
errs := validator.Validate(value, v)

type LessOrEqual added in v1.0.5

type LessOrEqual struct {
	Value float64
}

func (LessOrEqual) Validate added in v1.0.5

func (r LessOrEqual) Validate(value any) error

Validate function for validate value

Example:

con := constraints.LessOrEqual{
	Value: 35
}
errs := validator.Validate(value, con)

type LessThen added in v1.0.5

type LessThen struct {
	Value float64
}

func (LessThen) Validate added in v1.0.5

func (r LessThen) Validate(value any) error

Validate function for validate value

Example:

con := constraints.LessThen{
	Value: 35
}
errs := validator.Validate(value, con)

type Nil added in v1.0.2

type Nil struct{}

func (Nil) Validate added in v1.0.2

func (c Nil) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Nil{}
errs := validator.Validate(value, con)

type NotBlank

type NotBlank struct{}

func (NotBlank) Validate

func (c NotBlank) Validate(value any) error

Validate function for validate value

Example:

con := constraints.NotBlank{}
errs := validator.Validate(value, con)

type NotNil added in v1.0.2

type NotNil struct{}

func (NotNil) Validate added in v1.0.2

func (c NotNil) Validate(value any) error

Validate function for validate value

Example:

con := constraints.NotNil{}
errs := validator.Validate(value, con)

type Options added in v1.0.3

type Options struct {
	Options []any
}

func (Options) Validate added in v1.0.3

func (c Options) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Options{
	Options: []any{"option1", "option2", "option3", 42},
}

value := "option1"
errs := validator.Validate(value, con)

type PhoneNumber added in v1.0.6

type PhoneNumber struct {
	Format     string // Format string, e.g., "(00420) 000 000 000"
	AllowEmpty bool   // Allow empty values
}

func (PhoneNumber) Validate added in v1.0.6

func (c PhoneNumber) Validate(value any) error

Validate function for validate value

Example:

con := constraints.PhoneNumber{Format: "(00420) 000 000 000"}

value := "(00420) 123 456 789"
errs := validator.Validate(value, v)

type Range added in v1.0.5

type Range struct {
	Min float64
	Max float64
}

func (Range) Validate added in v1.0.5

func (r Range) Validate(value any) error

Validate function for validate value

Example:

con := constrains.Range{
	Min: 5,
	Max: 20,
}

value := 7
errs := validator.Validate(value, con)

type RegularExpression added in v1.0.2

type RegularExpression struct {
	Regexp string
}

func (RegularExpression) Validate added in v1.0.2

func (c RegularExpression) Validate(value any) error

Validate function for validate value

Example:

regExp := `^[a-z]{3}$`
con := constraints.RegularExpression{
	Regexp: regExp,
}
errs := validator.Validate(value, con)

type Required

type Required struct{}

func (Required) Validate

func (c Required) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Required{}
errs := validator.Validate(value, con)

type Struct added in v1.0.2

type Struct struct {
	Struct interface{}
}

func (Struct) Validate added in v1.0.2

func (c Struct) Validate(value any) error

Validate function for validate value

Example:

con := constraints.Struct{
	Struct: MyStruct{},
}

errs := validator.Validate(value, con)

type Url added in v1.0.5

type Url struct {
	AllowEmpty     bool
	AllowedSchemes []string
}

func (Url) Validate added in v1.0.5

func (c Url) Validate(value any) error

Validate function for validate value

Example:

v := constraints.Url{
	AllowEmpty:     false,
	AllowedSchemes: []string{"http", "https"},
}

value := "https://example.com"
errs := validator.Validate(value, v)

Jump to

Keyboard shortcuts

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