Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jchadwick-buf committed Nov 6, 2024
1 parent 657db35 commit 8c08015
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 33 deletions.
12 changes: 6 additions & 6 deletions internal/errors/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ func AppendFieldPath(err error, suffix *validate.FieldPathElement, skipSubscript
// significantly simpler to handle reverse-constructing paths with
// maps and slices.
if skipSubscript &&
len(violation.FieldPath) > 0 &&
violation.FieldPath[len(violation.FieldPath)-1].Subscript != nil {
len(violation.Field) > 0 &&
violation.Field[len(violation.Field)-1].Subscript != nil {
continue
}
violation.FieldPath = append(violation.FieldPath, suffix)
violation.Field = append(violation.Field, suffix)
}
}
}
Expand All @@ -100,9 +100,9 @@ func PrependRulePath(err error, prefix []*validate.FieldPathElement) {
if errors.As(err, &valErr) {
for _, violation := range valErr.Violations {
if violation, ok := violation.(*ViolationData); ok {
violation.RulePath = append(
violation.Rule = append(
append([]*validate.FieldPathElement{}, prefix...),

Check failure on line 104 in internal/errors/utils.go

View workflow job for this annotation

GitHub Actions / Go (1.21.x)

undefined: validate.FieldPathElement

Check failure on line 104 in internal/errors/utils.go

View workflow job for this annotation

GitHub Actions / Go (1.23.x)

undefined: validate.FieldPathElement

Check failure on line 104 in internal/errors/utils.go

View workflow job for this annotation

GitHub Actions / Go (1.22.x)

undefined: validate.FieldPathElement

Check failure on line 104 in internal/errors/utils.go

View workflow job for this annotation

GitHub Actions / Go (stable)

undefined: validate.FieldPathElement

Check failure on line 104 in internal/errors/utils.go

View workflow job for this annotation

GitHub Actions / Go (oldstable)

undefined: validate.FieldPathElement
violation.RulePath...,
violation.Rule...,
)
}
}
Expand All @@ -115,7 +115,7 @@ func ReverseFieldPaths(err error) {
if errors.As(err, &valErr) {
for _, violation := range valErr.Violations {
if violation, ok := violation.(*ViolationData); ok {
slices.Reverse(violation.FieldPath)
slices.Reverse(violation.Field)
}
}
}
Expand Down
33 changes: 16 additions & 17 deletions internal/errors/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (err *ValidationError) Error() string {
for _, violation := range err.Violations {
violation := violation.ToProto()
bldr.WriteString("\n - ")
if fieldPath := FieldPathString(violation.GetFieldPath().GetElements()); fieldPath != "" {
if fieldPath := FieldPathString(violation.GetField().GetElements()); fieldPath != "" {
bldr.WriteString(fieldPath)
bldr.WriteString(": ")
}
Expand All @@ -78,14 +78,13 @@ func (err *ValidationError) Error() string {

// ViolationData is a simple implementation of Violation.
type ViolationData struct {
FieldPath []*validate.FieldPathElement
RulePath []*validate.FieldPathElement
FieldValue protoreflect.Value
RuleValue protoreflect.Value
FieldDescriptor protoreflect.FieldDescriptor
ForKey bool
ConstraintID string
Message string
Field []*validate.FieldPathElement

Check failure on line 81 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (1.21.x)

undefined: validate.FieldPathElement

Check failure on line 81 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (1.23.x)

undefined: validate.FieldPathElement

Check failure on line 81 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (1.22.x)

undefined: validate.FieldPathElement

Check failure on line 81 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (stable)

undefined: validate.FieldPathElement

Check failure on line 81 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (oldstable)

undefined: validate.FieldPathElement
Rule []*validate.FieldPathElement

Check failure on line 82 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (1.21.x)

undefined: validate.FieldPathElement

Check failure on line 82 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (1.23.x)

undefined: validate.FieldPathElement

Check failure on line 82 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (1.22.x)

undefined: validate.FieldPathElement

Check failure on line 82 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (stable)

undefined: validate.FieldPathElement

Check failure on line 82 in internal/errors/validation.go

View workflow job for this annotation

GitHub Actions / Go (oldstable)

undefined: validate.FieldPathElement
FieldValue protoreflect.Value
RuleValue protoreflect.Value
ConstraintID string
Message string
ForKey bool
}

func (v *ViolationData) GetFieldValue() protoreflect.Value {
Expand All @@ -98,20 +97,20 @@ func (v *ViolationData) GetRuleValue() protoreflect.Value {

func (v *ViolationData) ToProto() *validate.Violation {
var fieldPathString *string
if len(v.FieldPath) > 0 {
fieldPathString = proto.String(FieldPathString(v.FieldPath))
if len(v.Field) > 0 {
fieldPathString = proto.String(FieldPathString(v.Field))
}
var forKey *bool
if v.ForKey {
forKey = proto.Bool(true)
}
return &validate.Violation{
FieldPathString: fieldPathString,
FieldPath: fieldPathProto(v.FieldPath),
RulePath: fieldPathProto(v.RulePath),
ConstraintId: proto.String(v.ConstraintID),
Message: proto.String(v.Message),
ForKey: forKey,
Field: fieldPathProto(v.Field),
Rule: fieldPathProto(v.Rule),
FieldPath: fieldPathString,
ConstraintId: proto.String(v.ConstraintID),
Message: proto.String(v.Message),
ForKey: forKey,
}
}

Expand Down
12 changes: 10 additions & 2 deletions internal/evaluator/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type anyPB struct {
In map[string]struct{}
// NotIn specifies which type URLs the value may not possess
NotIn map[string]struct{}
// InValue contains the original `in` rule value.
InValue protoreflect.Value
// NotInValue contains the original `not_in` rule value.
NotInValue protoreflect.Value
}

func (a anyPB) Evaluate(val protoreflect.Value, failFast bool) error {
Expand All @@ -54,7 +58,9 @@ func (a anyPB) Evaluate(val protoreflect.Value, failFast bool) error {
if len(a.In) > 0 {
if _, ok := a.In[typeURL]; !ok {
err.Violations = append(err.Violations, &errors.ViolationData{
RulePath: anyInRulePath,
Rule: anyInRulePath,
FieldValue: val,
RuleValue: a.InValue,
ConstraintID: "any.in",
Message: "type URL must be in the allow list",
})
Expand All @@ -67,7 +73,9 @@ func (a anyPB) Evaluate(val protoreflect.Value, failFast bool) error {
if len(a.NotIn) > 0 {
if _, ok := a.NotIn[typeURL]; ok {
err.Violations = append(err.Violations, &errors.ViolationData{
RulePath: anyNotInRulePath,
Rule: anyNotInRulePath,
FieldValue: val,
RuleValue: a.NotInValue,
ConstraintID: "any.not_in",
Message: "type URL must not be in the block list",
})
Expand Down
5 changes: 5 additions & 0 deletions internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,15 @@ func (bldr *Builder) processAnyConstraints(
}

typeURLDesc := fdesc.Message().Fields().ByName("type_url")
anyPbDesc := (&validate.AnyRules{}).ProtoReflect().Descriptor()
inField := anyPbDesc.Fields().ByName("in")
notInField := anyPbDesc.Fields().ByName("not_in")
anyEval := anyPB{
TypeURLDescriptor: typeURLDesc,
In: stringsToSet(fieldConstraints.GetAny().GetIn()),
NotIn: stringsToSet(fieldConstraints.GetAny().GetNotIn()),
InValue: fieldConstraints.GetAny().ProtoReflect().Get(inField),
NotInValue: fieldConstraints.GetAny().ProtoReflect().Get(notInField),
}
appendEvaluator(valEval, anyEval, itemsWrapper)
return nil
Expand Down
4 changes: 3 additions & 1 deletion internal/evaluator/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ type definedEnum struct {
func (d definedEnum) Evaluate(val protoreflect.Value, _ bool) error {
if d.ValueDescriptors.ByNumber(val.Enum()) == nil {
return &errors.ValidationError{Violations: []errors.Violation{&errors.ViolationData{
RulePath: enumDefinedOnlyRulePath,
Rule: enumDefinedOnlyRulePath,
FieldValue: val,
RuleValue: protoreflect.ValueOfBool(true),
ConstraintID: "enum.defined_only",
Message: "value must be one of the defined enum values",
}}}
Expand Down
5 changes: 3 additions & 2 deletions internal/evaluator/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ func (f field) Evaluate(val protoreflect.Value, failFast bool) error {
func (f field) EvaluateMessage(msg protoreflect.Message, failFast bool) (err error) {
if f.Required && !msg.Has(f.Descriptor) {
err := &errors.ValidationError{Violations: []errors.Violation{&errors.ViolationData{
FieldPath: []*validate.FieldPathElement{
Field: []*validate.FieldPathElement{
errors.FieldPathElement(f.Descriptor),
},
RulePath: requiredRulePath,
Rule: requiredRulePath,
RuleValue: protoreflect.ValueOfBool(true),
ConstraintID: "required",
Message: "value is required",
}}}
Expand Down
2 changes: 1 addition & 1 deletion internal/evaluator/oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (o oneof) Evaluate(val protoreflect.Value, failFast bool) error {
func (o oneof) EvaluateMessage(msg protoreflect.Message, _ bool) error {
if o.Required && msg.WhichOneof(o.Descriptor) == nil {
return &errors.ValidationError{Violations: []errors.Violation{&errors.ViolationData{
FieldPath: []*validate.FieldPathElement{{
Field: []*validate.FieldPathElement{{
FieldName: proto.String(string(o.Descriptor.Name())),
}},
ConstraintID: "required",
Expand Down
1 change: 1 addition & 0 deletions internal/expression/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (set ASTSet) ReduceResiduals(opts ...cel.ProgramOption) (ProgramSet, error)
AST: residual,
Source: ast.Source,
Path: ast.Path,
Value: ast.Value,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/expression/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (expr compiledProgram) eval(bindings *Variable) (*errors.ViolationData, err
return nil, nil
}
return &errors.ViolationData{
RulePath: expr.Path,
Rule: expr.Path,
RuleValue: expr.Value,
ConstraintID: expr.Source.GetId(),
Message: val,
Expand All @@ -121,7 +121,7 @@ func (expr compiledProgram) eval(bindings *Variable) (*errors.ViolationData, err
return nil, nil
}
return &errors.ViolationData{
RulePath: expr.Path,
Rule: expr.Path,
RuleValue: expr.Value,
ConstraintID: expr.Source.GetId(),
Message: expr.Source.GetMessage(),
Expand Down
7 changes: 5 additions & 2 deletions validator_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ func ExampleValidationError() {
err = validator.Validate(loc)
var valErr *ValidationError
if ok := errors.As(err, &valErr); ok {
violation := valErr.Violations[0].ToProto()
fmt.Println(FieldPathString(violation.GetFieldPath()), violation.GetConstraintId())
violation := valErr.Violations[0]
violationProto := violation.ToProto()
fmt.Println(FieldPathString(violationProto.GetField()), violationProto.GetConstraintId())
fmt.Println(violation.GetRuleValue(), violation.GetFieldValue())
}

// output: lat double.gte_lte
// -90 999.999
}

0 comments on commit 8c08015

Please sign in to comment.