-
-
Couldn't load subscription status.
- Fork 1.3k
feat(input): add pattern-based validation and error handling for input prompt #1862
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
base: main
Are you sure you want to change the base?
feat(input): add pattern-based validation and error handling for input prompt #1862
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1862 +/- ##
=======================================
Coverage ? 95.63%
=======================================
Files ? 45
Lines ? 2818
Branches ? 749
=======================================
Hits ? 2695
Misses ? 114
Partials ? 9 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I removed the demo for this feature because the e2e build test fails whenever the config object is modified and used inside the demo package. Since the functionality is already covered through detailed test cases, I have removed the demo to keep the CI build stable. Let me know if any changes are required. |
README.md
Outdated
| @@ -1 +1 @@ | |||
| packages/prompts/README.md | |||
| packages/prompts/README.md | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What changed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was automatically added by the linter when I ran yarn eslint --write . it only inserted a blank line, no actual content change.
| if (pattern && !pattern.test(answer)) { | ||
| setError(patternError || 'Invalid input update'); | ||
| setStatus('idle'); | ||
| return; | ||
| } | ||
| const isValid = | ||
| required && !answer ? 'You must provide a value' : await validate(answer); | ||
| if (isValid === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a question of which validation should occur first when both are present... This open 2 questions:
- Should we allow both to be set or not?
- If we do allow both, which should run first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, I chose to run the pattern check first when both pattern and validate are present. The idea is that pattern acts as a lightweight, immediate constraint on the input format, while validate can still handle deeper validations afterward.
Since a pattern error would already be shown live during typing, re-overriding that message on Enter didn’t feel right, it’s clearer UX to surface the existing pattern feedback first and only run validate once the pattern passes.
packages/input/src/index.ts
Outdated
| if (pattern && !pattern.test(answer)) { | ||
| setError(patternError || 'Invalid input update'); | ||
| setStatus('idle'); | ||
| return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not use an early return and loop the validation within the rest of the logical flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the early return and integrated the pattern validation within the existing logical flow to keep it consistent with the rest of the validation logic.
packages/input/src/index.ts
Outdated
| setValue(newValue); | ||
| if (pattern && !pattern.test(newValue)) { | ||
| setError(patternError || 'Invalid input'); | ||
| } else { | ||
| setError(undefined); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this here, wouldn't that case hit the default else case down there, which does the same logic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I removed the redundant pattern check since the default else block already performs the same validation for keypress updates.
dd200cb to
cad8455
Compare
cad8455 to
68e76d1
Compare
patternandpatternErroroptions in InputConfig