bchavez/Bogus · error · ArgumentOutOfRangeException

PESEL for year above 2300 is invalid.

Error message

PESEL for year above 2300 is invalid.

What it means

The PESEL month-shift switch only defines buckets up to year 2299; a year of 2300 or above throws ArgumentOutOfRangeException. The Pesel XML doc confirms the supported range is 1800-2300.

Source

Thrown at Source/Bogus/Extensions/Poland/ExtensionsForPoland.cs:48

         .Append(person.Random.Number(9))
         .Append(person.Random.Number(9))
         .Append(person.Random.Number(9))
         .AppendPeselGender(person)
         .AppendPeselChecksum()
         .ToString();
   }

   private static StringBuilder AppendPeselDateOfBirth(this StringBuilder builder, DateTime dateOfBirth)
   {
      int monthShift = dateOfBirth.Year switch
      {
         < 1800 => throw new ArgumentOutOfRangeException("PESEL for the year below 1800 is invalid."),
         < 1900 => 80,
         < 2000 => 0,
         < 2100 => 20,
         < 2200 => 40,
         < 2300 => 60,
         _ => throw new ArgumentOutOfRangeException("PESEL for year above 2300 is invalid."),
      };

      return builder
         .Append((dateOfBirth.Year % 100).ToString("00"))
         .Append((dateOfBirth.Month+monthShift).ToString("00"))
         .Append(dateOfBirth.Day.ToString("00"));
   }

   private static StringBuilder AppendPeselGender(this StringBuilder builder, Person person)
   {
      return builder
         .Append(person.Gender == Name.Gender.Male
            ? person.Random.Odd(0, 9)
            : person.Random.Even(0, 9));
   }

   private static StringBuilder AppendPeselChecksum(this StringBuilder builder)
   {

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Keep Person.DateOfBirth.Year below 2300 when calling Pesel.
  2. Use realistic recent birth-date ranges for fixtures.
  3. Validate the year is within 1800-2299 before generation.

Example fix

// before
person.DateOfBirth = new DateTime(2400, 1, 1);
var pesel = person.Pesel();

// after
person.DateOfBirth = f.Date.Between(new DateTime(1980,1,1), DateTime.Today);
var pesel = person.Pesel();
Defensive patterns

Strategy: validation

Validate before calling

int year = person.DateOfBirth.Year;
if (year >= 2300)
    throw new ArgumentOutOfRangeException(nameof(year), "PESEL requires year < 2300.");
var pesel = person.Pesel();

Type guard

static bool PeselYearOk(int y) => y >= 1800 && y < 2300;

Prevention

When it happens

Trigger: Calling person.Pesel() on a Person whose DateOfBirth.Year is 2300 or greater.

Common situations: Future-dated fixture data (e.g. a 2400 birth date) or an off-by error producing a far-future year.

Related errors


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/d75c99d1cd9ee408. Report an issue: GitHub.