Created
July 3, 2023 14:22
-
-
Save fragoulis/c1152278b9f8f95bb2cc7c522c9e1f3a to your computer and use it in GitHub Desktop.
Go validator cheatsheet creator
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/go-playground/validator/v10" | |
) | |
func main() { | |
validator := validator.New() | |
tests := []string{ | |
"foo", | |
"foo/bar", | |
"foo:3000", | |
"foo:3000/bar", | |
"sub.foo", | |
"sub.foo/bar", | |
"sub.foo:3000", | |
"sub.foo:3000/bar", | |
"foo.gr", | |
"foo.gr/bar", | |
"foo.gr:3000", | |
"foo.gr:3000/bar", | |
"sub.foo.gr", | |
"sub.foo.gr/bar", | |
"sub.foo.gr:3000", | |
"sub.foo.gr:3000/bar", | |
"http://foo", | |
"http://foo/bar", | |
"http://foo:3000", | |
"http://foo:3000/bar", | |
"http://sub.foo", | |
"http://sub.foo/bar", | |
"http://sub.foo:3000", | |
"http://sub.foo:3000/bar", | |
"http://foo.gr", | |
"http://foo.gr/bar", | |
"http://foo.gr:3000", | |
"http://foo.gr:3000/bar", | |
"http://sub.foo.gr", | |
"http://sub.foo.gr/bar", | |
"http://sub.foo.gr:3000", | |
"http://sub.foo.gr:3000/bar", | |
} | |
validators := []string{ | |
"url", | |
"url_encoded", | |
"hostname", | |
"hostname_port", | |
"fqdn", | |
} | |
for _, rule := range validators { | |
for _, test := range tests { | |
err := validator.Var(test, rule) | |
fmt.Printf("%q,%q,", rule, test) | |
if err != nil { | |
fmt.Println("FAIL") | |
} else { | |
fmt.Println("PASS") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment