Skip to content

Commit

Permalink
revert changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pkwarren committed Feb 26, 2024
1 parent 5435744 commit 33c8f56
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
19 changes: 9 additions & 10 deletions internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ func (bldr *Builder) buildField(
cache MessageCache,
) (field, error) {
fld := field{
Descriptor: fieldDescriptor,
Required: fieldConstraints.GetRequired(),
IgnoreEmpty: fieldDescriptor.HasPresence() || fieldConstraints.GetIgnoreEmpty() || fieldConstraints.GetIgnore() == validate.Ignore_IGNORE_IF_UNPOPULATED,
Descriptor: fieldDescriptor,
Required: fieldConstraints.GetRequired(),
IgnoreEmpty: fieldDescriptor.HasPresence() || fieldConstraints.GetIgnoreEmpty() || fieldConstraints.GetIgnore() == validate.Ignore_IGNORE_IF_UNPOPULATED || fieldConstraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE,
IgnoreDefault: fieldDescriptor.HasPresence() && fieldConstraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE,
}
if fld.IgnoreDefault {
fld.Zero = fieldDescriptor.Default()
}
err := bldr.buildValue(fieldDescriptor, fieldConstraints, false, &fld.Value, cache)
return fld, err
Expand Down Expand Up @@ -254,13 +258,8 @@ func (bldr *Builder) processIgnoreEmpty(
) error {
// the only time we need to ignore empty on a value is if it's evaluating a
// field item (repeated element or map key/value).
if forItems {
val.Ignore = constraints.GetIgnore()
if val.Ignore == validate.Ignore_IGNORE_UNSPECIFIED && constraints.GetIgnoreEmpty() {
val.Ignore = validate.Ignore_IGNORE_IF_UNPOPULATED
}
}
if val.Ignore != validate.Ignore_IGNORE_IF_UNPOPULATED && val.Ignore != validate.Ignore_IGNORE_IF_DEFAULT_VALUE {
val.IgnoreEmpty = forItems && constraints.GetIgnoreEmpty()
if !val.IgnoreEmpty {
// only need the zero value for checking ignore_empty constraint
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions internal/evaluator/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type field struct {
// This field is generally true for nullable fields or fields with the
// ignore_empty constraint explicitly set.
IgnoreEmpty bool
// IgnoreDefault indicates if a field should skip validation on its zero value,
// including for fields which have field presence and are set to the zero value.
IgnoreDefault bool
// Zero is the default or zero-value for this value's type
Zero protoreflect.Value
}

func (f field) Evaluate(val protoreflect.Value, failFast bool) error {
Expand Down
19 changes: 8 additions & 11 deletions internal/evaluator/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package evaluator

import (
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/reflect/protoreflect"
)

Expand All @@ -24,21 +23,19 @@ import (
type value struct {
// Constraints are the individual evaluators applied to a value
Constraints evaluators
// Ignore indicates whether the Constraints should be applied based on
// the value (unpopulated, default).
Ignore validate.Ignore
// IgnoreEmpty indicates that the Constraints should not be applied if the
// value is unset or the default (typically zero) value. This only applies to
// repeated elements or map keys/values with an ignore_empty rule.
IgnoreEmpty bool
// IgnoreDefault indicates if a field should skip validation on its zero value,
// including for fields which have field presence and are set to the zero value.
IgnoreDefault bool
// Zero is the default or zero-value for this value's type
Zero protoreflect.Value
}

func (v *value) Evaluate(val protoreflect.Value, failFast bool) error {
switch v.Ignore {
case validate.Ignore_IGNORE_UNSPECIFIED:
case validate.Ignore_IGNORE_IF_UNPOPULATED, validate.Ignore_IGNORE_IF_DEFAULT_VALUE:
if val.Equal(v.Zero) {
return nil
}
case validate.Ignore_IGNORE_ALWAYS:
if v.IgnoreEmpty && val.Equal(v.Zero) {
return nil
}
return v.Constraints.Evaluate(val, failFast)
Expand Down

0 comments on commit 33c8f56

Please sign in to comment.