d2lang/d2 · error

expected "stroke-width" to be a number between 0 and 15

Error message

expected "stroke-width" to be a number between 0 and 15

What it means

The 'stroke-width' style key must parse as an integer between 0 and 15 inclusive. This error is returned when strconv.Atoi fails or the value is out of that range.

Source

Thrown at d2graph/d2graph.go:356

		if !color.ValidColor(value) {
			return errors.New(`expected "fill" to be a valid named color ("orange"), a hex code ("#f0ff3a"), or a gradient ("linear-gradient(red, blue)")`)
		}
		s.Fill.Value = value
	case "fill-pattern":
		if s.FillPattern == nil {
			break
		}
		if !go2.Contains(d2ast.FillPatterns, strings.ToLower(value)) {
			return fmt.Errorf(`expected "fill-pattern" to be one of: %s`, strings.Join(d2ast.FillPatterns, ", "))
		}
		s.FillPattern.Value = value
	case "stroke-width":
		if s.StrokeWidth == nil {
			break
		}
		f, err := strconv.Atoi(value)
		if err != nil || (f < 0 || f > 15) {
			return errors.New(`expected "stroke-width" to be a number between 0 and 15`)
		}
		s.StrokeWidth.Value = value
	case "stroke-dash":
		if s.StrokeDash == nil {
			break
		}
		f, err := strconv.Atoi(value)
		if err != nil || (f < 0 || f > 10) {
			return errors.New(`expected "stroke-dash" to be a number between 0 and 10`)
		}
		s.StrokeDash.Value = value
	case "border-radius":
		if s.BorderRadius == nil {
			break
		}
		f, err := strconv.Atoi(value)
		if err != nil || (f < 0) {
			return errors.New(`expected "border-radius" to be a number greater or equal to 0`)

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Set stroke-width to an integer from 0 to 15.
  2. Remove units like "px" and round decimals to integers.
  3. Clamp large values to 15 before applying.

Example fix

// before
x.style.stroke-width: 2.5
// after
x.style.stroke-width: 2
Defensive patterns

Strategy: validation

Validate before calling

n, err := strconv.Atoi(v)
if err != nil || n < 0 || n > 15 {
    return fmt.Errorf("stroke-width %q must be an integer 0-15", v)
}

Type guard

func validStrokeWidth(s string) bool { n, err := strconv.Atoi(s); return err == nil && n >= 0 && n <= 15 }

Try / catch

if err := applyStyle("stroke-width", value); err != nil {
    if strings.Contains(err.Error(), "stroke-width") {
        value = "1" // safe default
    }
}

Prevention

When it happens

Trigger: Setting style 'stroke-width' to a non-integer ("2.5", "thick") or an integer outside 0–15 (e.g. 20, -1).

Common situations: D2 authors assuming pixel-scale widths larger than 15 are allowed, or using decimals/units like "2px".

Related errors


AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31). Data as JSON: /api/errors/53993d06be0a0977. Report an issue: GitHub.