{"record":{"id":"8f2118d0b4c4859c","repo":"badges/shields","slug":"field-field-must-be-of-type-string","errorCode":null,"errorMessage":"Field `${field}` must be of type string","messagePattern":"Field `(.+?)` must be of type string","errorType":"validation","errorClass":"ValidationError","httpStatus":null,"severity":"error","filePath":"badge-maker/lib/index.js","lineNumber":21,"sourceCode":" */\n\nimport _makeBadge from './make-badge.js'\n\nexport class ValidationError extends Error {}\n\nfunction _validate(format) {\n  if (format !== Object(format)) {\n    throw new ValidationError('makeBadge takes an argument of type object')\n  }\n\n  if (!('message' in format)) {\n    throw new ValidationError('Field `message` is required')\n  }\n\n  const stringFields = ['labelColor', 'color', 'message', 'label', 'logoBase64']\n  stringFields.forEach(function (field) {\n    if (field in format && typeof format[field] !== 'string') {\n      throw new ValidationError(`Field \\`${field}\\` must be of type string`)\n    }\n  })\n\n  if ('links' in format) {\n    if (!Array.isArray(format.links)) {\n      throw new ValidationError('Field `links` must be an array of strings')\n    } else {\n      if (format.links.length > 2) {\n        throw new ValidationError(\n          'Field `links` must not have more than 2 elements',\n        )\n      }\n      format.links.forEach(function (field) {\n        if (typeof field !== 'string') {\n          throw new ValidationError('Field `links` must be an array of strings')\n        }\n      })\n    }","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/badges/shields/blob/766fd8bc89a90b8534dc573ab72dec30215ab1ec/badge-maker/lib/index.js#L3-L39","documentation":"The fields labelColor, color, message, label, and logoBase64 must be strings when present. _validate iterates these fields (badge-maker/lib/index.js:18-23) and throws if a provided value has any other typeof. This prevents malformed SVG output from numbers, booleans, or objects reaching the renderer.","triggerScenarios":"makeBadge({ message: 42 }), makeBadge({ label: { text: 'build' } }), makeBadge({ color: true }), or makeBadge({ message: ['passing'] }) — any non-string value assigned to one of the five string fields.","commonSituations":"Numbers from CI (build number 123 as message), boolean success flags passed directly as message, colors read from JSON where they were numeric, or template variables that hold non-string types from an API response.","solutions":["Coerce the value to a string: makeBadge({ message: String(buildNumber) })","If the value is a boolean/status, map it to a string first: message: passed ? 'passing' : 'failing'","Validate the descriptor shape before calling makeBadge"],"exampleFix":"// before\nconst svg = makeBadge({ label: 'build', message: buildNumber }) // 123\n// after\nconst svg = makeBadge({ label: 'build', message: String(buildNumber) })","handlingStrategy":"type-guard","validationCode":"for (const f of ['labelColor', 'color', 'message', 'label', 'logoBase64']) {\n  if (f in badge && typeof badge[f] !== 'string') {\n    throw new Error(`badge field ${f} must be a string`)\n  }\n}\nconst svg = makeBadge(badge)","typeGuard":"function isBadgeStringField(v) {\n  return typeof v === 'string'\n}\n// usage: if (isBadgeStringField(badge.message)) makeBadge(badge)","tryCatchPattern":"try {\n  return makeBadge(badge)\n} catch (e) {\n  if (e instanceof ValidationError && e.message.includes('must be of type string')) {\n    const field = e.message.match(/Field `(.+)`/)[1]\n    return makeBadge({ ...badge, [field]: String(badge[field]) })\n  }\n  throw e\n}","preventionTips":["Coerce numeric CI values (build numbers, percentages) with String() before use","Map booleans to strings (passing/failing) instead of passing them raw","Validate config-sourced badge data at load time, not at render time"],"tags":["validation","type-mismatch","schema-validation"],"backgroundTag":"invalid-field-type","analyzedSha":"766fd8bc89a90b8534dc573ab72dec30215ab1ec","analyzedAt":"2026-08-30T01:40:27.499Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}