You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2021/06/29 10:52:40 UTC
[servicecomb-kie] branch master updated: Update labelKV validator
rule (#186)
This is an automated email from the ASF dual-hosted git repository.
littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git
The following commit(s) were added to refs/heads/master by this push:
new 1f3f193 Update labelKV validator rule (#186)
1f3f193 is described below
commit 1f3f193e760ebb553e4661ca3ed85763f21ccc65
Author: little-cui <su...@qq.com>
AuthorDate: Tue Jun 29 18:52:34 2021 +0800
Update labelKV validator rule (#186)
---
pkg/model/db_schema.go | 6 +++---
pkg/validator/rule.go | 6 ++++--
pkg/validator/rule_test.go | 2 +-
server/resource/v1/kv_resource_test.go | 16 ++++++++++++++++
4 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/pkg/model/db_schema.go b/pkg/model/db_schema.go
index 5583df7..4ebc5b1 100644
--- a/pkg/model/db_schema.go
+++ b/pkg/model/db_schema.go
@@ -44,8 +44,8 @@ type KVDoc struct {
CreateTime int64 `json:"create_time,omitempty" bson:"create_time," yaml:"create_time,omitempty"`
UpdateTime int64 `json:"update_time,omitempty" bson:"update_time," yaml:"update_time,omitempty"`
- Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" validate:"max=6,dive,keys,labelKV,endkeys,labelKV"` //redundant
- Domain string `json:"domain,omitempty" yaml:"domain,omitempty" validate:"min=1,max=256,commonName"` //redundant
+ Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" validate:"max=6,dive,keys,labelK,endkeys,labelV"` //redundant
+ Domain string `json:"domain,omitempty" yaml:"domain,omitempty" validate:"min=1,max=256,commonName"` //redundant
}
//ViewDoc is db struct, it saves user's custom view name and criteria
@@ -94,7 +94,7 @@ type ListKVRequest struct {
Project string `json:"project,omitempty" yaml:"project,omitempty" validate:"min=1,max=256,commonName"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty" validate:"min=1,max=256,commonName"` //redundant
Key string `json:"key" yaml:"key" validate:"max=128,getKey"`
- Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" validate:"max=8,dive,keys,labelKV,endkeys,labelKV"` //redundant
+ Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" validate:"max=8,dive,keys,labelK,endkeys,labelV"` //redundant
Offset int64 `validate:"min=0"`
Limit int64 `validate:"min=0,max=100"`
Status string `json:"status,omitempty" yaml:"status,omitempty" validate:"kvStatus"`
diff --git a/pkg/validator/rule.go b/pkg/validator/rule.go
index b6e9e71..3d922ba 100644
--- a/pkg/validator/rule.go
+++ b/pkg/validator/rule.go
@@ -22,7 +22,8 @@ import "github.com/go-chassis/foundation/validator"
const (
key = "key"
commonNameRegexString = `^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$`
- labelKvRegexString = `^[a-zA-Z0-9]{0,32}$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,30}[a-zA-Z0-9]$`
+ labelKeyRegexString = `^[a-zA-Z0-9]{0,32}$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,30}[a-zA-Z0-9]$`
+ labelValueRegexString = `^[a-zA-Z0-9]{0,160}$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,158}[a-zA-Z0-9]$`
getKeyRegexString = `^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$|^beginWith\([a-zA-Z0-9][a-zA-Z0-9_\-.]*\)$|^wildcard\([a-zA-Z0-9][a-zA-Z0-9_\-.*]*\)$`
asciiRegexString = `^[\x00-\x7F]*$`
allCharString = `.*`
@@ -37,7 +38,8 @@ var customRules = []*validator.RegexValidateRule{
validator.NewRegexRule("valueType", `^$|^(ini|json|text|yaml|properties)$`),
validator.NewRegexRule("kvStatus", `^$|^(enabled|disabled)$`),
validator.NewRegexRule("value", allCharString), //ASCII, 2M
- validator.NewRegexRule("labelKV", labelKvRegexString),
+ validator.NewRegexRule("labelK", labelKeyRegexString),
+ validator.NewRegexRule("labelV", labelValueRegexString),
validator.NewRegexRule("check", asciiRegexString), //ASCII, 1M
}
diff --git a/pkg/validator/rule_test.go b/pkg/validator/rule_test.go
index 5cf5705..4302b4a 100644
--- a/pkg/validator/rule_test.go
+++ b/pkg/validator/rule_test.go
@@ -144,7 +144,7 @@ func TestValidate(t *testing.T) {
"3": "a",
"4": "a",
"5": "a",
- "6": "a-" + strings.Repeat("x", 31), // error
+ "6": "a-" + strings.Repeat("x", 159), // error
},
}
assert.Error(t, validator.Validate(kvDoc))
diff --git a/server/resource/v1/kv_resource_test.go b/server/resource/v1/kv_resource_test.go
index b7d6f5e..695ece2 100644
--- a/server/resource/v1/kv_resource_test.go
+++ b/server/resource/v1/kv_resource_test.go
@@ -36,6 +36,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
+ "strings"
"sync"
"testing"
"time"
@@ -79,6 +80,21 @@ func init() {
}
}
func TestKVResource_Post(t *testing.T) {
+ t.Run("post kv, label is invalid, should return err", func(t *testing.T) {
+ kv := &model.KVDoc{
+ Key: "timeout",
+ Value: "1s",
+ Labels: map[string]string{"service": strings.Repeat("x", 161)},
+ }
+ j, _ := json.Marshal(kv)
+ r, _ := http.NewRequest("POST", "/v1/kv_test/kie/kv", bytes.NewBuffer(j))
+ r.Header.Set("Content-Type", "application/json")
+ kvr := &v1.KVResource{}
+ c, _ := restfultest.New(kvr, nil)
+ resp := httptest.NewRecorder()
+ c.ServeHTTP(resp, r)
+ assert.Equal(t, http.StatusBadRequest, resp.Result().StatusCode)
+ })
t.Run("post kv, label is service", func(t *testing.T) {
kv := &model.KVDoc{
Key: "timeout",