validation

package module
v0.0.0-...-a12c1c9 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2025 License: MIT Imports: 9 Imported by: 0

README

validation

Go Reference Go Go Report Card codecov

Package validation provides a validation library for Go.

Installation

go get -u -v github.com/gopi-frame/validation

Import

import "github.com/gopi-frame/validation"

Usage

Quick Start

package main

import (
    "context"
    "github.com/gopi-frame/validation"
)

var user = struct {
    Name string
    Age int
}{
    Name: "gopi",
    Age: 25,
}

func main() {
    validated := validation.Validate(
        context.Background(),
        validation.NotBlank("name", user.Name),
        validation.NotBlank("age", user.Age),
        validation.GreaterThan("age", user.Age, 18),
    )
    if validated.Fails() {
        fmt.Println(validated.GetMessages())
    }    
}

Built-in common validator builders:

  • Generic type builders:

    • validation.NotBlank[T comparable] validates if the value is not blank
    • validation.Blank[T comparable] validates if the value is blank
    • validation.In[T comparable] validates if the value is in the given list
    • validation.NotIn[T comparable] validates if the value is not in the given list
    • validation.EqualTo[T comparable] validates if the value is equal to the given value
    • validation.NotEqualTo[T comparable] validates if the value is not equal to the given value
    • validation.LessThan[T constraints.Ordered] validates if the value is less than the given value
    • validation.LessThanOrEqualTo[T constraints.Ordered] validates if the value is less than or equal to the given value
    • validation.GreaterThan[T constraints.Ordered] validates if the value is greater than the given value
    • validation.GreaterThanOrEqualTo[T constraints.Ordered] validates if the value is greater than or equal to the given value
  • String builders:

    • validation.Length validates if the length of the value is equal to the given value
    • validation.MinLength validates if the length of the value is greater than or equal to the given value
    • validation.MaxLength validates if the length of the value is less than or equal to the given value
    • validation.StartsWith validates if the value starts with the given value
    • validation.StartsWithAny validates if the value starts with any of the given values
    • validation.EndsWith validates if the value ends with the given value
    • validation.EndsWithAny validates if the value ends with any of the given values
    • validation.NotStartsWith validates if the value does not start with the given value
    • validation.NotStartsWithAny validates if the value does not start with any of the given values
    • validation.NotEndsWith validates if the value does not end with the given value
    • validation.NotEndsWithAny validates if the value does not end with any of the given values
    • validation.Match validates if the value matches the given regex
    • validation.NotMatch validates if the value does not match the given regex
    • validation.Contains validates if the value contains the given value
    • validation.NotContains validates if the value does not contain the given value
    • validation.Upper validates if the value is uppercase
    • validation.Lower validates if the value is lowercase
    • validation.Alpha validates if the value is alphabetic
    • validation.AlphaNumeric validates if the value is alphanumeric
    • validation.AlphaDash validates if the value is alphanumeric with dashes(_-)
    • valication.Ascii validates if the value only contains ascii characters
    • validation.AsciiNumeric validates if the value only contains ascii characters and numbers(0-9)
    • validation.AsciiDash validates if the value only contains ascii characters and numbers(0-9) and dashes(_-)
    • validation.Number validates if the value is a number
    • validation.PositiveNumber validates if the value is a positive number
    • validation.NegativeNumber validates if the value is a negative number
    • validation.Integer validates if the value is an integer
    • validation.PositiveInteger validates if the value is a positive integer
    • validation.NegativeInteger validates if the value is a negative integer
    • validation.Decimal validates if the value is a decimal
    • validation.Binary validates if the value is a binary
    • validation.Octal validates if the value is an octal
    • validation.Hexadecimal validates if the value is a hexadecimal
  • Time string builders:

    • validation.Time validates if the value is a time in given format
    • validation.ANSIC validates if the value is a time in ANSIC format
    • validation.UnixDate validates if the value is a time in UnixDate format
    • validation.RubyDate validates if the value is a time in RubyDate format
    • validation.RFC822 validates if the value is a time in RFC822 format
    • validation.RFC822Z validates if the value is a time in RFC822Z format
    • validation.RFC850 validates if the value is a time in RFC850 format
    • validation.RFC1123 validates if the value is a time in RFC1123 format
    • validation.RFC1123Z validates if the value is a time in RFC1123Z format
    • validation.RFC3339 validates if the value is a time in RFC3339 format
    • validation.RFC3339Nano validates if the value is a time in RFC3339Nano format
    • validation.Kitchen validates if the value is a time in Kitchen format
    • validation.Stamp validates if the value is a time in Stamp format
    • validation.StampMilli validates if the value is a time in StampMilli format
    • validation.StampMicro validates if the value is a time in StampMicro format
    • validation.StampNano validates if the value is a time in StampNano format
    • validation.DateTime validates if the value is a time in DateTime format
    • validation.DateOnly validates if the value is a date in DateOnly format
    • validation.TimeOnly validates if the value is a time in TimeOnly format
    • validation.Duration validates if the value is a duration
    • validation.Timezone validates if the value is a timezone
    • validation.Before validates if the time string is before the given time string
    • validation.After validates if the time string is after the given time string
    • validation.BeforeOrEqual validates if the time string is before or equal to the given time string
    • validation.AfterOrEqual validates if the time string is after or equal to the given time string
    • validation.BeforeTZ validates if the time string is before the given time string in the given timezone
    • validation.AfterTZ validates if the time string is after the given time string in the given timezone
    • validation.BeforeOrEqualTZ validates if the time string is before or equal to the given time string in the given timezone
    • validation.AfterOrEqualTZ validates if the time string is after or equal to the given time string in the given
  • Network string builders:

    • validation.IP validates if the value is a valid IP address
    • validation.IPV4 validates if the value is a valid IPV4 address
    • validation.IPV6 validates if the value is a valid IPV6 address
    • validation.URL validates if the value is a valid URL
    • validation.URLWithScheme validates if the value is a valid URL with given scheme
    • validation.RequestURI validates if the value is a valid request URI
    • validation.URLQuery validates if the value is a valid URL query
  • Data string builders:

    • validation.JSON validates if the value is a valid JSON
    • validation.JSONArray validates if the value is a valid JSON array
    • validation.JSONObject validates if the value is a valid JSON object
    • validation.JSONString validates if the value is a valid JSON string
    • validation.UUID validates if the value is a valid UUID
    • validation.UUIDv1 validates if the value is a valid version-1 UUID
    • validation.UUIDv2 validates if the value is a valid version-2 UUID
    • validation.UUIDv3 validates if the value is a valid version-3 UUID
    • validation.UUIDv4 validates if the value is a valid version-4 UUID
    • validation.UUIDv5 validates if the value is a valid version-5 UUID
    • validation.ULID validates if the value is a valid ULID
    • validation.Base64 validates if the value is a valid Base64 encoded string
    • validation.Base32 validates if the value is a valid Base32 encoded string
  • Map builders:

    • validation.ContainsKey validates if the map contains the given key
    • validation.ContainsValue validates if the map contains the given value
  • Slice builders:

    • validation.Includes validates if the slice contains the given values
    • validation.Excludes validates if the slice does not contain the given values
    • validation.Unique validates if the slice contains unique values
    • validation.Count validates if the slice contains the given number of values
    • validation.MinCount validates if the slice contains at least the given number of values
    • validation.MaxCount validates if the slice contains at most the given number of values
  • Group builders:

    • validation.Group[T any] validates if the given values are valid according to the given rules
    • validation.If[T any] validates if the given value is valid according to the given rules when the given condition is true
    • validation.Each[T any] validates if the given values' each element is valid according to the given rules when the given condition

