-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfield.go
117 lines (102 loc) · 3.92 KB
/
field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2023-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package protovalidate
import (
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
//nolint:gochecknoglobals
var (
requiredRuleDescriptor = (&validate.FieldRules{}).ProtoReflect().Descriptor().Fields().ByName("required")
requiredRulePath = &validate.FieldPath{
Elements: []*validate.FieldPathElement{
fieldPathElement(requiredRuleDescriptor),
},
}
)
// field performs validation on a single message field, defined by its
// descriptor.
type field struct {
// Value is the evaluator to apply to the field's value
Value value
// Required indicates that the field must have a set value.
Required bool
// HasPresence reports whether the field distinguishes between unpopulated
// and default values.
HasPresence bool
// Whether validation should be ignored for certain conditions.
Ignore validate.Ignore
// Zero is the default or zero-value for this value's type
Zero protoreflect.Value
// Err stores if there was a compilation error constructing this evaluator. It is stored
// here so that it can be returned as part of validating this specific field.
Err error
}
// shouldIgnoreAlways returns whether this field should always skip validation.
// If true, this will take precedence and all checks are skipped.
func (f field) shouldIgnoreAlways() bool {
return f.Ignore == validate.Ignore_IGNORE_ALWAYS
}
// shouldIgnoreEmpty returns whether this field should skip validation on its zero value.
// This field is generally true for nullable fields or fields with the
// ignore_empty rule explicitly set.
func (f field) shouldIgnoreEmpty() bool {
return f.HasPresence || f.Ignore == validate.Ignore_IGNORE_IF_UNPOPULATED || f.Ignore == validate.Ignore_IGNORE_IF_DEFAULT_VALUE
}
// shouldIgnoreDefault returns whether this field should skip validation on its zero value,
// including for fields which have field presence and are set to the zero value.
func (f field) shouldIgnoreDefault() bool {
return f.HasPresence && f.Ignore == validate.Ignore_IGNORE_IF_DEFAULT_VALUE
}
func (f field) Evaluate(_ protoreflect.Message, val protoreflect.Value, cfg *validationConfig) error {
return f.EvaluateMessage(val.Message(), cfg)
}
func (f field) EvaluateMessage(msg protoreflect.Message, cfg *validationConfig) (err error) {
if f.shouldIgnoreAlways() {
return nil
}
if !cfg.filter.ShouldValidate(msg, f.Value.Descriptor) {
return nil
}
if f.Err != nil {
return f.Err
}
if f.Required && !msg.Has(f.Value.Descriptor) {
return &ValidationError{Violations: []*Violation{{
Proto: &validate.Violation{
Field: fieldPath(f.Value.Descriptor),
Rule: prefixRulePath(f.Value.NestedRule, requiredRulePath),
RuleId: proto.String("required"),
Message: proto.String("value is required"),
},
FieldValue: protoreflect.Value{},
FieldDescriptor: f.Value.Descriptor,
RuleValue: protoreflect.ValueOfBool(true),
RuleDescriptor: requiredRuleDescriptor,
}}}
}
if f.shouldIgnoreEmpty() && !msg.Has(f.Value.Descriptor) {
return nil
}
val := msg.Get(f.Value.Descriptor)
if f.shouldIgnoreDefault() && val.Equal(f.Zero) {
return nil
}
return f.Value.EvaluateField(msg, val, cfg, true)
}
func (f field) Tautology() bool {
return !f.Required && f.Value.Tautology() && f.Err == nil
}
var _ messageEvaluator = field{}