inancgumus/learngo · error

record.visits cannot be negative

Error message

record.visits cannot be negative

What it means

Sentinel validation error from the pipe package's internal validate() guard: a decoded record has a negative visits value. UnmarshalText/UnmarshalJSON will happily decode a negative number for the visits field, so this check rejects input like domain/page/-5/x that is meaningless as a visit counter.

Source

Thrown at logparser/v5/pipe/record.go:100

// parseStr helps UnmarshalText for string to positive int parsing.
func parseStr(name, v string) (int, error) {
	n, err := strconv.Atoi(v)
	if err != nil {
		return 0, fmt.Errorf("Record.UnmarshalText %q: %v", name, err)
	}
	return n, nil
}

// validate whether a parsed record is valid or not.
func validate(r record) (err error) {
	switch {
	case r.domain == "":
		err = errors.New("record.domain cannot be empty")
	case r.page == "":
		err = errors.New("record.page cannot be empty")
	case r.visits < 0:
		err = errors.New("record.visits cannot be negative")
	case r.uniques < 0:
		err = errors.New("record.uniques cannot be negative")
	}
	return
}

View on GitHub (pinned to 3c475a78e5)

Solutions

  1. Clamp or correct negative values at the data source before unmarshaling.
  2. Check for ETL/diff logic that subtracts counts and can go below zero.
  3. Skip and report records with negative visits during import.
  4. If negatives are legitimate in your domain, relax the validation check.

Example fix

// before
{"domain":"example.com","page":"/","visits":-3}
// after
{"domain":"example.com","page":"/","visits":0}
Defensive patterns

Strategy: validation

Validate before calling

if rec.Visits < 0 {
    rec.Visits = 0 // clamp, or reject the record
}

Type guard

func hasValidVisits(r record) bool { return r.visits >= 0 }

Try / catch

var rec record
err := rec.UnmarshalJSON(data)
if err != nil {
    if strings.Contains(err.Error(), "record.visits cannot be negative") {
        log.Printf("dropping record with negative visits: %s", data)
        return nil
    }
    return err
}

Prevention

When it happens

Trigger: Unmarshaling a record where visits < 0, e.g. "visits": -1 in JSON or a text field like "-5" parsed as the visits column.

Common situations: Diff computations written back to storage producing negative deltas; ETL bugs subtracting counts; manual data edits; overflow/misparse of signed columns.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of inancgumus/learngo@3c475a78e5 (2026-09-02). Data as JSON: /api/errors/b2baffce12b12c80. Report an issue: GitHub.