Giter Site home page Giter Site logo

gokay's Introduction

gokay CircleCI Coverage Status

Codegenerated struct validation tool for go.

How it works

gokay parses a struct and generates a Validate method so that the struct implements the Validateable interface. It does so by parsing the valid tags in that struct's fields.

gokay generated Validate methods will return an ErrorMap that implements the Error interface. The ErrorMap is a map[string]error containing failed validations for each invalid field in a struct.

Code Documentation

godoc -http=:6060

Installing gokay

This project uses Glide to manage it's dependencies. Please refer to the glide docs to see how to install and use glide.

This project is tested on go 1.7.1 and glide 0.12.1

mkdir -p $GOPATH/github.com/zencoder
cd $GOPATH/src/github.com/zencoder
git clone https://github.com/zencoder/gokay
cd gokay
glide install
go install ./...

Running gokay

Usage

gokay <file> [custom-generator-package custom-generator-contructor]

Examples

Generate validation methods for types in a single file

gokay file.go

Generate validation methods with a custom package and constructor

gokay file.go gkcustom NewCustomGKGenerator

gokay relies on the goimports tool to resolve import path of custom generator.

Using gokay

  • Add validations to valid tag in struct def:
type ExampleStruct struct {
	HexStringPtr            *string `valid:"Length=(16),NotNil,Hex"`
	HexString               string  `valid:"Length=(12),Hex"`
	CanBeNilWithConstraints *string `valid:"Length=(12)"`
}
  • Run gokay command

Tag syntax

Validation tags are comma separated, with Validation parameters delimited by open and closed parentheses.

valid:"ValidationName1,ValidationName2=(vn2paramA)(vn2paramB)"

In the above example, the Hex and NotNil Validations are parameterless, whereas length requires 1 parameter.

Built-in Validations

Name Params Allowed Field Types Description
Hex N/A (*)string Checks if a string field is a valid hexadecimal format number (0x prefix optional)
NotNil N/A pointers Checks and fails if a pointer is nil
Length 1 (*)string Checks if a string's length matches the tag's parameter
UUID N/A (*)string Checks and fails if a string is not a valid UUID

Implicitly generated validations

These sections of code will be added to the generated Validate() method regardless of a field's valid tag's contents. If a struct does not have any valid tags and no fields with implicit validation rules, then no Validate method will be generated.

  • Struct fields: generated code will call static Validate method on any field that implements Validateable interface
  • Slice/Map fields: Static Validate will be called on each element of a slice or map of structs or struct pointers (one level of indirection). Only supports maps with string indices.

Note on built-in and implicit validations: With the obvious exception of NotNil, nil pointers fields are considered to be valid in order to allow support for optional fields.

Writing your own Validations

gokay was built to allow developers to write and attach their own Validations to the Validate generator.

  1. Write a function that validates a field. E.g:

    // LengthString checks if the value of a string pointer has a length of exactly 'expected'
    func LengthString(expected int64, str *string) error {
    	if str == nil {
    		return nil // Covers the case where a value can be nil OR has a length constraint
    	}
    
    	if expected != int64(len(*str)) {
    		return fmt.Errorf("Length was '%d', needs to be '%d'", len(*str), expected)
    	}
    	return nil
    }
  2. Write a struct that implements the Generater interface

    // Generater defines the behavior of types that generate validation code
    type Generater interface {
    	Generate(reflect.Type, reflect.StructField, []string) (string, error)
    	Name() string
    }
    • Name returns the string that will be used as a validation tag
  3. GenerateValidationCode should generate a block will leverage the function defined in step 1. This block will be inserted into the generated Validate function. GenerateValidationCode output example:

    // ValidationName
    if err := somepackage.ValidationFunction(someparamA, someparamB, s.Field); err != nil {
    	errorsField = append(errorsField, err)
    }
  4. Write a function that constructs a ValidateGenerator. This function should live in a different package from your data model. Example:

    package gkcustom
    
    // To run: `gokay gkexample NewCustomValidator`
    func NewCustomGKGenerator() *gkgen.ValidateGenerator {
    	v := gkgen.NewValidateGenerator()
    	v.AddValidation(NewCustomValidator())
    	return v
    }
  5. Write tests for your struct's constraints

  6. Add valid tags to your struct fields

  7. Run gokay: gokay file.go gkcustom NewCustomGKGenerator

More Examples

Development

Dependencies

Tested on go 1.7.1.

Build and run unit tests

make test

CI

This library builds on Circle CI, here.

License

Apache License Version 2.0

gokay's People

Contributors

briandowns avatar philcluff avatar

Watchers

 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.