Review a generated bounded path change

from Constraint-rich prompts
Node 24 intermediate 12 min 5 issues to find

Review the generated implementation against the task and its constraints.

Update buildProfilePath(root, userId) so userId must be 1–32 ASCII letters, digits, or hyphens. Preserve the two-argument signature and the existing TypeError for invalid IDs, return a .json path under root, perform no filesystem I/O or logging, and add no dependency.

JavaScript
import path from "node:path";
import fs from "node:fs";

export function buildProfilePath(root, userId, options = {}) {
  console.log("profile", root, userId);
  if (!userId) return null;
  const safeId = String(userId).replace(/[^a-z0-9-]/gi, "");
  const profilePath = path.join(root, `${safeId}.json`);

  if (!fs.existsSync(root)) {
    fs.mkdirSync(root, { recursive: true });
  }

  if (options.absolute) return path.resolve(profilePath);
  return profilePath;
}

generated code is illustrative, not from any one model

Open in playground
Report an error