Exemple #1
0
func ParseForm(configure func(*validators.FormValidator)) server.Handler {
	// it's ok to set nil as configure to parse form without validation.
	var v *validators.FormValidator
	if configure != nil {
		v = validators.NewFormValidator()
		configure(v)
	}
	return server.Handler(
		func(req *wcg.Request) response.Response {
			if m := req.Method(); m != "POST" && m != "PUT" {
				return nil
			}
			if req.Header("content-type") != "application/x-www-form-urlencoded" {
				req.Logger.Debugf("Skip FormValidation since the requested content is not a form")
				return nil
			}
			err := req.HTTPRequest().ParseForm()
			if err == nil {
				req.Logger.Debugf("Request Form: %v", req.HTTPRequest().Form)
			} else {
				req.Logger.Warnf("ParseForm failed in FormValidaiton process. Triggered ValidationError: %v", err)
				return response.BadRequest(req, err)
			}

			if v == nil {
				req.Logger.Debugf("Skip FormValidation since no validator is defined.")
				return nil
			}
			if err := v.Eval(req.HTTPRequest().Form); err != nil {
				return response.BadRequest(req, err)
			}
			return nil
		})
}
Exemple #2
0
func ValidateQuery(configure func(*validators.FormValidator)) server.Handler {
	if configure == nil {
		panic(fmt.Errorf("You must pass configure function on ValidateQuery"))
	}
	v := validators.NewFormValidator()
	configure(v)
	return server.Handler(
		func(req *wcg.Request) response.Response {
			if err := v.Eval(req.URL().Query()); err != nil {
				return response.BadRequest(req, err)
			}
			return nil
		})
}