Created
December 2, 2018 11:10
-
-
Save faceair/58c60e768edb464ad550cc7f39c2950f to your computer and use it in GitHub Desktop.
jio 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 ( | |
"io/ioutil" | |
"net/http" | |
"github.com/faceair/jio" | |
"github.com/go-chi/chi" | |
) | |
func validater(keys jio.K) func(next http.Handler) http.Handler { | |
return jio.ValidateBody(jio.Object().Keys(keys), jio.DefaultErrorHandler) | |
} | |
func main() { | |
r := chi.NewRouter() | |
r.Route("/people", func(r chi.Router) { | |
r.With(validater(jio.K{ | |
"name": jio.String().Min(3).Max(10).Required(), | |
"age": jio.Number().Integer().Min(0).Max(100).Required(), | |
"phone": jio.String().Regex(`^1[34578]\d{9}$`).Required(), | |
})).Post("/", func(w http.ResponseWriter, r *http.Request) { | |
body, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
panic(err) | |
} | |
w.Header().Set("Content-Type", "application/json; charset=utf-8") | |
w.WriteHeader(http.StatusOK) | |
w.Write(body) | |
}) | |
}) | |
http.ListenAndServe(":8080", r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment