コード例 #1
0
ファイル: binding.go プロジェクト: Julianzz/gogs
// Performs validation and combines errors from validation
// with errors from deserialization, then maps both the
// resulting struct and the errors to the context.
func validateAndMap(obj reflect.Value, context martini.Context, errors *base.BindingErrors, ifacePtr ...interface{}) {
	context.Invoke(Validate(obj.Interface()))
	errors.Combine(getErrors(context))
	context.Map(*errors)
	context.Map(obj.Elem().Interface())
	if len(ifacePtr) > 0 {
		context.MapTo(obj.Elem().Interface(), ifacePtr[0])
	}
}
コード例 #2
0
ファイル: binding.go プロジェクト: Julianzz/gogs
// ErrorHandler simply counts the number of errors in the
// context and, if more than 0, writes a 400 Bad Request
// response and a JSON payload describing the errors with
// the "Content-Type" set to "application/json".
// Middleware remaining on the stack will not even see the request
// if, by this point, there are any errors.
// This is a "default" handler, of sorts, and you are
// welcome to use your own instead. The Bind middleware
// invokes this automatically for convenience.
func ErrorHandler(errs base.BindingErrors, resp http.ResponseWriter) {
	if errs.Count() > 0 {
		resp.Header().Set("Content-Type", "application/json; charset=utf-8")
		if _, ok := errs.Overall[base.BindingDeserializationError]; ok {
			resp.WriteHeader(http.StatusBadRequest)
		} else {
			resp.WriteHeader(422)
		}
		errOutput, _ := json.Marshal(errs)
		resp.Write(errOutput)
		return
	}
}
コード例 #3
0
ファイル: user.go プロジェクト: Julianzz/gogs
func (f *UpdateProfileForm) Validate(errors *base.BindingErrors, req *http.Request, context martini.Context) {
	if req.Method == "GET" || errors.Count() == 0 {
		return
	}

	data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
	data["HasError"] = true

	if len(errors.Overall) > 0 {
		for _, err := range errors.Overall {
			log.Error("UpdateProfileForm.Validate: %v", err)
		}
		return
	}

	validate(errors, data, f)
}
コード例 #4
0
ファイル: setting.go プロジェクト: Julianzz/gogs
func (f *AddSSHKeyForm) Validate(errors *base.BindingErrors, req *http.Request, context martini.Context) {
	data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
	AssignForm(f, data)

	if req.Method == "GET" || errors.Count() == 0 {
		if req.Method == "POST" &&
			(len(f.KeyContent) < 100 || !strings.HasPrefix(f.KeyContent, "ssh-rsa")) {
			data["HasError"] = true
			data["ErrorMsg"] = "SSH key content is not valid"
		}
		return
	}

	data["HasError"] = true
	if len(errors.Overall) > 0 {
		for _, err := range errors.Overall {
			log.Error("AddSSHKeyForm.Validate: %v", err)
		}
		return
	}

	validate(errors, data, f)
}