diff --git a/pkg/bsql/validate.go b/pkg/bsql/validate.go index 5ab3da1d..86ff5bd7 100644 --- a/pkg/bsql/validate.go +++ b/pkg/bsql/validate.go @@ -212,5 +212,16 @@ func (l *ActionConfig) staticValidate(ctx context.Context, s *SQLSyncer) error { availableVars[k] = config.Type } + // An action defines either `query` or `queries`; both forms are accepted by + // ActionConfig.Validate and by the action handler, so validate whichever is set. + if len(l.Queries) > 0 { + for _, query := range l.Queries { + if err := validateVarsInQuery(s, query, availableVars); err != nil { + return err + } + } + return nil + } + return validateVarsInQuery(s, l.Query, availableVars) } diff --git a/pkg/bsql/validate_test.go b/pkg/bsql/validate_test.go index 89eb13e8..5c210e37 100644 --- a/pkg/bsql/validate_test.go +++ b/pkg/bsql/validate_test.go @@ -48,6 +48,44 @@ func TestValidate(t *testing.T) { }, expectErr: true, }, + { + name: "action with singular query", + validator: &ActionConfig{ + Query: "UPDATE users SET disabled = 1 WHERE id = ?", + Arguments: map[string]ArgumentConfig{ + "userid": {Type: "string"}, + }, + }, + expectErr: false, + }, + { + // Actions may define `queries` instead of `query`; validating only the + // singular field rejected every multi-statement action outright. + name: "action with queries", + validator: &ActionConfig{ + Queries: []string{ + "UPDATE users SET disabled = 1 WHERE id = ?", + "DELETE FROM user_sessions WHERE user_id = ?", + }, + Arguments: map[string]ArgumentConfig{ + "userid": {Type: "string"}, + }, + }, + expectErr: false, + }, + { + name: "action with queries referencing undefined var", + validator: &ActionConfig{ + Queries: []string{ + "UPDATE users SET disabled = 1 WHERE id = ?", + "DELETE FROM user_sessions WHERE user_id = ?", + }, + Arguments: map[string]ArgumentConfig{ + "userid": {Type: "string"}, + }, + }, + expectErr: true, + }, } for _, tc := range tcases {