bchavez/Bogus · error · ArgumentOutOfRangeException
PESEL for the year below 1800 is invalid.
Error message
PESEL for the year below 1800 is invalid.
What it means
ExtensionsForPoland.Pesel encodes the birth date with a month shift that only has defined buckets for years 1800-2299 (in 100-year steps). A year below 1800 throws ArgumentOutOfRangeException. This is a hard limit of the PESEL encoding scheme.
Source
Thrown at Source/Bogus/Extensions/Poland/ExtensionsForPoland.cs:42
{
return (string)value;
}
return new StringBuilder()
.AppendPeselDateOfBirth(person.DateOfBirth)
.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.MaleView on GitHub (pinned to 6ece18c5c2)
Solutions
- Ensure Person.DateOfBirth.Year is >= 1800 before calling Pesel.
- Generate birth dates with f.Date.Between(new DateTime(1900,1,1), DateTime.Today).
- Guard against default(DateTime) (year 1) being assigned to DateOfBirth.
Example fix
// before person.DateOfBirth = new DateTime(1750, 1, 1); var pesel = person.Pesel(); // after person.DateOfBirth = f.Date.Between(new DateTime(1960,1,1), DateTime.Today); var pesel = person.Pesel();
Defensive patterns
Strategy: validation
Validate before calling
int year = person.DateOfBirth.Year;
if (year < 1800)
throw new ArgumentOutOfRangeException(nameof(year), "PESEL requires year >= 1800.");
var pesel = person.Pesel(); Type guard
static bool PeselYearOk(int y) => y >= 1800 && y < 2300;
Prevention
- Guard against default(DateTime) (year 1) leaking into DateOfBirth.
- Use realistic recent birth dates for fixtures.
- The PESEL lower bound is a hard 1800.
When it happens
Trigger: Calling person.Pesel() on a Person whose DateOfBirth.Year is less than 1800.
Common situations: Historical test data with a pre-1800 birth date, or an accidental DateTime.MinValue / default(DateTime) DateOfBirth.
Related errors
- PESEL for year above 2300 is invalid.
- {nameof(year)} must be between 1858 and 2057.
- {nameof(year)} must be between 1854 and 2039.
- {nameof(p.DateOfBirth.Year)} must be between 1900 and 2099.
- Gender not handled.
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/39aeed612ceb4696.
Report an issue: GitHub.