-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
339 additions
and
3 deletions.
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
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,97 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type JSON struct { | ||
message string | ||
|
||
whenFunc WhenFunc | ||
skipEmpty bool | ||
skipError bool | ||
} | ||
|
||
func NewJSON() *JSON { | ||
return &JSON{ | ||
message: "Must be a valid JSON", | ||
} | ||
} | ||
|
||
func (j *JSON) ValidateValue(_ context.Context, value any) error { | ||
var bytes []byte | ||
|
||
switch v := value.(type) { | ||
case string: | ||
bytes = []byte(v) | ||
case *string: | ||
bytes = []byte(*v) | ||
case []byte: | ||
bytes = v | ||
case *[]byte: | ||
bytes = *v | ||
case json.RawMessage: | ||
bytes = v | ||
case *json.RawMessage: | ||
bytes = *v | ||
case fmt.Stringer: | ||
if i, ok := v.(fmt.Stringer); ok { | ||
bytes = []byte(i.String()) | ||
} | ||
default: | ||
return NewResult().WithError(NewValidationError(j.message)) | ||
} | ||
|
||
if isValid := json.Valid(bytes); !isValid { | ||
return NewResult().WithError(NewValidationError(j.message)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (j *JSON) When(v WhenFunc) *JSON { | ||
rc := *j | ||
rc.whenFunc = v | ||
|
||
return &rc | ||
} | ||
|
||
func (j *JSON) when() WhenFunc { | ||
return j.whenFunc | ||
} | ||
|
||
func (j *JSON) setWhen(v WhenFunc) { | ||
j.whenFunc = v | ||
} | ||
|
||
func (j *JSON) SkipOnEmpty() *JSON { | ||
rc := *j | ||
rc.skipEmpty = true | ||
|
||
return &rc | ||
} | ||
|
||
func (j *JSON) skipOnEmpty() bool { | ||
return j.skipEmpty | ||
} | ||
|
||
func (j *JSON) setSkipOnEmpty(v bool) { | ||
j.skipEmpty = v | ||
} | ||
|
||
func (j *JSON) SkipOnError() *JSON { | ||
rs := *j | ||
rs.skipError = true | ||
|
||
return &rs | ||
} | ||
|
||
func (j *JSON) shouldSkipOnError() bool { | ||
return j.skipError | ||
} | ||
|
||
func (j *JSON) setSkipOnError(v bool) { | ||
j.skipError = 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,241 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type stringer string | ||
|
||
func (s stringer) String() string { | ||
return string(s) | ||
} | ||
|
||
func TestJSON_ValidateValue_Successfully(t *testing.T) { | ||
// region data provider | ||
ctx := context.Background() | ||
|
||
str := `"hello world"` | ||
obj := `{"hello":"world"}` | ||
arr := `["hello","world"]` | ||
|
||
bytesWithString := []byte(str) | ||
bytesWithObject := []byte(obj) | ||
bytesWithArray := []byte(arr) | ||
|
||
jsonRawWithString := json.RawMessage(str) | ||
jsonRawWithObject := json.RawMessage(obj) | ||
jsonRawWithArray := json.RawMessage(arr) | ||
|
||
type args struct { | ||
value any | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "stringer with string", | ||
args: args{value: stringer(str)}, | ||
}, | ||
{ | ||
name: "string", | ||
args: args{value: str}, | ||
}, | ||
{ | ||
name: "string object", | ||
args: args{value: obj}, | ||
}, | ||
{ | ||
name: "string array", | ||
args: args{value: arr}, | ||
}, | ||
{ | ||
name: "string pointer", | ||
args: args{value: &str}, | ||
}, | ||
{ | ||
name: "string pointer object", | ||
args: args{value: &obj}, | ||
}, | ||
{ | ||
name: "string pointer array", | ||
args: args{value: &arr}, | ||
}, | ||
{ | ||
name: "bytes with string", | ||
args: args{value: bytesWithString}, | ||
}, | ||
{ | ||
name: "bytes with object", | ||
args: args{value: bytesWithObject}, | ||
}, | ||
{ | ||
name: "bytes with array", | ||
args: args{value: bytesWithArray}, | ||
}, | ||
{ | ||
name: "bytes pointer with string", | ||
args: args{value: &bytesWithString}, | ||
}, | ||
{ | ||
name: "bytes with object", | ||
args: args{value: &bytesWithObject}, | ||
}, | ||
{ | ||
name: "bytes with array", | ||
args: args{value: &bytesWithArray}, | ||
}, | ||
{ | ||
name: "json.RawMessage with string", | ||
args: args{value: jsonRawWithString}, | ||
}, | ||
{ | ||
name: "json.RawMessage with object", | ||
args: args{value: jsonRawWithObject}, | ||
}, | ||
{ | ||
name: "json.RawMessage with array", | ||
args: args{value: jsonRawWithArray}, | ||
}, | ||
{ | ||
name: "json.RawMessage pointer with string", | ||
args: args{value: &jsonRawWithString}, | ||
}, | ||
{ | ||
name: "json.RawMessage pointer with object", | ||
args: args{value: &jsonRawWithObject}, | ||
}, | ||
{ | ||
name: "json.RawMessage pointer with array", | ||
args: args{value: &jsonRawWithArray}, | ||
}, | ||
} | ||
// endregion | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
j := NewJSON() | ||
err := j.ValidateValue(ctx, tt.args.value) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestJSON_ValidateValue_Failure(t *testing.T) { | ||
// region data provider | ||
ctx := context.Background() | ||
|
||
str := `hello world` | ||
obj := `{hello":"world"` | ||
arr := `["hello","world` | ||
|
||
bytesWithString := []byte(str) | ||
bytesWithObject := []byte(obj) | ||
bytesWithArray := []byte(arr) | ||
|
||
jsonRawWithString := json.RawMessage(str) | ||
jsonRawWithObject := json.RawMessage(obj) | ||
jsonRawWithArray := json.RawMessage(arr) | ||
|
||
type args struct { | ||
value any | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "int", | ||
args: args{value: 6}, | ||
}, | ||
{ | ||
name: "stringer with string", | ||
args: args{value: stringer(str)}, | ||
}, | ||
{ | ||
name: "string", | ||
args: args{value: str}, | ||
}, | ||
{ | ||
name: "string object", | ||
args: args{value: obj}, | ||
}, | ||
{ | ||
name: "string array", | ||
args: args{value: arr}, | ||
}, | ||
{ | ||
name: "string pointer", | ||
args: args{value: &str}, | ||
}, | ||
{ | ||
name: "string pointer object", | ||
args: args{value: &obj}, | ||
}, | ||
{ | ||
name: "string pointer array", | ||
args: args{value: &arr}, | ||
}, | ||
{ | ||
name: "bytes with string", | ||
args: args{value: bytesWithString}, | ||
}, | ||
{ | ||
name: "bytes with object", | ||
args: args{value: bytesWithObject}, | ||
}, | ||
{ | ||
name: "bytes with array", | ||
args: args{value: bytesWithArray}, | ||
}, | ||
{ | ||
name: "bytes pointer with string", | ||
args: args{value: &bytesWithString}, | ||
}, | ||
{ | ||
name: "bytes with object", | ||
args: args{value: &bytesWithObject}, | ||
}, | ||
{ | ||
name: "bytes with array", | ||
args: args{value: &bytesWithArray}, | ||
}, | ||
{ | ||
name: "json.RawMessage with string", | ||
args: args{value: jsonRawWithString}, | ||
}, | ||
{ | ||
name: "json.RawMessage with object", | ||
args: args{value: jsonRawWithObject}, | ||
}, | ||
{ | ||
name: "json.RawMessage with array", | ||
args: args{value: jsonRawWithArray}, | ||
}, | ||
{ | ||
name: "json.RawMessage pointer with string", | ||
args: args{value: &jsonRawWithString}, | ||
}, | ||
{ | ||
name: "json.RawMessage pointer with object", | ||
args: args{value: &jsonRawWithObject}, | ||
}, | ||
{ | ||
name: "json.RawMessage pointer with array", | ||
args: args{value: &jsonRawWithArray}, | ||
}, | ||
} | ||
// endregion | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
j := NewJSON() | ||
err := j.ValidateValue(ctx, tt.args.value) | ||
require.Error(t, err) | ||
}) | ||
} | ||
} |