Custom Validator

package main

import (
  "context"
  "fmt"
  "github.com/gopi-frame/validation"
  "github.com/gopi-frame/validation/code"
)

func main() {
  validator := validation.NewValidator(
    validation.WithMessages(map[string]string{
      code.IsNotBlank: "{{.attribute}} is required",
    }), // replace the default messages with custom messages
  )
  // replace the default validator with the custom validator
  validation.SetDefaultValidator(validator)
  validated := validation.Validate(
    context.Background(),
    validation.NotBlank("name", "gopi"),
    validation.NotBlank("age", 25),
    validation.GreaterThan("age", 25, 18),
  )
  if validated.Fails() {
    fmt.Println(validated.GetMessages())
  }
}

Set Custom Error Messages Temporarily

package main

import (
    "context"
    "github.com/gopi-frame/validation"
    "github.com/gopi-frame/validation/code"
)

var user = struct {
    Name string
    Age int
}{
    Name: "gopi",
    Age: 25,
}

func main() {
    validated := validation.Validate(
        context.Background(),
        validation.NotBlank("name", user.Name),
        validation.NotBlank("age", user.Age),
        validation.GreaterThan("age", user.Age, 18),
    ).SetMessages(map[string]map[string]string{
        "name": {
            code.NotBlank: "Name is required",
        },
        "age": {
            code.NotBlank:    "Age is required",
            code.GreaterThan: "Age must be greater than 18",
        },
    })
    if validated.Fails() {
        fmt.Println(validated.GetMessages())
    }
}

