Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
feat: implementing 'mustContainLetter' validation function
Browse files Browse the repository at this point in the history
feat: payloads.Rulesheet Name field validation
test: Name field validation
  • Loading branch information
eliasfeijo committed Dec 26, 2023
1 parent 39a0d3d commit 047e00f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
31 changes: 31 additions & 0 deletions controllers/v1/rulesheets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,37 @@ func TestRulesheet_CreateRulesheet(t *testing.T) {
assert.Equal(t, "doesNotStartWithDigit", response.ValidationErrors[0].Tag)
})

t.Run("Error on validate Name field must contain letter", func(t *testing.T) {
gin.SetMode(gin.TestMode)

w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)

c.Request = &http.Request{
Header: make(http.Header),
}

payload := &payloads.Rulesheet{
Name: "#$%&*()",
}

bytedPayload, _ := json.Marshal(payload)

c.Request.Body = ioutil.NopCloser(bytes.NewReader(bytedPayload))

srv := new(mock_services.Rulesheets)

createdRulesheet := &dtos.Rulesheet{}

srv.On("Create", mock.Anything, createdRulesheet).Return(nil)
v1.NewRulesheets(srv).CreateRulesheet()(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
response := &responses.Error{}
json.Unmarshal(w.Body.Bytes(), response)
assert.Equal(t, "Name", response.ValidationErrors[0].Field)
assert.Equal(t, "mustContainLetter", response.ValidationErrors[0].Tag)
})

// t.Run("Error on define rulesheet entity", func(t *testing.T) {
// gin.SetMode(gin.TestMode)

Expand Down
10 changes: 10 additions & 0 deletions controllers/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func validatePayload(payload interface{}) *responses.Error {
validate := validator.New()
validate.RegisterValidation("doesNotStartWithDigit", validateDoesNotStartWithDigit)
validate.RegisterValidation("mustContainLetter", validateMustContainLetter)
if validationErr := validate.Struct(payload); validationErr != nil {
err2, ok := validationErr.(validator.ValidationErrors)

Expand Down Expand Up @@ -44,3 +45,12 @@ func validateDoesNotStartWithDigit(fl validator.FieldLevel) bool {
}
return true
}

func validateMustContainLetter(fl validator.FieldLevel) bool {
for _, r := range fl.Field().String() {
if unicode.IsLetter(r) {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion payloads/v1/rulesheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package v1
// - Rules: a pointer to a map of string keys and interface values. This is likely where the actual rules for the rulesheet are stored. The keys in the map would likely correspond to some sort of rule identifier or name, and the values would contain the logic or conditions for.
type Rulesheet struct {
ID uint `json:"id,omitempty"`
Name string `json:"name,omitempty" validate:"required,doesNotStartWithDigit"`
Name string `json:"name,omitempty" validate:"required,doesNotStartWithDigit,mustContainLetter"`
Description string `json:"description,omitempty"`
Slug string `json:"slug,omitempty"`
Version string `json:"version,omitempty"`
Expand Down

0 comments on commit 047e00f

Please sign in to comment.