validator

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Go Validator

Go Validator is a lightweight Go library used for basic data validation and that's it.

Installation

go get github.com/nanoninja/validator

Getting started

package main

import (
    "encoding/json"
    "os"

    "github.com/nanoninja/validator"
)

func main() {
    logger := validator.NewLogger()

    logger.Add(validator.Group[string]("name", "gopher",
        validator.NotBlank{},
        validator.Alpha{},
        &validator.Length{Min: 3, Max: 20},
        validator.LowerCase{},
    ))

    if !logger.IsValid() {
        enc := json.NewEncoder(os.Stdout)
        enc.SetIndent("", "  ")
        enc.Encode(logger.Errors())
    }
}

Creating Validator

package main

import (
    "errors"
    "fmt"
)

var ErrMyCustom = errors.New("this value must be greater or equal than zero")

type MyCustom struct{}

func (MyCustom) Name() string {
    return "custom"
}

func (MyCustom) Validate(n int) error {
    if n < 0 {
        return ErrMyCustom
    }
    return nil
}

func main() {
    v := MyCustom{}

    if err := v.Validate(-10); err != nil {
        fmt.Println(err)
    }
}

List of available validators

  • Alpha
  • Alphanum
  • Blank
  • DateTime
  • IP
  • IPv4
  • IPv6
  • JSON
  • Length
  • LowerCase
  • Match
  • MaxLength
  • MinLength
  • NotBlank
  • Numeric
  • Regex
  • RequestURI
  • RequestURL
  • UpperCase
  • UUID
  • Username

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDateTime     = errors.New("this value is not a valid datetime")
	ErrDateTimeZone = errors.New("this value is not a valid timezone")
)
View Source
var (
	ErrIP   = errors.New("this value is not a valid IP")
	ErrIPv4 = errors.New("this value is not a valid IPv4")
	ErrIPv6 = errors.New("this value is not a valid IPv6")
)
View Source
var (
	ErrMatch = errors.New("this value is not valid")
	ErrRegex = errors.New("this value is not valid")
)
View Source
var (
	ErrAlpha      = errors.New("this value should be alpha")
	ErrAlphanum   = errors.New("this value should be alphanumeric")
	ErrBlank      = errors.New("this value should be blank")
	ErrEmail      = errors.New("this value is not a valid email address")
	ErrJSON       = errors.New("this value should be valid JSON")
	ErrLowerCase  = errors.New("this value should be lowercase")
	ErrNotBlank   = errors.New("this value should not be blank")
	ErrNumeric    = errors.New("this value should be numeric")
	ErrRequestURI = errors.New("this value is not a valid URI")
	ErrRequestURL = errors.New("this value is not a valid URL")
	ErrSlug       = errors.New("this value should be a valid slug")
	ErrUpperCase  = errors.New("this value should be uppercase")
	ErrUsername   = errors.New("this value is not a valid username")
	ErrUUID       = errors.New("this value is not a valid UUID")
)

Functions

func HasLetter

func HasLetter(r rune, s string) bool

func IsAlpha

func IsAlpha(s string) bool

func IsLetterAlpha

func IsLetterAlpha(r rune) bool

func IsLetterNumeric

func IsLetterNumeric(r rune) bool

func IsNumeric

func IsNumeric(s string) bool

Types

type Alpha

type Alpha struct{}

func (Alpha) Name

func (Alpha) Name() string

func (Alpha) Validate

func (Alpha) Validate(s string) error

type Alphanum

type Alphanum struct{}

func (Alphanum) Name

func (Alphanum) Name() string

func (Alphanum) Validate

func (Alphanum) Validate(s string) error

type Blank

type Blank struct{}

func (Blank) Name

func (Blank) Name() string

func (Blank) Validate

func (Blank) Validate(s string) error

type DateTime

type DateTime struct {
	Layout   string `json:"-" xml:"-"`
	Format   string `json:"format" xml:"format"`
	Timezone string `json:"timezone" xml:"timezone"`
}

func (DateTime) Name

func (DateTime) Name() string

func (*DateTime) Validate

func (v *DateTime) Validate(s string) error

type Email

type Email struct {
	Address string `json:"address" xml:"address"`
}

func (Email) Name

func (Email) Name() string

func (*Email) Validate

func (v *Email) Validate(s string) error

type Error

type Error struct {
	Message    string `json:"message"`
	Constraint string `json:"constraint"`
	Params     any    `json:"params"`
	Value      any    `json:"value"`
}

func (Error) Error

func (e Error) Error() string

