passport

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: MIT Imports: 14 Imported by: 0

README

Passport

Go Test Coverage Statusd

Install

go get -v github.com/xray-family/passport@latest

Quick Start

package main

import (
    "encoding/json"
    "fmt"
    "github.com/xray-family/passport"
    "net/http"
)

type Req struct {
    Name  string `json:"name"`
    Age   int    `json:"age"`
    Roles []int  `json:"roles"`
}

func (c *Req) Validate(h http.Header) error {
    opt := passport.WithLang(h.Get("Accept-Language"))
    return passport.NewValidator(opt).Validate(
        passport.String("Name", c.Name).Required().Alphabet(),
        passport.Ordered("Age", c.Age).Gte(18),
        passport.Slice("Roles", c.Roles).Required(),
    )
}

func Handle(writer http.ResponseWriter, request *http.Request) {
    var r = &Req{}
    _ = json.NewDecoder(request.Body).Decode(r)
    _ = request.Body.Close()
    var err = r.Validate(request.Header)
    fmt.Printf("%v\n", err)
}

func main() {
    http.HandleFunc("/", Handle)
    http.ListenAndServe(":8080", nil)
}

Advanced

Customized Check Functions
package main

import (
    "fmt"
    "github.com/nicksnyder/go-i18n/v2/i18n"
    "github.com/xray-family/passport"
)

func isPhone(s string) bool {
    return len(s) == 11 && s[0] == '1'
}

func main() {
    _ = passport.GetBundle().AddMessages(passport.English, &i18n.Message{
        ID:    "Phone",
        Other: "Failed to verify cell phone number",
    })
    var validator = passport.NewValidator()
    var err = validator.Validate(
        passport.String("phone number", "xyz").Customize("Phone", isPhone),
    )
    fmt.Printf("%v\n", err)
}
Field Translations
package main

import (
    "fmt"
    "github.com/nicksnyder/go-i18n/v2/i18n"
    "github.com/xray-family/passport"
)

