-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validator): new
PayloadValidationOptions
type (#10)
Accepts values for both `ValidationOptions` and `DecoderOptions`. The appropriate set of options is passed to `Validate()` and `Decode()`, respectively.
- Loading branch information
Showing
2 changed files
with
72 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package validators | |
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/oleoneto/go-structs/structs" | ||
) | ||
|
||
type Identifiable struct { | ||
|
@@ -271,7 +273,7 @@ func Test_ValidatePayload(t *testing.T) { | |
type args struct { | ||
data []byte | ||
model any | ||
options ValidationOptions | ||
options PayloadValidationOptions | ||
} | ||
|
||
tests := []struct { | ||
|
@@ -282,9 +284,17 @@ func Test_ValidatePayload(t *testing.T) { | |
{ | ||
name: "person - 1", | ||
args: args{ | ||
data: []byte(`{"name": "", "contact": {"emails": []}}`), | ||
model: &Person{}, | ||
options: ValidationOptions{}, | ||
data: []byte(`{"name": "", "contact": {"emails": []}}`), | ||
model: &Person{}, | ||
options: PayloadValidationOptions{ | ||
DecoderOptions: structs.DecoderOptions{ | ||
Rules: []structs.SchemaValidationRule{ | ||
structs.ADDITIONAL_PROPERTY, | ||
structs.INVALID_TYPE, | ||
structs.REQUIRED_ATTRIBUTE, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[string][]string{ | ||
"id": {"REQUIRED_ATTRIBUTE_MISSING"}, | ||
|
@@ -295,21 +305,36 @@ func Test_ValidatePayload(t *testing.T) { | |
{ | ||
name: "person - 2", | ||
args: args{ | ||
data: []byte(`{"id": "1108129d-1d98-4a21-837a-ae6319f64c73", "name": 1, "contact": {"emails": ["}}`), | ||
model: &Person{}, | ||
options: ValidationOptions{}, | ||
data: []byte(`{"id": "1108129d-1d98-4a21-837a-ae6319f64c73", "name": 1, "contact": {"emails": ["}}`), | ||
model: &Person{}, | ||
options: PayloadValidationOptions{ | ||
DecoderOptions: structs.DecoderOptions{ | ||
Rules: []structs.SchemaValidationRule{ | ||
structs.ADDITIONAL_PROPERTY, | ||
structs.INVALID_TYPE, | ||
structs.REQUIRED_ATTRIBUTE, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
want: map[string][]string{ | ||
"_": {"INVALID_PAYLOAD"}, | ||
}, | ||
}, | ||
{ | ||
name: "person - 3", | ||
args: args{ | ||
data: []byte(`{"id": "2b852002-f19d-11ec-8ea0-0242ac120002", "name": 1, "contact": {"emails": ["leo", "[email protected]"]}}`), | ||
model: &Person{}, | ||
options: ValidationOptions{}, | ||
data: []byte(`{"id": "2b852002-f19d-11ec-8ea0-0242ac120002", "name": 1, "contact": {"emails": ["leo", "[email protected]"]}}`), | ||
model: &Person{}, | ||
options: PayloadValidationOptions{ | ||
DecoderOptions: structs.DecoderOptions{ | ||
Rules: []structs.SchemaValidationRule{ | ||
structs.ADDITIONAL_PROPERTY, | ||
structs.INVALID_TYPE, | ||
structs.REQUIRED_ATTRIBUTE, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[string][]string{ | ||
"name": {"INVALID_TYPE"}, | ||
|
@@ -319,18 +344,34 @@ func Test_ValidatePayload(t *testing.T) { | |
{ | ||
name: "person - 4", | ||
args: args{ | ||
data: []byte(`{"id": "2b852002-f19d-11ec-8ea0-0242ac120002", "name": "Leonardo", "contact": {"emails": ["[email protected]"]}}`), | ||
model: &Person{}, | ||
options: ValidationOptions{}, | ||
data: []byte(`{"id": "2b852002-f19d-11ec-8ea0-0242ac120002", "name": "Leonardo", "contact": {"emails": ["[email protected]"]}}`), | ||
model: &Person{}, | ||
options: PayloadValidationOptions{ | ||
DecoderOptions: structs.DecoderOptions{ | ||
Rules: []structs.SchemaValidationRule{ | ||
structs.ADDITIONAL_PROPERTY, | ||
structs.INVALID_TYPE, | ||
structs.REQUIRED_ATTRIBUTE, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[string][]string{}, | ||
}, | ||
{ | ||
name: "resource - 1", | ||
args: args{ | ||
model: &Resource{}, | ||
data: []byte(`{"currency": "BRL", "price": 14, "group": 7, "type": "NEW", "code": "ABC12", "qty": 2, "rating": 5, "related": ["123", "145"], "published_at": "2020-01-01T00:00:00+01:00", "id": "some-id"}`), | ||
options: ValidationOptions{}, | ||
model: &Resource{}, | ||
data: []byte(`{"currency": "BRL", "price": 14, "group": 7, "type": "NEW", "code": "ABC12", "qty": 2, "rating": 5, "related": ["123", "145"], "published_at": "2020-01-01T00:00:00+01:00", "id": "some-id"}`), | ||
options: PayloadValidationOptions{ | ||
DecoderOptions: structs.DecoderOptions{ | ||
Rules: []structs.SchemaValidationRule{ | ||
structs.ADDITIONAL_PROPERTY, | ||
structs.INVALID_TYPE, | ||
structs.REQUIRED_ATTRIBUTE, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[string][]string{ | ||
"id": {"ADDITIONAL_PROPERTY"}, | ||
|