Conditional Validation

Conditional validation is a way to validate a value against multiple rules based on a condition. If the value is an implementation of the validation.Validatable interface, the Validate method of the value will be called to validate the value first.

package main

import (
    "context"
    "github.com/gopi-frame/validation"
)

var user = struct {
    Name string
    Age int
    Gender string
}{
    Name: "gopi",
    Age: 25,
    Gender: "male",
}

func main() {
    var femaleOnly = true
    validated := validation.Validate(
        context.Background(),
        validation.NotBlank("name", user.Name),
        validation.NotBlank("age", user.Age),
        validation.GreaterThan("age", user.Age, 18),
        validation.If(
            femaleOnly,
            validation.EqualTo("gender", user.Gender, "female"),
        ),
    )
}

Group Validation

Group validation is a way to validate a value against multiple rules. If the value is an implementation of the validation.Validatable interface, the Validate method of the value will be called to validate the value first.

package main

import (
    "context"
    "github.com/gopi-frame/validation"
    "github.com/gopi-frame/validation/validator"
)

var user = struct {
    Name string
    Age  int
}{
    Name: "gopi",
    Age:  25,
}

func main() {
    validated := validation.Validate(
        context.Background(),
        validation.Group("Name", user.Name, validator.IsNotBlank[string]()),
        validation.Group("Age", user.Age, validator.IsNotBlank[int](), validator.IsGreaterThan[int](18)),
    )
    if validated.Fails() {
        fmt.Println(validated.GetMessages())
    }
}

Custom Validatable Type

package main

import (
    "context"
    vc "github.com/gopi-frame/contract/validation"
    "github.com/gopi-frame/validation"
    "github.com/gopi-frame/validation/error"
    "github.com/gopi-frame/validation/validator"
)

type User struct {
    Name string
    Age  int
}

func (u *User) Validate(ctx context.Context, _ vc.ErrorBuilder) vc.Error {
    var errs = error.NewBag()
    if validated := validation.Value(ctx, u.Name, validator.IsNotBlank[string]()); validated.Fails() {
        errs.AddError("Name", validated)
    }
    if validated := validation.Value(
        ctx, 
        u.Age, 
        validator.IsNotBlank[int](), 
        validator.IsGreaterThanOrEqualTo(18),
    ); validated.Fails() {
        errs.AddError("Age", validated)
    }
    return errs
}

func main() {
    var user = &User{
        Name: "gopi",
        Age:  25,
    }
    validated := validation.Attribute(context.Context(), "User", user)
    if validated.Fails() {
        fmt.Println(validated.GetMessages())
    }
}

List Validation

package main

import (
    "context"
    "github.com/gopi-frame/validation"
    "github.com/gopi-frame/validation/validator"
)


type User struct {
    Name string
    Age  int
}

