Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nil ptr dereference #17

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ func toString(v any) (string, bool) {

return "", false
}

func hasRequiredRule(rules []Rule) (Required, bool) {
for _, r := range rules {
if v, ok := r.(Required); ok {
return v, ok
}
}

return Required{}, false
}
38 changes: 22 additions & 16 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ func ValidateValue(ctx context.Context, value any, rules ...Rule) error {
return nil
}

if value == nil {
requiredRule, ok := hasRequiredRule(rules)
if !ok {
return nil
}

return requiredRule.ValidateValue(ctx, value)
}

dataSet, err := normalizeDataSet(value)
if err != nil {
return err
Expand All @@ -25,23 +34,16 @@ func ValidateValue(ctx context.Context, value any, rules ...Rule) error {
rules = normalizeRules(rules)
result := NewResult()

for _, validatorRule := range rules {
if _, ok := validatorRule.(Required); !ok {
if value == nil {
// if value is not required and is nil
continue
}
}

if err := validatorRule.ValidateValue(ctx, value); err != nil {
for _, r := range rules {
if err := r.ValidateValue(ctx, value); err != nil {
var errRes Result
if errors.As(err, &errRes) {
for _, rErr := range errRes.Errors() {
result = result.WithError(rErr)
}
} else {
return err
result = result.WithError(errRes.Errors()...)

continue
}

return err
}
}

Expand Down Expand Up @@ -110,11 +112,11 @@ func Validate(ctx context.Context, dataSet any, rules RuleSet) error {
for _, err := range errs {
err.Message = DefaultTranslator.Translate(ctx, err.Message, err.Params)
summaryResult = summaryResult.WithError(err)
//summaryResult = summaryResult.WithError(
// summaryResult = summaryResult.WithError(
// NewValidationError(DefaultTranslator.Translate(ctx, err.Message, err.Params)).
// WithParams(err.Params).
// WithValuePath(err.ValuePath),
//)
// )
}
}

Expand All @@ -126,6 +128,10 @@ func Validate(ctx context.Context, dataSet any, rules RuleSet) error {
}

func normalizeDataSet(ds any) (DataSet, error) {
if ds == nil {
return set.NewDataSetAny(ds), nil
}

rt := reflect.TypeOf(ds)
if rt.Kind() == reflect.Pointer {
rt = rt.Elem()
Expand Down
27 changes: 25 additions & 2 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestValidate_Int_Successfully(t *testing.T) {
func TestValidateValue_Int_Successfully(t *testing.T) {
ctx := context.Background()
rules := []Rule{
NewRequired(),
Expand All @@ -18,7 +18,7 @@ func TestValidate_Int_Successfully(t *testing.T) {
assert.NoError(t, err)
}

func TestValidate_Int_Failure(t *testing.T) {
func TestValidateValue_Int_Failure(t *testing.T) {
ctx := context.Background()
rules := []Rule{
NewRequired(),
Expand All @@ -39,6 +39,16 @@ func TestValidate_Int_Failure(t *testing.T) {
assert.Equal(t, expectedResult, err)
}

func TestValidateValue_IntNilPtrValue_Successfully(t *testing.T) {
ctx := context.Background()
rules := []Rule{
NewNumber(1, 3),
}

err := ValidateValue(ctx, nil, rules...)
assert.NoError(t, err)
}

func TestValidate_Map_Successfully(t *testing.T) {
ctx := context.Background()
rules := RuleSet{
Expand All @@ -54,3 +64,16 @@ func TestValidate_Map_Successfully(t *testing.T) {
err := Validate(ctx, data, rules)
assert.NoError(t, err)
}

func TestValidate_Nil_Failure(t *testing.T) {
ctx := context.Background()
rules := RuleSet{
"count": {
NewRequired(),
NewNumber(1, 3),
},
}

err := Validate(ctx, nil, rules)
assert.Error(t, err)
}
Loading