Skip to content
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

[feature] Command Payload Validation #228

Open
bounoable opened this issue Aug 29, 2024 · 0 comments
Open

[feature] Command Payload Validation #228

bounoable opened this issue Aug 29, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@bounoable
Copy link
Contributor

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
	})
}
@bounoable bounoable added the enhancement New feature or request label Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant