validator

package module
v0.0.0-...-3c55163 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

validator

A struct validator written in Golang

Go Report Card GoDoc

quickstart

package main

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

func main() {
    type model struct {
        *int `validate:"required(T) message(fail)"`
    }
    m := model{}
    result := validator.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

func RequiredFunc

func RequiredFunc() Func

RequiredFunc method

type Item

type Item struct {
	Required  bool    `alias:"required"`
	Min       float64 `alias:"min"`
	Max       float64 `alias:"max"`
	MinLength int     `alias:"min_length"`
	MaxLength int     `alias:"max_length"`
	Length    int     `alias:"length"`
	Enums     string  `alias:"enums"`
	Regex     string  `alias:"regex"`
	Message   string  `alias:"message"`
}

Item struct

func (*Item) String

func (i *Item) String() string

String return json

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