func (u *User) Validate(ctx context.Context, _ vc.ErrorBuilder) vc.Error {
    var errs = error.NewBag()
    if validated := validation.Value(ctx, u.Name, validator.IsNotBlank[string]()); validated.Fails() {
        errs.AddError("Name", validated)
    }
    if validated := validation.Value(
        ctx,
        u.Age,
        validator.IsNotBlank[int](),
        validator.IsGreaterThanOrEqualTo(18),
    ); validated.Fails() {
        errs.AddError("Age", validated)
    }
    return errs
}

var users = []User{
    {"gopi", 25},
    {"john", 30},
    {"jane", 28},
}

func main() {
    validated := validation.Validate(
        context.Background(),
        validation.Each("age", users),
    )
}

Translation

Register New Translation

package main

import (
  "context"
  "fmt"
  "github.com/gopi-frame/validation"
  "github.com/gopi-frame/validation/code"
  "github.com/gopi-frame/validation/translator"
)

func main() {
  translator.RegisterTranslation("zh-CN", map[string]string{
    code.IsNotBlank: "{{.attribute}}不能为空",
  })
  validated := validation.Validate(
    validation.BindLanguage(context.Background(), "zh-CN"),
    validation.NotBlank("name", ""),
    validation.GreaterThan("age", 14, 18),
  )
  if validated.Fails() {
    fmt.Println(validated.GetErrors("name").Get(code.IsNotBlank).Error()) // name不能为空
    fmt.Println(validated.GetErrors("age").Get(code.GreaterThan).Error()) // age should be greater than 18.
  }
}

Register Custom Translator

Implement the translator.Translator interface and register the translator by fallowing way.

