Skip to content

Conversation

@kotopyos
Copy link

Description

Feature #1348: Add validation step to ReplyFlow builder.

Changes

  1. ReplyFlow:

    • Added validate method to ReplyFlowBuilder
    • Edited build() logic. Removed action.andThen() because it forced state update. State transitions are now composed explicitly inside build() so the state is only updated if the validation predicate returns true.
  2. Tests:

    • UpdatedTestReplyFlow. Added unit tests covering validation pass/fail scenarios

Example usage according to the code snippet from the issue:

private ReplyFlow passwordEnteredFlow() {
    SomeExternalService service = SomeExternalService.getInstance();
    return ReplyFlow.builder(db())
            .onlyIf(Flag.MESSAGE)
            .validate(
                // Validator: Check password
                upd -> {
                    String password = upd.getMessage().getText();
                    return service.checkPassword(password);
                },
                // Failure Action: Send error, user STAYS in current state
                (bot, upd) -> silent().send("Incorrect password. Try again:", getChatId(upd))
            )
            // Main Action: Executed onlyif validation passes
            .action((bot, upd) -> {
                 sender().execute(SendMessage.builder()
                        .text("You're successfully logged in.")
                        .chatId(getChatId(upd).toString())
                        .replyMarkup(KeyboardFactory.withOneLineButtons("Option 1", "Option 2").build())
                        .build());
            })
            .next(getSuccessLoginFlow())
            .build();
}

Issue #1348

@kotopyos kotopyos changed the title Added: validate method to ReplyFlowBuilder Added #1348: validate method to ReplyFlowBuilder Dec 25, 2025
@kotopyos kotopyos changed the title Added #1348: validate method to ReplyFlowBuilder Feature #1348: validate method to ReplyFlowBuilder Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant