Coder Social home page Coder Social logo

genval's Introduction

genval GoDoc Build Status

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

  • no reflection in generated code - it means fast
  • possibilities to override generated behavior
  • can be used as //go:generate genval pkg
  • Enum support

Installation

go get github.com/gojuno/genval

Usage

type User struct {
    Name    string  `validate:"max_len=64"`
    Age     uint    `validate:"min=18"`
    Emails []string `validate:"min_items=1,item=[min_len=5]"`
}

//generates:
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
}
Some other examples:

How to generate?

genval mypkg

or you can use it as go:generate directive

//go:generate genval mypkg

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

Enum support

Go doesn`t support enums, but you can create some custom type and add few constants with required values.

type State int

const (
    StateOk    State = 200
    StateError State = 400
)

//generates:
func (r State) Validate() error {
    switch r {
    case StateOk:
    case StateError:
    default:
        return fmt.Errorf("invalid value for enum State: %v", r)
    }
    return nil
}

Some tips

  1. don`t use interface{} if you can
  2. commit generated code under source control
  3. read generated code if needed, do not afraid it

Custom validation

Additional validation

In some cases it`s required to add some custom validation. You can just add unexported validate method.

type User struct {
    Name  string `validate:"max_len=64"`
    Age   uint   `validate:"min=16"`
    Email string
}

func (u User) validate() error {
    if u.Age < 18 && u.Email == "" {
        return errors.New("email is required for people younger than 18")
    }

    return nil
}

// generates:
func (r User) Validate() error {
	if utf8.RuneCountInString(r.Name) > 64 {
		return fmt.Errorf("field Name is longer than 64 chars")
	}
	if r.Age < 16 {
		return fmt.Errorf("field Age is less than 16 ")
	}

	return r.validate() // custom validation call
}

Override validation

If you don`t want to use genval for some structs or use just custom validation then you can override exported Validate method. In this case genval will generate nothing for this struct.

genval's People

Contributors

hexdigest avatar l1va avatar mguzelevich avatar mkabischev avatar mkorolyov avatar v1-wizard avatar vpbarb avatar yauhen-l avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

genval's Issues

don`t generated redundant code

In some cases genval generates empty ranges.
Example:

type AliasSlice []string

actual result

// Validate validates AliasSlice
func (r AliasSlice) Validate() error {
	for _, x := range r {
		_ = x
	}
	return nil
}

expected result

// Validate validates AliasSlice
func (r AliasSlice) Validate() error {
	return nil
}

add dot notation for err messages for nested fields.

By default Validate() method will be generated with fields names equal to struct field names.

Also -tags flag will be added, that will accept comma separated list of tags, like -tags json,xml.
For each tag will be generated separate validate method with name Validate<tag_in_upper_case>.

So if we set -tags json,xml 3 methods will be generated:

  • Validate()
  • ValidateJSON()
  • ValidateXML()

Difference will be only in err msg fields naming. e.g. ValidateJSON will use json tag field names.

support old command line format

Now genval accepts two flags:
genval -d directory_name -p package_name

In most cases directory_name equals to package_name so we can support old format which accepts single argument and uses for both flags.

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.