-
Notifications
You must be signed in to change notification settings - Fork 1
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 #26 from Rhyanz46/avoidnullfield
requiredWithout
- Loading branch information
Showing
7 changed files
with
180 additions
and
60 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
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,61 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/Rhyanz46/go-map-validator/map_validator" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRequiredWithout(t *testing.T) { | ||
check, err := map_validator.NewValidateBuilder().SetRules(map_validator.RulesWrapper{ | ||
Rules: map[string]map_validator.Rules{ | ||
"name": {Type: reflect.String}, | ||
"flavor": {Type: reflect.String, RequiredWithout: []string{"custom_flavor"}}, | ||
"custom_flavor": {Type: reflect.String, RequiredWithout: []string{"flavor"}}, | ||
}, | ||
}).Load(map[string]interface{}{ | ||
"name": "SSD", | ||
}) | ||
if err != nil { | ||
t.Errorf("Expected not have error, but got error : %s", err) | ||
return | ||
} | ||
|
||
expected := "is null you need to put value in this" | ||
_, err = check.RunValidate() | ||
if !strings.Contains(err.Error(), expected) { | ||
t.Errorf("Expected error with text at least %s, but we got %s", expected, err) | ||
} | ||
} | ||
|
||
func TestChildRequiredWithout(t *testing.T) { | ||
role := map_validator.RulesWrapper{ | ||
Rules: map[string]map_validator.Rules{ | ||
"data": {Object: &map_validator.RulesWrapper{ | ||
Rules: map[string]map_validator.Rules{ | ||
"name": {Type: reflect.String}, | ||
"expired": {Type: reflect.String, Null: true}, | ||
"flavor": {Type: reflect.String, RequiredWithout: []string{"custom_flavor", "size"}}, | ||
"custom_flavor": {Type: reflect.String, RequiredWithout: []string{"flavor", "size"}}, | ||
"size": {Type: reflect.Int, RequiredWithout: []string{"flavor", "custom_flavor"}}, | ||
}, | ||
}}, | ||
}, | ||
} | ||
payload := map[string]interface{}{ | ||
"data": map[string]interface{}{ | ||
"name": "sabalong", | ||
}, | ||
} | ||
check, err := map_validator.NewValidateBuilder().SetRules(role).Load(payload) | ||
if err != nil { | ||
t.Errorf("Expected not have error, but got error : %s", err) | ||
return | ||
} | ||
expected := "is null you need to put value in this" | ||
_, err = check.RunValidate() | ||
if !strings.Contains(err.Error(), expected) { | ||
t.Errorf("Expected error with text at least %s, but we got %s", expected, err) | ||
} | ||
} |
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