Fix Blade form character counts to ignore whitespace#482
Conversation
📝 WalkthroughWalkthroughThe PR changes character validation for form fields to count only non-whitespace characters. A regex-based helper is added to ChangesNon-whitespace character validation for form fields
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 6 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (6 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/utils/src/forms.ts (1)
42-51: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win
errorMessagewon’t help here.json-schema-to-zodignores that keyword, so the regex failure still reacheserr.message. If you want the old “at most N characters” copy, map the regex issue to a friendly message inpackages/api/src/routers/forms.tsinstead of relying on JSON Schema metadata.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/utils/src/forms.ts` around lines 42 - 51, The PARAGRAPH schema in forms.ts is still relying on JSON Schema metadata for validation text, but json-schema-to-zod ignores errorMessage so users will only see the raw regex error. Update the validation handling in forms.ts and the form router logic in forms.ts under packages/api/src/routers/forms.ts to intercept the regex failure from createNonWhitespaceLengthPattern and map it to the friendly “at most N characters” message there. Use the PARAGRAPH branch and the form validation/error mapping code as the key places to adjust, rather than depending on schema.errorMessage.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/utils/src/forms.ts`:
- Around line 16-23: The createNonWhitespaceLengthPattern helper can build an
invalid quantifier when the defaulted upper bound is smaller than the lower
bound, so clamp the computed max before generating the regex. Update
createNonWhitespaceLengthPattern in forms.ts to ensure the upperBound is always
at least the lowerBound (including when min is provided and max is undefined),
then construct the pattern from those adjusted bounds so schema compilation
cannot fail.
---
Nitpick comments:
In `@packages/utils/src/forms.ts`:
- Around line 42-51: The PARAGRAPH schema in forms.ts is still relying on JSON
Schema metadata for validation text, but json-schema-to-zod ignores errorMessage
so users will only see the raw regex error. Update the validation handling in
forms.ts and the form router logic in forms.ts under
packages/api/src/routers/forms.ts to intercept the regex failure from
createNonWhitespaceLengthPattern and map it to the friendly “at most N
characters” message there. Use the PARAGRAPH branch and the form
validation/error mapping code as the key places to adjust, rather than depending
on schema.errorMessage.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: f2e69404-84bb-49b3-be54-cd8b7a6d21f4
📒 Files selected for processing (3)
apps/blade/src/app/_components/forms/question-response-card.tsxpackages/api/src/routers/forms.tspackages/utils/src/forms.ts
| function createNonWhitespaceLengthPattern(min?: number, max?: number) { | ||
| if (min === undefined && max === undefined) return undefined; | ||
|
|
||
| const lowerBound = min ?? 0; | ||
| const upperBound = max ?? ""; | ||
|
|
||
| return `^(?:\\s*\\S){${lowerBound},${upperBound}}\\s*$`; | ||
| } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Confirm whether form config permits min > default max (150/750) for SHORT_ANSWER/PARAGRAPH.
fd -t f 'forms.ts' packages/utils/src --exec ast-grep outline {}
rg -nP -C3 '\bmin\b|\bmax\b' packages/utils/src/forms.ts
# Look for validation that constrains min <= max on form questions
rg -nP -C3 'QuestionValidator|FormSchemaValidator|\.min\(|\.max\(' packages/utils/srcRepository: KnightHacks/forge
Length of output: 3181
Clamp the default max before constructing the length regex.
min can exceed the defaulted max (150/750), which produces an invalid {n,m} quantifier and throws when the schema is compiled. Keep the upper bound at least as large as the lower bound:
🐛 Proposed fix
const lowerBound = min ?? 0;
- const upperBound = max ?? "";
+ const upperBound = max === undefined ? "" : Math.max(max, lowerBound);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| function createNonWhitespaceLengthPattern(min?: number, max?: number) { | |
| if (min === undefined && max === undefined) return undefined; | |
| const lowerBound = min ?? 0; | |
| const upperBound = max ?? ""; | |
| return `^(?:\\s*\\S){${lowerBound},${upperBound}}\\s*$`; | |
| } | |
| function createNonWhitespaceLengthPattern(min?: number, max?: number) { | |
| if (min === undefined && max === undefined) return undefined; | |
| const lowerBound = min ?? 0; | |
| const upperBound = max === undefined ? "" : Math.max(max, lowerBound); | |
| return `^(?:\\s*\\S){${lowerBound},${upperBound}}\\s*$`; | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/utils/src/forms.ts` around lines 16 - 23, The
createNonWhitespaceLengthPattern helper can build an invalid quantifier when the
defaulted upper bound is smaller than the lower bound, so clamp the computed max
before generating the regex. Update createNonWhitespaceLengthPattern in forms.ts
to ensure the upperBound is always at least the lowerBound (including when min
is provided and max is undefined), then construct the pattern from those
adjusted bounds so schema compilation cannot fail.
There was a problem hiding this comment.
this function isn't needed tbh can we just inline it
There was a problem hiding this comment.
@alexanderpaolini Yep — inlining it here is cleaner since it’s single-use, and it keeps the bound-clamping logic right next to the SHORT_ANSWER / PARAGRAPH handling.
The key thing to preserve is the clamp so we never build an invalid {min,max} quantifier when min is larger than the default max.
case "SHORT_ANSWER":
case "PARAGRAPH": {
schema.type = "string";
const defaultMax = type === "SHORT_ANSWER" ? 150 : 750;
const lowerBound = min ?? 0;
const upperBound = Math.max(max ?? defaultMax, lowerBound);
schema.pattern = `^(?:\\s*\\S){${lowerBound},${upperBound}}\\s*$`;
break;
}Why this is better:
- removes a one-off helper
- keeps the regex behavior local to the only branch that needs it
- still guarantees schema compilation won’t blow up on
{n,m}withn > m
Summary
Verification
pnpm --filter=@forge/utils typecheckpnpm --filter=@forge/api typecheckpnpm --filter=@forge/blade typecheckpnpm --filter=@forge/utils lintpnpm --filter=@forge/api lintpnpm --filter=@forge/blade lintpnpm --filter=@forge/utils formatpnpm --filter=@forge/api formatpnpm prettier --check apps/blade/src/app/_components/forms/question-response-card.tsx packages/api/src/routers/forms.ts packages/utils/src/forms.tsa b cas 3 characters and rejectsa b c dfor a 3-character non-whitespace limit.Note:
pnpm --filter=@forge/blade formatcurrently fails on pre-existing formatting insrc/app/hackathon/README.md, outside this PR's touched files.Summary by CodeRabbit
New Features
Bug Fixes