GithubHelp home page GithubHelp logo

Comments (5)

apterf avatar apterf commented on May 21, 2024 1

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 validator
as middleware/commandhandler/validator/commandhandler.go.

  1. Add a command interface with method Validate()
type Command interface {
	eh.Command

	// Validate returns the error when validating the command.
	Validate() error
}
  1. The middleware will simply call the validation, if failed, return with the 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?

from eventhorizon.

maxekman avatar maxekman commented on May 21, 2024

A flexible way to do it would be to create a separate CommandHandler middleware that does only validation.

from eventhorizon.

apterf avatar apterf commented on May 21, 2024

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.

https://github.com/looplab/eventhorizon/blob/master/commandhandler/aggregate/commandhandler.go#L57-L60

e.g. a custom type from time.Time

type customTime time.Time

If validator can be added to each different command, can we make it an optional check?
It will be quite difficult to make it generic and always suitable for all cases.

from eventhorizon.

maxekman avatar maxekman commented on May 21, 2024

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.

from eventhorizon.

apterf avatar apterf commented on May 21, 2024

Great I will go for this then.

from eventhorizon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.