Skylm808 commented on code in PR #909:
URL: https://github.com/apache/dubbo-go-pixiu/pull/909#discussion_r3106800249


##########
pkg/filter/http/apiconfig/openapi/validator.go:
##########
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package openapi
+
+import (
+       "bytes"
+       "encoding/json"
+       "fmt"
+       "io"
+       "math"
+       "net/http"
+       "strings"
+)
+
+import (
+       "github.com/apache/dubbo-go-pixiu/pkg/router"
+)
+
+func ValidateRequest(req *http.Request, plan *ValidationPlan) error {
+       if req == nil || plan == nil {
+               return nil
+       }
+
+       if err := validatePathParameters(req, plan); err != nil {
+               return err
+       }
+
+       if err := validateQueryParameters(req, plan.QueryParameters); err != 
nil {
+               return err
+       }
+
+       if err := validateHeaderParameters(req, plan.HeaderParameters); err != 
nil {
+               return err
+       }
+
+       if err := validateRequestBody(req, plan.RequestBody); err != nil {
+               return err
+       }
+
+       return nil
+}
+
+func validatePathParameters(req *http.Request, plan *ValidationPlan) error {
+       if plan.RoutePattern == "" || len(plan.PathParameters) == 0 {
+               return nil
+       }
+
+       values := router.GetURIParams(&router.API{URLPattern: 
plan.RoutePattern}, *req.URL)
+       for _, param := range plan.PathParameters {
+               value := values.Get(param.Name)
+               if param.Required && strings.TrimSpace(value) == "" {
+                       return fmt.Errorf("path parameter %q is required", 
param.Name)
+               }
+               if value == "" {
+                       continue
+               }
+               if len(param.Enum) > 0 && !contains(param.Enum, value) {
+                       return fmt.Errorf("path parameter %q must be one of 
%v", param.Name, param.Enum)
+               }
+       }
+
+       return nil
+}
+
+func validateQueryParameters(req *http.Request, params []ParameterValidation) 
error {

Review Comment:
   参数这,上次确实只用了 `required` 和 `enum`,没有把 schema 里的其他约束真正校验起来。这次把 `type / minimum 
/ maximum / minLength / maxLength` 也补进去了,并补了对应测试,像 `page=abc` 这种情况现在会直接返回校验错误。
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to