Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { api } from "~/trpc/react";

type FormQuestion = z.infer<typeof FORMS.QuestionValidator>;

const countNonWhitespaceCharacters = (value: string) =>
value.replace(/\s/g, "").length;

interface QuestionResponseCardProps {
question: FormQuestion;
value?: string | string[] | number | Date | boolean | null;
Expand Down Expand Up @@ -111,7 +114,7 @@ function QuestionBody({
case "SHORT_ANSWER": {
const currentValue = (value as string) || "";
const maxLength = 150;
const charCount = currentValue.length;
const charCount = countNonWhitespaceCharacters(currentValue);
const isOverLimit = charCount > maxLength;

return (
Expand All @@ -138,7 +141,7 @@ function QuestionBody({
case "PARAGRAPH": {
const currentValue = (value as string) || "";
const maxLength = 750;
const charCount = currentValue.length;
const charCount = countNonWhitespaceCharacters(currentValue);
const isOverLimit = charCount > maxLength;

return (
Expand Down
12 changes: 10 additions & 2 deletions packages/api/src/routers/forms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TRPCRouterRecord } from "@trpc/server";
import type { JSONSchema7 } from "json-schema";
import { TRPCError } from "@trpc/server";
import { and, count, desc, eq, inArray, lt, sql } from "drizzle-orm";
import jsonSchemaToZod from "json-schema-to-zod";
Expand Down Expand Up @@ -154,14 +153,23 @@ export const formsRouter = {
minioClient,
);

const jsonSchema = forms.generateJsonSchema(formData);

if (!jsonSchema.success) {
throw new TRPCError({
message: jsonSchema.msg,
code: "BAD_REQUEST",
});
}

return {
...retForm,
responseRoleIds: responseRoles.map((r) => r.roleId),
formData: {
...formData,
instructions: instructionsWithFreshUrls,
},
zodValidator: jsonSchemaToZod(form.formValidatorJson as JSONSchema7),
zodValidator: jsonSchemaToZod(jsonSchema.schema),
};
}),

Expand Down
26 changes: 20 additions & 6 deletions packages/utils/src/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ type OptionalSchema =
| { success: true; schema: JSONSchema7 }
| { success: false; msg: string };

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

@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


function createJsonSchemaValidator({
optional,
type,
Expand All @@ -30,12 +39,16 @@ function createJsonSchemaValidator({

switch (type) {
case "SHORT_ANSWER":
case "PARAGRAPH":
case "PARAGRAPH": {
schema.type = "string";
if (max === undefined) {
schema.maxLength = type === "SHORT_ANSWER" ? 150 : 750;
}
const maxNonWhitespaceLength =
max ?? (type === "SHORT_ANSWER" ? 150 : 750);
schema.pattern = createNonWhitespaceLengthPattern(
min,
maxNonWhitespaceLength,
);
break;
}
case "EMAIL":
schema.type = "string";
schema.format = "email";
Expand Down Expand Up @@ -93,15 +106,16 @@ function createJsonSchemaValidator({
}

if (min !== undefined) {
if (schema.type === "string") schema.minLength = min;
if (schema.type === "string" && schema.pattern === undefined)
schema.minLength = min;
if (schema.type === "array") schema.minItems = min;
if (schema.type === "number") schema.minimum = min;
} else {
if (schema.type === "array" && !optional) schema.minItems = 1;
}

if (max !== undefined) {
if (schema.type === "string") {
if (schema.type === "string" && schema.pattern === undefined) {
// Explicit max value overrides any defaults
schema.maxLength = max;
}
Expand Down
Loading