func main() {
    _ = passport.GetBundle().AddMessages(passport.Chinese, &i18n.Message{
        ID:    "Req.Name",
        Other: "用户名",
    })
    var validator = passport.NewValidator(
        passport.WithLang(passport.Chinese.String()),
        passport.WithAutoTranslate(true),
    )
    var err = validator.Validate(
        passport.String("Req.Name", "").Required(),
    )
    fmt.Printf("%v\n", err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Chinese = language.Make("zh-CN")
	English = language.Make("en-US")
)

Functions

func GetBundle

func GetBundle() *i18n.Bundle

func GetLocalizer

func GetLocalizer() *i18n.Localizer

func SetLang

func SetLang(tag language.Tag, langs ...string)

func Validate

func Validate(values ...Value) error

Types

type AnyValue

type AnyValue[T any] struct {
	// contains filtered or unexported fields
}

func Any

func Any[T any](k string, v T) *AnyValue[T]

func (*AnyValue[T]) Customize

func (c *AnyValue[T]) Customize(messageId string, f func(T) bool) *AnyValue[T]

func (*AnyValue[T]) Err

func (c *AnyValue[T]) Err() error

Err get error

type Option added in v0.0.3

type Option func(c *config)

func WithAutoTranslate added in v0.0.3

func WithAutoTranslate(enabled bool) Option

WithAutoTranslate automatic translation of key names

func WithLang added in v0.0.3

func WithLang(langs ...string) Option

WithLang set languages

type OrderedValue

type OrderedValue[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

func Ordered

func Ordered[T cmp.Ordered](k string, v T) *OrderedValue[T]

func (*OrderedValue[T]) Between added in v0.0.3

func (c *OrderedValue[T]) Between(a, b T) *OrderedValue[T]

Between check that the range of values of the ordered value satisfies a <= x <b

func (*OrderedValue[T]) Customize

func (c *OrderedValue[T]) Customize(messageId string, f func(T) bool) *OrderedValue[T]

Customize customized data validation @layout error message @f check function

func (*OrderedValue[T]) Err

func (c *OrderedValue[T]) Err() error

Err get error

func (*OrderedValue[T]) Gt

func (c *OrderedValue[T]) Gt(v T) *OrderedValue[T]

Gt check the ordered value is greater than v

func (*OrderedValue[T]) Gte

func (c *OrderedValue[T]) Gte(v T) *OrderedValue[T]

Gte check the ordered value is greater or equal than v

func (*OrderedValue[T]) In

func (c *OrderedValue[T]) In(args ...T) *OrderedValue[T]

In check if args contains the ordered value.

func (*OrderedValue[T]) Lt

func (c *OrderedValue[T]) Lt(v T) *OrderedValue[T]

Lt check the ordered value is less than v

func (*OrderedValue[T]) Lte

func (c *OrderedValue[T]) Lte(v T) *OrderedValue[T]

Lte check the ordered value is less or equal than v

func (*OrderedValue[T]) Required

func (c *OrderedValue[T]) Required() *OrderedValue[T]

Required the ordered value cannot be empty

type PointerValue

type PointerValue[T any] struct {
	// contains filtered or unexported fields
}

func Pointer

func Pointer[T any](k string, v *T) *PointerValue[T]

func (*PointerValue[T]) Customize

func (c *PointerValue[T]) Customize(messageId string, f func(*T) bool) *PointerValue[T]

func (*PointerValue[T]) Err

func (c *PointerValue[T]) Err() error

func (*PointerValue[T]) Required

func (c *PointerValue[T]) Required() *PointerValue[T]

type SliceValue

type SliceValue[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

func Slice

func Slice[T cmp.Ordered](k string, v []T) *SliceValue[T]

func (*SliceValue[T]) Contains

func (c *SliceValue[T]) Contains(v T) *SliceValue[T]

Contains checks whether the slice contains v

func (*SliceValue[T]) Customize

func (c *SliceValue[T]) Customize(messageId string, f func([]T) bool) *SliceValue[T]

func (*SliceValue[T]) Eq

func (c *SliceValue[T]) Eq(v int) *SliceValue[T]

Eq check the slice length is equal to v

func (*SliceValue[T]) Err

func (c *SliceValue[T]) Err() error

Err get error

func (*SliceValue[T]) Gt

func (c *SliceValue[T]) Gt(v int) *SliceValue[T]

Gt check the slice length is greater than v

func (*SliceValue[T]) Gte

func (c *SliceValue[T]) Gte(v int) *SliceValue[T]

Gte check the slice length is greater or equal than v

func (*SliceValue[T]) Lt

func (c *SliceValue[T]) Lt(v int) *SliceValue[T]

Lt check the slice length is less than v

func (*SliceValue[T]) Lte

func (c *SliceValue[T]) Lte(v int) *SliceValue[T]

Lte check the slice length is less or equal than v

func (*SliceValue[T]) Required

func (c *SliceValue[T]) Required() *SliceValue[T]

Required the slice cannot be empty

type StringValue

type StringValue[T ~string] struct {
	// contains filtered or unexported fields
}

func String

func String[T ~string](k string, v T) *StringValue[T]

func (*StringValue[T]) Alphabet

func (c *StringValue[T]) Alphabet() *StringValue[T]

Alphabet Check if the string consists of letters

func (*StringValue[T]) AlphabetNumeric

func (c *StringValue[T]) AlphabetNumeric() *StringValue[T]

AlphabetNumeric Check the string consists of letters and numbers.

func (*StringValue[T]) Base64

func (c *StringValue[T]) Base64() *StringValue[T]

Base64 verify that the string is formatted for base64.

func (*StringValue[T]) Between added in v0.0.3

func (c *StringValue[T]) Between(a, b T) *StringValue[T]

Between check that the range of values of the ordered value satisfies a <= x <b

func (*StringValue[T]) Customize

func (c *StringValue[T]) Customize(messageId string, f func(T) bool) *StringValue[T]

Customize customized data validation @layout error message @f check function

func (*StringValue[T]) Email

func (c *StringValue[T]) Email() *StringValue[T]

Email verify that the string is formatted for email.

func (*StringValue[T]) Eq

func (c *StringValue[T]) Eq(v int) *StringValue[T]

Eq check that the string length is equal to v

func (*StringValue[T]) Err

func (c *StringValue[T]) Err() error

Err get error

func (*StringValue[T]) Gt

func (c *StringValue[T]) Gt(v int) *StringValue[T]

Gt check that the string length is greater than v

func (*StringValue[T]) Gte

func (c *StringValue[T]) Gte(v int) *StringValue[T]

Gte check that the string length is greater or equal than v

func (*StringValue[T]) Hex

func (c *StringValue[T]) Hex() *StringValue[T]

Hex verify that the string is formatted for hex.

func (*StringValue[T]) IPv4

func (c *StringValue[T]) IPv4() *StringValue[T]

IPv4 verify that the string is formatted for IPv4.

func (*StringValue[T]) IPv6

func (c *StringValue[T]) IPv6() *StringValue[T]

IPv6 verify that the string is formatted for IPv6.

func (*StringValue[T]) In

func (c *StringValue[T]) In(args ...T) *StringValue[T]

In check if args contains the string.

func (*StringValue[T]) Lowercase

func (c *StringValue[T]) Lowercase() *StringValue[T]

Lowercase verify the string consists of lowercase letters.

func (*StringValue[T]) Lt

func (c *StringValue[T]) Lt(v int) *StringValue[T]

Lt check that the string length is less than v

func (*StringValue[T]) Lte

func (c *StringValue[T]) Lte(v int) *StringValue[T]

Lte check that the string length is less or equal than v

func (*StringValue[T]) MatchRegexp

func (c *StringValue[T]) MatchRegexp(re *regexp.Regexp) *StringValue[T]

MatchRegexp verify that the string matches the regular expression re

func (*StringValue[T]) MatchString

func (c *StringValue[T]) MatchString(re string) *StringValue[T]

MatchString verify that the string matches the regular expression re

func (*StringValue[T]) Numeric

func (c *StringValue[T]) Numeric() *StringValue[T]

Numeric Check if the string consists of numbers

func (*StringValue[T]) Required

func (c *StringValue[T]) Required() *StringValue[T]

Required the string cannot be empty

func (*StringValue[T]) URL

func (c *StringValue[T]) URL() *StringValue[T]

URL verify that the string is formatted for URL.

func (*StringValue[T]) Uppercase

func (c *StringValue[T]) Uppercase() *StringValue[T]

Uppercase verify the string consists of uppercase letters.

type Validator

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

func NewValidator

func NewValidator(options ...Option) *Validator

func (*Validator) Validate

func (c *Validator) Validate(values ...Value) error

type Value

type Value interface {
	Err() error
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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