|
@@ -58,9 +58,10 @@ func ResponseErr(status int, err error) *HTTPServerResponse {
|
|
|
type HTTPServerCallback func(request *HTTPServerRequest) *HTTPServerResponse
|
|
type HTTPServerCallback func(request *HTTPServerRequest) *HTTPServerResponse
|
|
|
|
|
|
|
|
type HTTPServerHandler struct {
|
|
type HTTPServerHandler struct {
|
|
|
- Callback HTTPServerCallback
|
|
|
|
|
- Data any
|
|
|
|
|
- Buffer int
|
|
|
|
|
|
|
+ Callback HTTPServerCallback
|
|
|
|
|
+ Data any
|
|
|
|
|
+ Buffer int
|
|
|
|
|
+ Validators map[string][]Validator
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type HTTPServerAction struct {
|
|
type HTTPServerAction struct {
|
|
@@ -180,6 +181,31 @@ func (this *HTTPServer) ServeHTTP(responseStream http.ResponseWriter, requestStr
|
|
|
responseStream.WriteHeader(http.StatusInternalServerError)
|
|
responseStream.WriteHeader(http.StatusInternalServerError)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: improve
|
|
|
|
|
+ if handler.Validators != nil {
|
|
|
|
|
+ value := reflect.ValueOf(handler.Data)
|
|
|
|
|
+ for field := range value.Fields() {
|
|
|
|
|
+ validators, ok := handler.Validators[field.Name]
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if len(validators) == 0 {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for _, validator := range validators {
|
|
|
|
|
+ if validator.Validate(value.Interface()) {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.log.Error("malformed validation failed", LogValue("path", requestStream.URL.Path), LogError(err))
|
|
|
|
|
+ responseStream.WriteHeader(http.StatusNotAcceptable)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Log query
|
|
// Log query
|