Giter VIP home page Giter VIP logo

genval's Introduction

genval Build Status

Generates Validate() methods for all structs in pkg by tags

  • no reflection in generated code - it means fast
  • generator not needed on runtime
  • possibilities to override generated behavior for local purposes
  • can be used as //go:generate genval pkg
  • if type has constants, validation will be by all found constants

Installation

go get github.com/gojuno/genval

Usage and documentation

./genval packageWithStructsForGeneration  

or as go:generate directive

//go:generate genval packageWithStructsForGeneration
Additional flags
outputFile - output file name (default: validators.go)
needValidatableCheck - check struct on Validatable before calling Validate() (default: true)
Supported tags:
  • String: min_len, max_len - min and max valid lenghth
  • Number: min, max - min and max valid value (can be float)
  • Array: min_items, max_items - min and max count of items in array
    item - scope tag, contains validation tags for each item
  • Pointer: nullable, not_null - it's clear
  • Interface: func - the same as for struct (func NameOfTheFunc(i interface{})error{..})
  • Struct: func - name of the method of this struct (func(s Struct) MethodName()error{..})
    or name of the func that will be used for validation (func nameOfTheFunc(s Struct)error{..})
    Methods should starts from '.'
    Can be used not once: func=.MethodName,func=nameOfTheFunc or even func=.MethodName;nameOfTheFunc
  • Map: min_items, max_items - min and max count of items in map
    key, value - scope tags, contains validation tags for key or value
Default validation - no Validation
Some tips:
  1. not use interface{} if you can
  2. commit generated code under source control
  3. read generated code if needed, do not afraid it
Ways to override generated behavior(see examples or try yourself):
  1. custom (r Struct)Validate() error method
  2. func tag for types
Examples:
type User struct {
    Name string `validate:"max_len=64"`
    Age  uint   `validate:"min=18"`
    Emails []string `validate:"min_items=1,item=[min_len=5]"`
}
//generated:
func (r User) validate() error {
    if utf8.RuneCountInString(r.Name) > 64 {
        return fmt.Errorf("field Name is longer than 64 chars")
    }
    if r.Age < 18 {
        return fmt.Errorf("field Age is shorter than 18 chars")
    }
    if len(r.Emails) < 1 {
        return fmt.Errorf("array Emails has less items than 1 ")
    }
    for _, x := range r.Emails {
        _ = x
        if utf8.RuneCountInString(x) < 5 {
            return fmt.Errorf("field x is shorter than 5 chars")
        }
    }
    return nil
}

Other examples:

Validation by constants :

type State int

const (
    StateOk    State = 200
    StateError State = 400
)
//generated:
func (r State) validate() error {
    switch r {
    case StateOk:
    case StateError:
    default:
        return fmt.Errorf("invalid value for enum State: %v", r)
    }
    return nil
}

genval's People

Contributors

l1va avatar mguzelevich avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.