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

Input Validation #535

Open
znd4 opened this issue Apr 10, 2024 · 1 comment
Open

Input Validation #535

znd4 opened this issue Apr 10, 2024 · 1 comment
Labels
cmd/input enhancement New feature or request

Comments

@znd4
Copy link

znd4 commented Apr 10, 2024

Is your feature request related to a problem? Please describe.
If step 5 in my script requires a particular type of free-text input, (e.g. URL, S3 bucket, semver) if the user enters an invalid input, they'll have to start the form over (unless I implement a custom REPL for that input, or save WIP forms to disk)

Describe the solution you'd like

I'd like to be able to provide a regular expression to gum input, e.g.

# semver pattern, see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
PATTERN='^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
gum input --regex $PATTERN

If the user enters an invalid input, there should be some kind of (accessible) feedback -- like flashing a warning symbol. Additionally, this might require a customizable warning message, e.g.:

gum input ... --regex-message "Invalid input. Please follow semver (e.g.: 1.2.3)"

Describe alternatives you've considered

Save intermediate inputs to disk and prompt users at start

# my_script.py
import subprocess as sp
import datetime as dt

form_body = {}
if previous_form_on_disk() and user_wants_to_use_saved_form():
    form_body() = select_form()

current_save_path = local_data_path() / f"{dt.datetime.now()}"
    
form_body["field1"] = form_body.get("field1", prompt_1())
save(current_save_path, form_body)
# ...

implement REPL for the tricky gum inputs

def prompt_n():
    header = ""
    while True:
        result = sp.check_output(["gum", "input", ...])
        if re.match(result, r"..."):
            return result
        header="invalid input. Please follow ..."

Additional context

This would definitely require some design work, and I'm not sure if there's a generic enough solution to warrant including this as a feature (instead of coming up with a nifty documentation snippet)

@znd4
Copy link
Author

znd4 commented Apr 10, 2024

This might also require upstream work in huh, depending on the design

@aymanbagabas aymanbagabas added the enhancement New feature or request label Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmd/input enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants