go-validation

module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2022 License: MIT

README

go-validation

This is a package for generating types for validation. The readme is a work in progress so for additional information check out examples folder for extra documentation.

Get binary

Generate binary from cli/main.go

Generate

You generate files from existing go files, by annotating types and fields with (vgen:"include" or vgen:"i") and then running (vgen gen [path to folder]). This will create a (vgen_[filename].go) file

Usage

To validate a type create a [typename]In. Then create a [typename]Rules type. With these two you can call the Validate or ValidatedConvert functions which will return the validation errors or your original type these were generated from.

Why

Why this package?
I created this package mostly because of a need of a type safe validation package which also could differentiate between empty and not set (nil) values.

Example

Create a type with vgen:"i" somewhere on the last line

// vgen:"i"
type Person struct {
	Name string
	Age  int
}

Which generates PersonIn and PersonRules types in a file in the same folder

// Input struct
type PersonIn struct {
	Name *string `json:"name"`
	Age  *int    `json:"age"`
}

// Rules
type PersonRules struct {
	Name   validation.Rule[string]
	Age    validation.Rule[int]
	Custom validation.Rule[Person]
}

With functions

func (in PersonIn) Convert() Person { ... }
func (r PersonRules) Validate(in PersonIn) error { ... }
func (in PersonIn) Validate(r PersonRules) error { ... }
func (r PersonRules) ValidatedConvert(in PersonIn) (Person, error) { ... }
func (in PersonIn) ValidatedConvert(r PersonRules) (Person, error) { ... }

Now you can validate the request like

var (
    // this could also come from json.Unmarshal
    person = simplestruct.PersonIn{
        Name: validation.P(""),
		Age:  validation.P(18),
	}
    rules = simplestruct.PersonRules{
		Name: validation.StringNotEmptyRule{}, // Here we use a single rule
		Age: validation.Rules[int]{ // Here we can use validation.Rules to set mulitple rules
			validation.NumGteRule[int]{Value: 10},
			validation.NumLteRule[int]{Value: 15},
        },
    }
)

func main() {
	err := person.Validate(rules)
	if err != nil {
        // printed normally
		fmt.Println(err)
        // marshal into json
		j, _ := json.MarshalIndent(err, "", "  ")
		fmt.Println(string(j))
	}

    // or
    validatedPerson, err := person.ValidatedConvert(rules)  
	if err != nil {
        // printed normally
		fmt.Println(err)
        // marshal into json
		j, _ := json.MarshalIndent(err, "", "  ")
		fmt.Println(string(j))
	} else {
        fmt.Printf("%+v\n", validatedPerson)
    }
}

Directories

Path Synopsis
cli
cmd
examples
alias
Generated file DO NOT EDIT
Generated file DO NOT EDIT
arraysmaps
Generated file DO NOT EDIT
Generated file DO NOT EDIT
decodejson
Generated file DO NOT EDIT
Generated file DO NOT EDIT
imported
Generated file DO NOT EDIT
Generated file DO NOT EDIT
importfrom
Generated file DO NOT EDIT
Generated file DO NOT EDIT
optional
Generated file DO NOT EDIT
Generated file DO NOT EDIT
simplestruct
Generated file DO NOT EDIT
Generated file DO NOT EDIT
structRules
Generated file DO NOT EDIT
Generated file DO NOT EDIT
tags
Generated file DO NOT EDIT
Generated file DO NOT EDIT

Jump to

Keyboard shortcuts

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