Skip to content

Commit

Permalink
ValidateValue func back to public
Browse files Browse the repository at this point in the history
  • Loading branch information
raoptimus committed Mar 26, 2024
1 parent fb3da81 commit 4872ea5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion each.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (r *Each) ValidateValue(ctx context.Context, value any) error {
for i := 0; i < vs.Len(); i++ {
v := vs.Index(i).Interface()

if err := validateValue(ctx, v, r.rules...); err != nil {
if err := ValidateValue(ctx, v, r.rules...); err != nil {
var r Result
if errors.As(err, &r) {
for _, err := range r.Errors() {
Expand Down
2 changes: 1 addition & 1 deletion nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (r *Nested) ValidateValue(ctx context.Context, value any) error {
}
valuePath := data.FieldAliasName(fieldName)

if err := validateValue(ctx, validatedValue, rules...); err != nil {
if err := ValidateValue(ctx, validatedValue, rules...); err != nil {
var itemResult Result

if errors.As(err, &itemResult) {
Expand Down
108 changes: 54 additions & 54 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,60 @@ import (
"github.com/raoptimus/validator.go/v2/set"
)

func ValidateValue(ctx context.Context, value any, rules ...Rule) error {
if len(rules) == 0 {
return nil
}

if value == nil {
// todo: should be moved to Nested validator
requiredRule, ok := hasRequiredRule(rules)
if !ok {
return nil
}

return requiredRule.ValidateValue(ctx, value)
}

dataSet, err := normalizeDataSet(value)
if err != nil {
return err
}

if extDS, ok := ExtractDataSet[DataSet](ctx); !ok || value != extDS {
ctx = withDataSet(ctx, dataSet)
}

rules = normalizeRules(rules)
result := NewResult()

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

continue
}

return err
}
}

if result.IsValid() {
return nil
}

for _, err := range result.Errors() {
err.Message = DefaultTranslator.Translate(ctx, err.Message, err.Params)
}

return result
}

func Validate(ctx context.Context, dataSet any, rules RuleSet) error {
normalizedDS, err := normalizeDataSet(dataSet) // 2 allocs
if err != nil {
Expand Down Expand Up @@ -80,60 +134,6 @@ func Validate(ctx context.Context, dataSet any, rules RuleSet) error {
return nil
}

func validateValue(ctx context.Context, value any, rules ...Rule) error {
if len(rules) == 0 {
return nil
}

if value == nil {
// todo: should be moved to Nested validator
requiredRule, ok := hasRequiredRule(rules)
if !ok {
return nil
}

return requiredRule.ValidateValue(ctx, value)
}

dataSet, err := normalizeDataSet(value)
if err != nil {
return err
}

if extDS, ok := ExtractDataSet[DataSet](ctx); !ok || value != extDS {
ctx = withDataSet(ctx, dataSet)
}

rules = normalizeRules(rules)
result := NewResult()

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

continue
}

return err
}
}

if result.IsValid() {
return nil
}

for _, err := range result.Errors() {
err.Message = DefaultTranslator.Translate(ctx, err.Message, err.Params)
}

return result
}

func normalizeDataSet(ds any) (DataSet, error) {
if ds == nil {
return set.NewDataSetAny(ds), nil
Expand Down
8 changes: 4 additions & 4 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestValidateValue_Int_Successfully(t *testing.T) {
NewNumber(1, 3),
}

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

Expand All @@ -25,7 +25,7 @@ func TestValidateValue_Int_Failure(t *testing.T) {
NewNumber(1, 3),
}

err := validateValue(ctx, 0, rules...)
err := ValidateValue(ctx, 0, rules...)
assert.Error(t, err)
assert.Equal(t, "Value cannot be blank. Value must be no less than 1.", err.Error())

Expand All @@ -45,7 +45,7 @@ func TestValidateValue_IntNilPtrValue_Successfully(t *testing.T) {
NewNumber(1, 3),
}

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

Expand All @@ -57,7 +57,7 @@ func TestValidateValue_IntPtrValue_Successfully(t *testing.T) {

v := 2

err := validateValue(ctx, &v, rules...)
err := ValidateValue(ctx, &v, rules...)
assert.NoError(t, err)
}

Expand Down

0 comments on commit 4872ea5

Please sign in to comment.