-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from rezakhademix/feature-add-json-validate-rule
feat: IsJSON rule added
- Loading branch information
Showing
4 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters