Created
March 26, 2021 01:48
-
-
Save theoparis/51240d89e9c8934ecaeb6bc2a7cd72c2 to your computer and use it in GitHub Desktop.
govald example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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