Skip to content

Instantly share code, notes, and snippets.

@theoparis
Created March 26, 2021 01:48
Show Gist options
  • Save theoparis/51240d89e9c8934ecaeb6bc2a7cd72c2 to your computer and use it in GitHub Desktop.
Save theoparis/51240d89e9c8934ecaeb6bc2a7cd72c2 to your computer and use it in GitHub Desktop.
govald example
package main
import (
"fmt"
"github.com/mitchellh/mapstructure"
gv "govald/govald"
)
type User struct {
Username string
Email string
}
func main() {
schema := gv.Define(
"user",
gv.Define("username").
Default(func() interface{} { return "admin" }).
Min(5).
Max(25),
gv.Define("email").
Default(func() interface{} { return "[email protected]" }).
Email(nil),
)
err, raw := schema.ParseFile("./test.json5")
if err != nil {
fmt.Println(err)
}
var user User
err = mapstructure.Decode(raw, &user)
if err != nil {
fmt.Println(err)
}
fmt.Printf("User: [%s, %s]\n", user.Username, user.Email)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment