Skip to content

Commit

Permalink
Make sure to validate the AST in addition to just selectors and values
Browse files Browse the repository at this point in the history
This could be useful when the AST we are given doesn’t necessarily come from our internal parser.
  • Loading branch information
mkeeler committed Apr 3, 2019
1 parent 50165c7 commit 4007189
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,31 @@ import (
func validateRecurse(ast Expression, fields FieldConfigurations, maxRawValueLength int) (int, error) {
switch node := ast.(type) {
case *UnaryExpression:
return validateRecurse(node, fields, maxRawValueLength)
switch node.Operator {
case UnaryOpNot:
// this is fine
default:
return 0, fmt.Errorf("Invalid unary expression operator: %d", node.Operator)
}

if node.Operand == nil {
return 0, fmt.Errorf("Invalid unary expression operand: nil")
}
return validateRecurse(node.Operand, fields, maxRawValueLength)
case *BinaryExpression:
switch node.Operator {
case BinaryOpAnd, BinaryOpOr:
// this is fine
default:
return 0, fmt.Errorf("Invalid binary expression operator: %d", node.Operator)
}

if node.Left == nil {
return 0, fmt.Errorf("Invalid left hand side of binary expression: nil")
} else if node.Right == nil {
return 0, fmt.Errorf("Invalid right hand side of binary expression: nil")
}

leftMatches, err := validateRecurse(node.Left, fields, maxRawValueLength)
if err != nil {
return leftMatches, err
Expand Down Expand Up @@ -73,6 +96,13 @@ func validateRecurse(ast Expression, fields FieldConfigurations, maxRawValueLeng

node.Value.Converted = coerced
}
} else {
switch node.Operator {
case MatchIsEmpty, MatchIsNotEmpty:
// these don't require values
default:
return 1, fmt.Errorf("Match operator %q requires a non-nil value", node.Operator)
}
}
return 1, nil
}
Expand Down

0 comments on commit 4007189

Please sign in to comment.