Skip to content

[REQUEST] Prompt validated via Regex #2938

@antoniovazquezblanco

Description

@antoniovazquezblanco

Consider posting in https://github.com/textualize/rich/discussions for feedback before raising a feature request.

Have you checked the issues for a similar suggestions? Yes. Have not found the request among open issues or PRs.

How would you improve Rich?

I would love to have a Prompt class that can validate string input based on a regex pattern :)

What problem does it solve for you?

It may be the fastest way to implement an string validated prompt.

** Code example **

Probably not the best example implementation but... :)

class RegexPrompt(PromptBase[str]):
    response_type = str

    def __init__(self, regex, validate_error_message, *kwargs):
        super().__init__(kwargs)
        self._regex = regex
        self.validate_error_message = "[prompt.invalid]"+validate_error_message

    def process_response(self, value: str) -> str:
        if not self._regex.fullmatch(value):
            raise InvalidResponse(self.validate_error_message)
        return value

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions