badges/shields · error · ValidationError

Field `links` must not have more than 2 elements

Error message

Field `links` must not have more than 2 elements

What it means

The `links` array may contain at most 2 URLs — the left and right halves of the badge each get at most one link. _validate checks format.links.length > 2 (badge-maker/lib/index.js:29) and throws when more are supplied. This is a hard library limit on clickable regions, not an arbitrary array cap.

Source

Thrown at badge-maker/lib/index.js:30

  }

  if (!('message' in format)) {
    throw new ValidationError('Field `message` is required')
  }

  const stringFields = ['labelColor', 'color', 'message', 'label', 'logoBase64']
  stringFields.forEach(function (field) {
    if (field in format && typeof format[field] !== 'string') {
      throw new ValidationError(`Field \`${field}\` must be of type string`)
    }
  })

  if ('links' in format) {
    if (!Array.isArray(format.links)) {
      throw new ValidationError('Field `links` must be an array of strings')
    } else {
      if (format.links.length > 2) {
        throw new ValidationError(
          'Field `links` must not have more than 2 elements',
        )
      }
      format.links.forEach(function (field) {
        if (typeof field !== 'string') {
          throw new ValidationError('Field `links` must be an array of strings')
        }
      })
    }
  }

  const styleValues = [
    'plastic',
    'flat',
    'flat-square',
    'for-the-badge',
    'social',
  ]

View on GitHub (pinned to 766fd8bc89)

Solutions

  1. Truncate the array to two entries before calling: links: myLinks.slice(0, 2)
  2. Split content across multiple badges if you need more than two linked areas
  3. Keep only the two most important links and render the rest as separate badges

Example fix

// before
const svg = makeBadge({ message: 'project', links: [home, docs, repo] })
// after
const svg = makeBadge({ message: 'project', links: [home, docs].slice(0, 2) })
Defensive patterns

Strategy: validation

Validate before calling

if (badge.links && badge.links.length > 2) {
  badge.links = badge.links.slice(0, 2)
}
const svg = makeBadge(badge)

Try / catch

try {
  return makeBadge(badge)
} catch (e) {
  if (e instanceof ValidationError && e.message.includes('more than 2 elements')) {
    return makeBadge({ ...badge, links: badge.links.slice(0, 2) })
  }
  throw e
}

Prevention

When it happens

Trigger: makeBadge({ message: 'x', links: ['https://a.com', 'https://b.com', 'https://c.com'] }) — three or more URLs in the links array.

Common situations: Trying to attach one link per badge segment or forwarding all project URLs (homepage, docs, repo, issues) into links; bulk-generating badges from a config where a links list grew over time.

Related errors


AI-assisted analysis of badges/shields@766fd8bc89 (2026-08-30). Data as JSON: /api/errors/9a227e902ddc5aac. Report an issue: GitHub.