govtr

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

govtr

A struct validator written in Golang

CircleCI codecov Go Report Card GoDoc Release License

quickstart

package main

import (
    "fmt"
    "github.com/billcoding/govtr"
)

func main() {
    type model struct {
        *int `govtr:"msg(fail)"`
    }
    m := model{}
    result := govtr.New(&m).Validate()
    fmt.Println(result.Passed)
    fmt.Println(result.Messages())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func interface {
	Accept(typ reflect.Type) bool
	Pass(value reflect.Value) bool
}

Func interface

func EnumsFunc

func EnumsFunc(enums string) Func

EnumsFunc method

func LengthFunc

func LengthFunc(length int) Func

LengthFunc method

func MaxFunc

func MaxFunc(max float64) Func

MaxFunc method

func MaxLengthFunc

func MaxLengthFunc(maxLength int) Func

MaxLengthFunc method

func MinFunc

func MinFunc(min float64) Func

MinFunc method

func MinLengthFunc

func MinLengthFunc(minLength int) Func

MinLengthFunc method

func RegexFunc

func RegexFunc(regex string) Func

RegexFunc method

type Item

type Item struct {
	Mn  float64 `alias:"mn"`
	Mx  float64 `alias:"mx"`
	MnL int     `alias:"mnl"`
	MxL int     `alias:"mxl"`
	L   int     `alias:"l"`
	E   string  `alias:"e"`
	R   string  `alias:"r"`
	Msg string  `alias:"msg"`
}

Item struct

func (*Item) Validate

func (i *Item) Validate(field reflect.StructField, value reflect.Value) (bool, string)

Validate by fields

type Result

type Result struct {
	StructPtr interface{}
	Passed    bool
	Items     []*ResultItem
}

Result struct

func (*Result) Messages

func (r *Result) Messages() string

Messages return un-passed messages

type ResultItem

type ResultItem struct {
	Field   *reflect.StructField
	Passed  bool
	Message string
}

ResultItem struct

type Validator

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

Validator struct

func New

func New(structPtr interface{}) *Validator

New return new Validator

func (*Validator) Validate

func (v *Validator) Validate() *Result

Validate start

Jump to

Keyboard shortcuts

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