modernice / goes

goes is an event-sourcing framework for Go.
https://goes.modernice.dev
Apache License 2.0
134 stars 12 forks source link

[feature] Command Payload Validation #228

Open bounoable opened 1 month ago

bounoable commented 1 month ago

Support command payload validation:

package auth

type registerPayload struct {
    Name string
    Email string
}

func (payload registerPayload) Validate() error {
    if payload.Name == "" {
        return errors.New("empty name")
    }
    if !isValidEmail(payload.Email) {
        return fmt.Errorf("invalid email address: %s", payload.Email)
    }
    return nil
}

func RegisterUser(id uuid.UUID, name string, email string) command.Cmd[registerPayload] {
    return command.New("auth.user.register", registerPayload{name, email}, command.Aggregate("auth.user", id))
}

func HandleCommands(ctx context.Context, bus command.Bus) <-chan error {
    return command.MustHandle(ctx, bus, "auth.user.register", func(ctx command.Context) error {
        // ... this will only execute if ctx.Payload() valid ...
        return nil
    })
}