-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
variables_in_allowed_position.go
40 lines (34 loc) · 1.13 KB
/
variables_in_allowed_position.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
package validator
import (
"github.com/open-policy-agent/opa/internal/gqlparser/ast"
//nolint:revive // Validator rules each use dot imports for convenience.
. "github.com/open-policy-agent/opa/internal/gqlparser/validator"
)
func init() {
AddRule("VariablesInAllowedPosition", func(observers *Events, addError AddErrFunc) {
observers.OnValue(func(walker *Walker, value *ast.Value) {
if value.Kind != ast.Variable || value.ExpectedType == nil || value.VariableDefinition == nil || walker.CurrentOperation == nil {
return
}
tmp := *value.ExpectedType
// todo: move me into walk
// If there is a default non nullable types can be null
if value.VariableDefinition.DefaultValue != nil && value.VariableDefinition.DefaultValue.Kind != ast.NullValue {
if value.ExpectedType.NonNull {
tmp.NonNull = false
}
}
if !value.VariableDefinition.Type.IsCompatible(&tmp) {
addError(
Message(
`Variable "%s" of type "%s" used in position expecting type "%s".`,
value,
value.VariableDefinition.Type.String(),
value.ExpectedType.String(),
),
At(value.Position),
)
}
})
})
}