type ErrorHandler

type ErrorHandler interface {
	IsValid() bool
	Errors() map[string][]error
}

type GroupHandler

type GroupHandler interface {
	Identifier
	ErrorHandler
}

func Group

func Group[T any](name string, value T, validators ...Handler[T]) GroupHandler

type Handler

type Handler[T any] interface {
	Identifier
	Validator[T]
}

type IP

type IP struct{}

func (IP) Name

func (IP) Name() string

func (IP) Validate

func (IP) Validate(s string) error

type IPv4

type IPv4 struct{}

func (IPv4) Name

func (IPv4) Name() string

func (IPv4) Validate

func (IPv4) Validate(s string) error

type IPv6

type IPv6 struct{}

func (IPv6) Name

func (IPv6) Name() string

func (IPv6) Validate

func (IPv6) Validate(s string) error

type Identifier

type Identifier interface {
	Name() string
}

type JSON

type JSON struct{}

func (JSON) Name

func (JSON) Name() string

func (JSON) Validate

func (JSON) Validate(s string) error

type Length

type Length struct {
	Length int `json:"length" xml:"length"`
	Min    int `json:"min" xml:"min"`
	Max    int `json:"max" xml:"max"`
}

func (Length) Name

func (Length) Name() string

func (*Length) Validate

func (v *Length) Validate(s string) error

type Logger

type Logger interface {
	Add(GroupHandler)
	Get(name string) GroupHandler
	Remove(name string)
	ErrorHandler
}

func NewLogger

func NewLogger() Logger

type LowerCase

type LowerCase struct{}

func (LowerCase) Name

func (LowerCase) Name() string

func (LowerCase) Validate

func (LowerCase) Validate(s string) error

type Match

type Match struct {
	Pattern string `json:"pattern" xml:"pattern"`
}

func (Match) Name

func (Match) Name() string

func (Match) Validate

func (v Match) Validate(s string) error

type MaxLength

type MaxLength struct {
	Length int `json:"length" xml:"length"`
	Max    int `json:"max" xml:"max"`
}

func (MaxLength) Name

func (MaxLength) Name() string

func (*MaxLength) Validate

func (v *MaxLength) Validate(s string) error

type MinLength

type MinLength struct {
	Length int `json:"length" xml:"length"`
	Min    int `json:"min" xml:"min"`
}

func (MinLength) Name

func (MinLength) Name() string

func (*MinLength) Validate

func (v *MinLength) Validate(s string) error

type NotBlank

type NotBlank struct{}

func (NotBlank) Name

func (NotBlank) Name() string

func (NotBlank) Validate

func (NotBlank) Validate(s string) error

type Numeric

type Numeric struct{}

func (Numeric) Name

func (Numeric) Name() string

func (Numeric) Validate

func (Numeric) Validate(s string) error

type Regex

type Regex struct {
	Pattern string         `json:"pattern" xml:"pattern"`
	Re      *regexp.Regexp `json:"-" xml:"-"`
}

func (Regex) Name

func (Regex) Name() string

func (*Regex) Validate

func (v *Regex) Validate(s string) error

type RequestURI

type RequestURI struct{}

func (RequestURI) Name

func (RequestURI) Name() string

func (RequestURI) Validate

func (RequestURI) Validate(s string) error

type RequestURL

type RequestURL struct{}

func (RequestURL) Name

func (RequestURL) Name() string

func (RequestURL) Validate

func (RequestURL) Validate(s string) error

type Slug added in v0.2.0

type Slug struct{}

func (Slug) Name added in v0.2.0

func (Slug) Name() string

func (Slug) Validate added in v0.2.0

func (Slug) Validate(s string) error

type UUID

type UUID struct {
	Version uint8 `json:"version" xml:"version"`
}

func (UUID) Name

func (UUID) Name() string

func (UUID) Validate

func (v UUID) Validate(s string) error

type UpperCase

type UpperCase struct{}

func (UpperCase) Name

func (UpperCase) Name() string

func (UpperCase) Validate

func (UpperCase) Validate(s string) error

type Username

type Username struct {
	Format     string `json:"format" xml:"format"`
	ExtraChars string `json:"-" xml:"-"`
}

The validation format used matches the following regular expression :

Format: /^[a-zA-Z][a-zA-Z0-9]*$/

To limit the length, it is recommended to use the Length, MinLength or MaxLength validator.

func (Username) Name

func (Username) Name() string

func (Username) Validate

func (v Username) Validate(s string) error

type Validator

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

Jump to

Keyboard shortcuts

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