-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[commandbus] Add formal mechanism for validating commands #71
Comments
A flexible way to do it would be to create a separate |
Hi Max, It will be great to have this implemented and hope to help with it. As you suggested, I'm planning to add a middleware
type Command interface {
eh.Command
// Validate returns the error when validating the command.
Validate() error
}
func NewMiddleware() eh.CommandHandlerMiddleware {
return eh.CommandHandlerMiddleware(func(h eh.CommandHandler) eh.CommandHandler {
return eh.CommandHandlerFunc(func(ctx context.Context, cmd eh.Command) error {
if c, ok := cmd.(Command); ok {
err := c.Validate()
if err != nil {
return err
}
}
// Immediate command execution.
return h.HandleCommand(ctx, cmd)
})
})
} How do you feel about that? Any other idea? |
And also, we found a problem (maybe a separate issue) when a struct with private fields was used in a command. https://github.com/looplab/eventhorizon/blob/master/command.go#L155-L167 According to existing logic, private fields will be skipped and the struct will also be treated as zero value. e.g. a custom type from type customTime time.Time If validator can be added to each different command, can we make it an optional check? |
The approach for the validator looks good. As for the other issue, If you want to open a new issue for that it would also be welcome. |
Great I will go for this then. |
For example a validation method on the aggregate interface, or a middleware that runs before handling the command in the aggregate.
The text was updated successfully, but these errors were encountered: