validate

package
v0.0.0-...-ccfaf30 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package validate 日本語環境向けバリデーションを行なうライブラリがあるパッケージ

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Struct

func Struct(v interface{}) error

Struct 与えられた構造体のバリデーション処理を行なう

Example
package main

import (
	"fmt"

	"github.com/topgate/goutils/validate"
)

func main() {
	type User struct {
		ID   int    `validate:"min=1"`
		Name string `validate:"len=5" fieldname:"名前"`
	}

	user := User{
		ID:   0,
		Name: "花山太郎",
	}

	err := validate.Struct(user)
	fmt.Print(err.Error())
}
Output:

IDは1かより大きくなければなりません
名前の長さは5文字でなければなりません
Example (FieldErrors)
package main

import (
	"fmt"

	"github.com/topgate/goutils/validate"
)

func main() {

	type User struct {
		ID   int    `validate:"min=1"`
		Name string `validate:"len=5" fieldname:"名前"`
	}

	user := User{
		ID:   0,
		Name: "花山太郎",
	}

	err := validate.Struct(user)
	ve, ok := err.(*validate.ValidationError)
	if ok {
		fmt.Println(ve.FiledErrors[0].Error())
		fmt.Println(ve.FiledErrors[1].Error())
	}
}
Output:

IDは1かより大きくなければなりません
名前の長さは5文字でなければなりません

Types

type FieldError

type FieldError interface {
	validator.FieldError
	Error() string
}

FieldError フィールドのエラー

type ValidationError

type ValidationError struct {
	// FieldError フィールドごとのエラー
	FiledErrors []FieldError
}

ValidationError バリデーション時のエラー

func (ValidationError) Error

func (v ValidationError) Error() string

Jump to

Keyboard shortcuts

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