validator := validation.NewValidator(
    validation.WithTranslator(new(MyTranslator)),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ANSIC

func ANSIC(attribute string, value string) validation.ValidatorBuilder

func After

func After(attribute string, value string, layout string, other time.Time) validation.ValidatorBuilder

func AfterOrEqualTo

func AfterOrEqualTo(attribute string, value string, layout string, other time.Time) validation.ValidatorBuilder

func AfterOrEqualToTZ

func AfterOrEqualToTZ(attribute string, value string, layout string, tz *time.Location, other time.Time) validation.ValidatorBuilder

func AfterTZ

func AfterTZ(attribute string, value string, layout string, tz *time.Location, other time.Time) validation.ValidatorBuilder

func Alpha

func Alpha(attribute string, value string) validation.ValidatorBuilder

Alpha returns a builder function to check if a string contains only alphabetic characters.

func AlphaDash

func AlphaDash(attribute string, value string) validation.ValidatorBuilder

AlphaDash returns a builder function to check if a string contains only alphanumeric characters and dashes.

func AlphaNumeric

func AlphaNumeric(attribute string, value string) validation.ValidatorBuilder

AlphaNumeric returns a builder function to check if a string contains only alphanumeric characters.

func Ascii

func Ascii(attribute string, value string) validation.ValidatorBuilder

Ascii returns a builder function to check if a string contains only ASCII characters.

func AsciiDash

func AsciiDash(attribute string, value string) validation.ValidatorBuilder

AsciiDash returns a builder function to check if a string contains only ASCII characters and dashes.

func AsciiNumeric

func AsciiNumeric(attribute string, value string) validation.ValidatorBuilder

AsciiNumeric returns a builder function to check if a string contains only ASCII characters and numbers.

func Base32

func Base32(attribute string, s string) validation.ValidatorBuilder

func Base64

func Base64(attribute string, s string) validation.ValidatorBuilder

func Before

func Before(attribute string, value string, layout string, other time.Time) validation.ValidatorBuilder

func BeforeOrEqualTo

func BeforeOrEqualTo(attribute string, value string, layout string, other time.Time) validation.ValidatorBuilder

func BeforeOrEqualToTZ

func BeforeOrEqualToTZ(attribute string, value string, layout string, tz *time.Location, other time.Time) validation.ValidatorBuilder

func BeforeTZ

func BeforeTZ(attribute string, value string, layout string, tz *time.Location, other time.Time) validation.ValidatorBuilder

func Binary

func Binary(attribute string, value string) validation.ValidatorBuilder

Binary checks if the string is a binary.

func BindLanguage

func BindLanguage(ctx context.Context, language string) context.Context

BindLanguage binds language to context. It is useful when you want to change the language temporarily.

func Blank

func Blank[T comparable](attribute string, value T) validation.ValidatorBuilder

Blank returns a builder function that validates the given value is blank.

func Contains

func Contains(attribute string, value string, substring string) validation.ValidatorBuilder

Contains returns a builder function to check if a string contains a substring.

func ContainsKey

func ContainsKey[K comparable, V any](attribute string, m map[K]V, key K) validation.ValidatorBuilder

func Count

func Count[T comparable](attribute string, s []T, count int) validation.ValidatorBuilder

func DateOnly

func DateOnly(attribute string, value string) validation.ValidatorBuilder

func DateTime

func DateTime(attribute string, value string) validation.ValidatorBuilder

func Decimal

func Decimal(attribute string, value string) validation.ValidatorBuilder

Decimal if the string is a decimal.

func Duration

func Duration(attribute string, value string) validation.ValidatorBuilder

func Each

func Each[T any](attribute string, values []T, rules ...validation.Rule[T]) validation.ValidatorBuilder

func EndsWith

func EndsWith(attribute string, value string, suffix string) validation.ValidatorBuilder

EndsWith returns a builder function to check if a string ends with a suffix.

func EndsWithAny

func EndsWithAny(attribute string, value string, suffixes ...string) validation.ValidatorBuilder

EndsWithAny returns a builder function to check if a string ends with any of the given suffixes.

func EqualTo

func EqualTo[T comparable](attribute string, value T, other T) validation.ValidatorBuilder

func Excludes

func Excludes[T comparable](attribute string, s []T, elements ...T) validation.ValidatorBuilder

func GreaterThan

func GreaterThan[T cmp.Ordered](attribute string, value T, other T) validation.ValidatorBuilder

func GreaterThanOrEqualTo

func GreaterThanOrEqualTo[T cmp.Ordered](attribute string, value T, other T) validation.ValidatorBuilder

func Group

func Group[T any](attribute string, value T, rules ...validation.Rule[T]) validation.ValidatorBuilder

Group returns a validator builder that validates the given value using the given rules. If the value is an implementation of the validation.Validatable interface, it will be validated first before the rules.

func Hexadecimal

func Hexadecimal(attribute string, value string) validation.ValidatorBuilder

Hexadecimal checks if the string is a hexadecimal.

func IP

func IP(attribute string, value string) validation.ValidatorBuilder

func IPv4

func IPv4(attribute string, value string) validation.ValidatorBuilder

func IPv6

func IPv6(attribute string, value string) validation.ValidatorBuilder

func If

func If[T any](condition bool, attribute string, value T, rules ...validation.Rule[T]) validation.ValidatorBuilder

If returns a validator builder that validates the given value using the given rules if the condition is true. if the value is an implementation of the validation.Validatable interface, it will be validated first before the rules.

func In

func In[T comparable](attribute string, value T, values ...T) validation.ValidatorBuilder

func Includes

func Includes[T comparable](attribute string, s []T, elements ...T) validation.ValidatorBuilder

func Integer

func Integer(attribute string, value string) validation.ValidatorBuilder

Integer checks if the string is an integer.

func IsEnum

func IsEnum[T enum.Enum](attribute string, value T) validation.ValidatorBuilder

func IsEnumString

func IsEnumString[T enum.Enum](attribute string, value string) validation.ValidatorBuilder

func JSON

func JSON(attribute string, s string) validation.ValidatorBuilder

func JSONArray

func JSONArray(attribute string, s string) validation.ValidatorBuilder

func JSONObject

func JSONObject(attribute string, s string) validation.ValidatorBuilder

func JSONString

func JSONString(attribute string, s string) validation.ValidatorBuilder

func Kitchen

func Kitchen(attribute string, value string) validation.ValidatorBuilder

func LanguageFromContext

func LanguageFromContext(ctx context.Context) string

LanguageFromContext returns language from context.

func Length

func Length(attribute string, value string, length int) validation.ValidatorBuilder

Length returns a builder function to check if a string has a specific length.

func LessThan

func LessThan[T cmp.Ordered](attribute string, value T, other T) validation.ValidatorBuilder

func LessThanOrEqualTo

func LessThanOrEqualTo[T cmp.Ordered](attribute string, value T, other T) validation.ValidatorBuilder

func Lower

func Lower(attribute string, value string) validation.ValidatorBuilder

Lower returns a builder function to check if a string is lowercase.

func Match

func Match(attribute string, value string, pattern string) validation.ValidatorBuilder

Match returns a builder function to check if a string matches a regular expression pattern.

func MaxCount

func MaxCount[T comparable](attribute string, s []T, count int) validation.ValidatorBuilder

func MaxLength

func MaxLength(attribute string, value string, length int) validation.ValidatorBuilder

MaxLength returns a builder function to check if a string has a maximum length.

func MinCount

func MinCount[T comparable](attribute string, s []T, count int) validation.ValidatorBuilder

func MinLength

func MinLength(attribute string, value string, length int) validation.ValidatorBuilder

MinLength returns a builder function to check if a string has a minimum length.

func NegativeInteger

func NegativeInteger(attribute string, value string) validation.ValidatorBuilder

NegativeInteger checks if the string is a negative integer.

func NegativeNumber

func NegativeNumber(attribute string, value string) validation.ValidatorBuilder

NegativeNumber checks if the string is a negative number.

func NotBlank

func NotBlank[T comparable](attribute string, value T) validation.ValidatorBuilder

NotBlank returns a builder function that validates the given value is not blank.

func NotContains

func NotContains(attribute string, value string, substring string) validation.ValidatorBuilder

NotContains returns a builder function to check if a string does not contain a substring.

func NotContainsKey

func NotContainsKey[K comparable, V any](attribute string, m map[K]V, key K) validation.ValidatorBuilder

func NotEndsWith

func NotEndsWith(attribute string, value string, suffix string) validation.ValidatorBuilder

NotEndsWith returns a builder function to check if a string does not end with a suffix.

func NotEndsWithAny

func NotEndsWithAny(attribute string, value string, suffixes ...string) validation.ValidatorBuilder

NotEndsWithAny returns a builder function to check if a string does not end with any of the given suffixes.

func NotEqualTo

func NotEqualTo[T comparable](attribute string, value T, other T) validation.ValidatorBuilder

func NotIn

func NotIn[T comparable](attribute string, value T, values ...T) validation.ValidatorBuilder

func NotMatch

func NotMatch(attribute string, value string, pattern string) validation.ValidatorBuilder

NotMatch returns a builder function to check if a string does not match a regular expression pattern.

func NotStartsWith

func NotStartsWith(attribute string, value string, prefix string) validation.ValidatorBuilder

NotStartsWith returns a builder function to check if a string does not start with a prefix.

func NotStartsWithAny

func NotStartsWithAny(attribute string, value string, prefixes ...string) validation.ValidatorBuilder

NotStartsWithAny returns a builder function to check if a string does not start with any of the given prefixes.

func Number

func Number(attribute string, value string) validation.ValidatorBuilder

Number checks if the string is a number.

func Octal

func Octal(attribute string, value string) validation.ValidatorBuilder

Octal checks if the string is an octal.

func PathAbsolute

func PathAbsolute(attribute string, value string) validation.ValidatorBuilder

func PathDir

func PathDir(attribute string, value string) validation.ValidatorBuilder

func PathExists

func PathExists(attribute string, value string) validation.ValidatorBuilder

func PathFile

func PathFile(attribute string, value string) validation.ValidatorBuilder

func PathNotExists

func PathNotExists(attribute string, value string) validation.ValidatorBuilder

func PathRelative

func PathRelative(attribute string, value string) validation.ValidatorBuilder

func PositiveInteger

func PositiveInteger(attribute string, value string) validation.ValidatorBuilder

PositiveInteger checks if the string is a positive integer.

func PositiveNumber

func PositiveNumber(attribute string, value string) validation.ValidatorBuilder

PositiveNumber checks if the string is a positive number.

func RFC1123

func RFC1123(attribute string, value string) validation.ValidatorBuilder

func RFC1123Z

func RFC1123Z(attribute string, value string) validation.ValidatorBuilder

func RFC3339

func RFC3339(attribute string, value string) validation.ValidatorBuilder

func RFC3339Nano

func RFC3339Nano(attribute string, value string) validation.ValidatorBuilder

func RFC822

func RFC822(attribute string, value string) validation.ValidatorBuilder

func RFC822Z

func RFC822Z(attribute string, value string) validation.ValidatorBuilder

func RFC850

func RFC850(attribute string, value string) validation.ValidatorBuilder

func RequestURI

func RequestURI(attribute string, value string) validation.ValidatorBuilder

func RubyDate

func RubyDate(attribute string, value string) validation.ValidatorBuilder

func Stamp

func Stamp(attribute string, value string) validation.ValidatorBuilder

func StampMicro

func StampMicro(attribute string, value string) validation.ValidatorBuilder

func StampMilli

func StampMilli(attribute string, value string) validation.ValidatorBuilder

func StampNano

func StampNano(attribute string, value string) validation.ValidatorBuilder

func StartsWith

func StartsWith(attribute string, value string, prefix string) validation.ValidatorBuilder

StartsWith returns a builder function to check if a string starts with a prefix.

func StartsWithAny

func StartsWithAny(attribute string, value string, prefixes ...string) validation.ValidatorBuilder

StartsWithAny returns a builder function to check if a string starts with any of the given prefixes.

func Time

func Time(attribute string, value string, layout string) validation.ValidatorBuilder

func TimeOnly

func TimeOnly(attribute string, value string) validation.ValidatorBuilder

func Timezone

func Timezone(attribute string, value string) validation.ValidatorBuilder

func ULID

func ULID(attribute string, s string) validation.ValidatorBuilder

func URL

func URL(attribute string, value string) validation.ValidatorBuilder

func URLQuery

func URLQuery(attribute string, value string) validation.ValidatorBuilder

func URLWithScheme

func URLWithScheme(attribute string, value string, scheme string) validation.ValidatorBuilder

func UUID

func UUID(attribute string, s string) validation.ValidatorBuilder

func UUIDv1

func UUIDv1(attribute string, s string) validation.ValidatorBuilder

func UUIDv2

func UUIDv2(attribute string, s string) validation.ValidatorBuilder

func UUIDv3

func UUIDv3(attribute string, s string) validation.ValidatorBuilder

func UUIDv4

func UUIDv4(attribute string, s string) validation.ValidatorBuilder

func UUIDv5

func UUIDv5(attribute string, s string) validation.ValidatorBuilder

func Unique

func Unique[T comparable](attribute string, s []T) validation.ValidatorBuilder

func UnixDate

func UnixDate(attribute string, value string) validation.ValidatorBuilder

func Upper

func Upper(attribute string, value string) validation.ValidatorBuilder

Upper returns a builder function to check if a string is uppercase.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder(validator validation.Validatable) *Builder

func (*Builder) Build

func (b *Builder) Build(ctx validation.ValidatorContext)

func (*Builder) GetAttribute

func (b *Builder) GetAttribute() string

func (*Builder) GetKey

func (b *Builder) GetKey() []string

func (*Builder) SetAttribute

func (b *Builder) SetAttribute(attribute string) validation.ValidatorBuilder

func (*Builder) SetKey

func (b *Builder) SetKey(paths ...string) validation.ValidatorBuilder

type Option

type Option func(v *Validator) error

func WithDefaultLanguage

func WithDefaultLanguage(language string) Option

func WithErrorBuilder

func WithErrorBuilder(builder validation.ErrorBuilder) Option

func WithMessages

func WithMessages(messages map[string]string) Option

func WithTranslator

func WithTranslator(translator validation.Translator) Option

type Path

type Path struct {
}

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

func NewValidator

func NewValidator(options ...Option) (*Validator, error)

func (*Validator) BuildError

func (v *Validator) BuildError(code string, message string, params ...validation.Param) validation.Error

func (*Validator) Validate

func (v *Validator) Validate(ctx context.Context, builders ...validation.ValidatorBuilder) validation.ErrorBag

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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