validate

package module
v0.0.0-...-9b93ea0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 13 Imported by: 0

README

validate

Go Reference

Input validation made easy for Go interface methods.

Installation

Make a custom build of protogo:

$ protogo build --plugin=github.com/protogodev/validate

Or build from a local fork:

$ protogo build --plugin=github.com/protogodev/validate=../my-fork
Usage
$ protogo validate -h
Usage: protogo validate <source-file> <interface-name>

Arguments:
  <source-file>       source file
  <interface-name>    interface name

Flags:
  -h, --help             Show context-sensitive help.

      --out="."          output directory
      --fmt              whether to make the generated code formatted
      --custom=STRING    the declaration file of custom validators

Quick Start

NOTE: The following code is located in helloworld.

  1. Define the interface

    type Service interface {
        SayHello(ctx context.Context, name string) (message string, err error)
    }
    
  2. Implement the service

    type Greeter struct{}
    
    func (g *Greeter) SayHello(ctx context.Context, name string) (string, error) {
        return "Hello " + name, nil
    }
    
  3. Add the validation annotations

    type Service interface {
        // @schema:
        //   name: len(0, 10) && match(`^\w+$`)
        SayHello(ctx context.Context, name string) (message string, err error)
    }
    
  4. Generate the validation middleware

    $ cd examples/helloworld
    $ protogo validate ./service.go Service
    
  5. Use the middleware for input validation

    func main() {
        var svc helloworld.Service = &helloworld.Greeter{}
        svc = helloworld.ValidateMiddleware(nil)(svc)
    
        message, err = svc.SayHello(context.Background(), "!Tracey")
        fmt.Printf("message: %q, err: %v\n", message, err)
    
        // Output:
        // message: "", err: name: INVALID(invalid format)
    }
    

Validation Syntax

Operator / Validator Validating / Vext Equivalent(s) Example
! Not !lt(0)
&& All / And gt(0) && lt(10)
|| Any / Or eq(0) || eq(1)
nonzero Nonzero nonzero
zero Zero zero
len LenString / LenSlice len(0, 10)
runecnt RuneCount runecnt(0, 10)
eq Eq eq(1)
ne Ne ne(2)
gt Gt gt(0)
gte Gte gte(0)
lt Lt lt(10)
lte Lte lte(10)
xrange Range xrange(0, 10)
in In in(0, 1)
nin Nin nin("Y", "N")
match Match match(`^\w+$`)
email Email email
ip IP ip
time Time time("2006-01-02T15:04:05Z07:00")
_ A special validator that means to use the nested Schema() of the struct argument. _

Examples

See examples.

Documentation

Check out the documentation.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseDoc

func ParseDoc(comments []string) map[string][]Option

Types

type Generator

type Generator struct {
	OutDir    string `name:"out" default:"." help:"output directory"`
	Formatted bool   `name:"fmt" default:"true" help:"whether to make the generated code formatted"`
	Custom    string `name:"custom" help:"the declaration file of custom validators"`
}

func (*Generator) Generate

func (g *Generator) Generate(data *ifacetool.Data) (*generator.File, error)

func (*Generator) PkgName

func (g *Generator) PkgName() string

type Option

type Option struct{ K, V string }

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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