Skip to content

Fix Blade form character counts to ignore whitespace#482

Open
DVidal1205 wants to merge 1 commit into
mainfrom
lenny/forms-ignore-whitespace-char-count
Open

Fix Blade form character counts to ignore whitespace#482
DVidal1205 wants to merge 1 commit into
mainfrom
lenny/forms-ignore-whitespace-char-count

Conversation

@DVidal1205

@DVidal1205 DVidal1205 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Update Blade form short-answer and paragraph counters to ignore whitespace characters.
  • Generate text-response validation with non-whitespace length patterns so whitespace does not count against limits on submit/edit.
  • Regenerate the client zod validator from current form data when loading forms so existing forms use the updated whitespace-aware validation.

Verification

  • pnpm --filter=@forge/utils typecheck
  • pnpm --filter=@forge/api typecheck
  • pnpm --filter=@forge/blade typecheck
  • pnpm --filter=@forge/utils lint
  • pnpm --filter=@forge/api lint
  • pnpm --filter=@forge/blade lint
  • pnpm --filter=@forge/utils format
  • pnpm --filter=@forge/api format
  • pnpm prettier --check apps/blade/src/app/_components/forms/question-response-card.tsx packages/api/src/routers/forms.ts packages/utils/src/forms.ts
  • Node smoke test confirmed the generated regex accepts a b c as 3 characters and rejects a b c d for a 3-character non-whitespace limit.

Note: pnpm --filter=@forge/blade format currently fails on pre-existing formatting in src/app/hackathon/README.md, outside this PR's touched files.

Summary by CodeRabbit

  • New Features

    • Character counters for short answer and paragraph questions now ignore whitespace, matching how limits are enforced.
  • Bug Fixes

    • Improved validation for paragraph responses so length limits are checked more consistently.
    • Form loading now uses the latest generated validation rules, which helps avoid stale validation behavior and unexpected form errors.

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR changes character validation for form fields to count only non-whitespace characters. A regex-based helper is added to packages/utils/src/forms.ts and applied to the PARAGRAPH JSON schema; minLength/maxLength are skipped when a pattern is already set. The getForm API endpoint now regenerates the JSON schema at runtime instead of reusing the stored value. The UI character counter is updated to match.

Changes

Non-whitespace character validation for form fields

Layer / File(s) Summary
Non-whitespace regex pattern and schema generation
packages/utils/src/forms.ts
Adds createNonWhitespaceLengthPattern(min?, max?) that builds a regex enforcing non-whitespace character bounds. Updates the PARAGRAPH branch in createJsonSchemaValidator to assign schema.pattern from this helper. Gates minLength/maxLength assignment on schema.pattern being absent.
getForm regenerates JSON schema at runtime
packages/api/src/routers/forms.ts
getForm now calls createJsonSchemaValidator on the freshly rebuilt formData (after re-signing media URLs) and throws BAD_REQUEST on failure; previously it cast form.formValidatorJson directly. Removes the unused JSONSchema7 import.
UI character counter uses non-whitespace count
apps/blade/src/app/_components/forms/question-response-card.tsx
Adds countNonWhitespaceCharacters helper. Applies it to charCount in the SHORT_ANSWER and PARAGRAPH switch branches, updating both the over-limit check and the displayed {charCount}/{maxLength} counter.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

Blade, API, Bug, Major

Suggested reviewers

  • Adr1an04
  • DGoel1602
🚥 Pre-merge checks | ✅ 6 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The title matches the PR topic, but it does not start with the required issue number in brackets. Prefix the title with an issue key like "[#123]" and keep the description concise, e.g. "[#123] Fix Blade form character counts to ignore whitespace".
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
No Hardcoded Secrets ✅ Passed Scanned the three touched files; no API-key/password/token/secret string literals were present, and the added literals are non-sensitive constants only.
Validated Env Access ✅ Passed No raw process.env usage appears in the PR-touched files or their patch; the changes stay within validated env patterns.
No Typescript Escape Hatches ✅ Passed No any, @ts-ignore, @ts-expect-error, or non-null assertions appear in the touched files; changes use explicit types and guards.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch lenny/forms-ignore-whitespace-char-count

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/utils/src/forms.ts (1)

42-51: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

errorMessage won’t help here. json-schema-to-zod ignores that keyword, so the regex failure still reaches err.message. If you want the old “at most N characters” copy, map the regex issue to a friendly message in packages/api/src/routers/forms.ts instead 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

📥 Commits

Reviewing files that changed from the base of the PR and between d9803f4 and 580165b.

📒 Files selected for processing (3)
  • apps/blade/src/app/_components/forms/question-response-card.tsx
  • packages/api/src/routers/forms.ts
  • packages/utils/src/forms.ts

Comment on lines +16 to +23
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*$`;
}

@coderabbitai coderabbitai Bot Jun 28, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 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/src

Repository: 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.

Suggested change
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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function isn't needed tbh can we just inline it

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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} with n > m

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.

2 participants