Skip to content

Commit

Permalink
Merge pull request #103 from rezakhademix/feature-add-json-validate-rule
Browse files Browse the repository at this point in the history
feat: IsJSON rule added
  • Loading branch information
rezakhademix authored Jun 13, 2024
2 parents c35482a + ae6b865 commit 6c6d08d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_IP4(t *testing.T) {
{
name: "test empty space string won't pass the ip4 validation rule",
field: "server_ip",
value: "",
value: " ",
isPassed: false,
message: "",
expectedMsg: "server_ip should be a valid ipv4",
Expand Down
25 changes: 25 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package govalidator

import "encoding/json"

const (
// JSON represents rule name which will be used to find the default error message.
JSON = "json"
// JSONMsg is the default error message format for fields with JSON validation rule.
JSONMsg = "%s should be a valid JSON"
)

// IsJSON checks if given string is a valid JSON.
//
// Example:
//
// v := validator.New()
// v.IsJSON("{"menu": {"id": "1", "value": "file"}}", "input", "input should be a valid JSON.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) IsJSON(j, field, msg string) Validator {
v.check(json.Valid([]byte(j)), field, v.msg(JSON, msg, field))

return v
}
78 changes: 78 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package govalidator

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_IsJSON(t *testing.T) {
tests := []struct {
name string
field string
value string
isPassed bool
message string
expectedMsg string
}{
{
name: "test {\"menu\": {\"id\": \"1\", \"value\": \"file\"}} is a valid JSON input",
field: "input",
value: "{\"menu\": {\"id\": \"1\", \"value\": \"file\"}}",
isPassed: true,
message: "",
expectedMsg: "",
},
{
name: "test empty string won't pass the JSON validation rule",
field: "input",
value: "",
isPassed: false,
message: "",
expectedMsg: "input should be a valid JSON",
},
{
name: "test empty space string won't pass the JSON validation rule",
field: "input",
value: " ",
isPassed: false,
message: "",
expectedMsg: "input should be a valid JSON",
},
{
name: "test `Reza` won't pass the JSON validation rule",
field: "input",
value: "Reza",
isPassed: false,
message: "",
expectedMsg: "input should be a valid JSON",
},
{
name: "test `{\"example\":2:]}}` won't pass the JSON validation rule",
field: "input",
value: "{\"example\":2:]}}",
isPassed: false,
message: "input should be a valid JSON",
expectedMsg: "input should be a valid JSON",
},
}

for _, test := range tests {
v := New()

v.IsJSON(test.value, test.field, test.message)

assert.Equal(t, test.isPassed, v.IsPassed(), test.name)

if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed, expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}
1 change: 1 addition & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var (
Before: BeforeMsg,
After: AfterMsg,
IP4: IP4Msg,
JSON: JSONMsg,
}

// ErrMethodMessageNotFound is the default message when a method does not have any error message on methodToErrorMessage.
Expand Down

0 comments on commit 6c6d08d

Please sign in to comment.