{"record":{"id":"ba643ddb0aa689ec","repo":"santifer/career-ops","slug":"getemployerprofile-id-is-required-ba643d","errorCode":null,"errorMessage":"getEmployerProfile: id is required","messagePattern":"getEmployerProfile: id is required","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"plugins/h1b-sponsor/lib/index.mjs","lineNumber":377,"sourceCode":"  const q = String(name || '').trim();\n  if (q.length < 2) return { total: 0, results: [] };\n\n  const target = normalize(q);\n  const page = boundedTop(SEARCH_PAGE);\n  let total = 0;\n  for await (const rec of readRecords(indexFile(opts))) {\n    if (!normalize(rec.n).includes(target)) continue;\n    total++;\n    page.push(rec);\n  }\n  // Same order the endpoint pages in, so the one page a broad query shows holds\n  // the entities that actually file rather than the ones that sort first.\n  return { total, results: page.drain().map(r => ({ id: String(r.k), name: String(r.n) })) };\n}\n\nexport async function getEmployerProfile(id, opts = {}) {\n  const sanitized = String(id || '').trim();\n  if (!sanitized) throw new Error('getEmployerProfile: id is required');\n\n  const file = indexFile(opts);\n  const memo = seen.get(memoKey(file, sanitized));\n  if (memo) return recordToProfile(memo);\n\n  for await (const rec of readRecords(file)) {\n    if (String(rec.k) !== sanitized) continue;\n    seen.set(memoKey(file, sanitized), rec);\n    return recordToProfile(rec);\n  }\n  // The HTTP path 404s here and reports the same thing: an id that resolves to\n  // no employer is unknown, not an employer with zero filings.\n  return null;\n}\n","sourceCodeStart":359,"sourceCodeEnd":392,"githubUrl":"https://github.com/santifer/career-ops/blob/aac998c7ed7248ea853b720ceeb1fdbeb322fc5d/plugins/h1b-sponsor/lib/index.mjs#L359-L392","documentation":"getEmployerProfile(id) looks up a single employer in the H1B index by its record id. Before doing any I/O it coerces the argument to a string and trims it; if nothing remains it throws, because an empty id can never match a record and proceeding would scan the whole index for nothing. The guard also protects against undefined/null being passed through from upstream search results that were destructured incorrectly.","triggerScenarios":"Calling getEmployerProfile('') , getEmployerProfile(null), getEmployerProfile(undefined), or getEmployerProfile('   ') — typically when an id field is missing from an API payload, a search result row ({id, name}) lost its id, or a route/query parameter was absent (e.g. req.query.id is undefined).","commonSituations":"A web handler forwards an empty URL segment (/employer/ → id ''); a frontend renders a detail page before the search result object loads and passes undefined; a data migration or CSV import produced rows with blank employer ids; destructuring a result array in the wrong order so id picks up the name slot's undefined.","solutions":["Ensure the caller supplies a real employer id obtained from a prior search() call's results (the id field of {id, name} entries).","Validate the id at the entry point of your handler/route and return a 400 before invoking the library.","If the id comes from user input, trim it and check length > 0; treat whitespace-only as missing.","If upstream data legitimately has blank ids, filter those records out before mapping them to getEmployerProfile calls."],"exampleFix":"// before\nconst profile = await getEmployerProfile(req.params.id);\n\n// after\nconst id = (req.params.id || '').trim();\nif (!id) return res.status(400).json({ error: 'employer id is required' });\nconst profile = await getEmployerProfile(id);","handlingStrategy":"validation","validationCode":"function requireEmployerId(id) {\n  const s = String(id ?? '').trim();\n  if (!s) throw new TypeError('employer id is required and must be non-empty');\n  return s;\n}\n// call site:\nconst profile = await getEmployerProfile(requireEmployerId(inputId));","typeGuard":"function isEmployerId(v) {\n  return (typeof v === 'string' || typeof v === 'number') && String(v).trim().length > 0;\n}","tryCatchPattern":"try {\n  profile = await getEmployerProfile(id);\n} catch (e) {\n  if (/id is required/.test(e.message)) {\n    return respond(400, 'Missing employer id');\n  }\n  throw e;\n}","preventionTips":["Always obtain ids from search() result objects rather than constructing them by hand.","Validate route params/query params at the boundary and 400 early on missing values.","Destructure result objects by property name, never by array position.","Filter out records with blank ids before batch-mapping to getEmployerProfile."],"tags":["validation","arguments","api-misuse"],"backgroundTag":"empty-required-field","analyzedSha":"aac998c7ed7248ea853b720ceeb1fdbeb322fc5d","analyzedAt":"2026-09-16T06:35:29.214Z","contentChangedAt":"2026-09-16T06:35:29.214Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}