bchavez/Bogus · error · ArgumentOutOfRangeException

{nameof(p.DateOfBirth.Year)} must be between 1900 and 2099.

Error message

{nameof(p.DateOfBirth.Year)} must be between 1900 and 2099.

What it means

ExtensionsForRomania.Cnp encodes the sex/century digit and handles three eras: 1800-1899, 1900-1999, 2000-2099. A year outside 1800-2099 throws ArgumentOutOfRangeException. NOTE: the message text claims 'must be between 1900 and 2099' but the code actually supports 1800-2099 - the message omits the 1800-1899 era that IS handled, so treat the message as misleading and use 1800-2099 as the real supported window.

Source

Thrown at Source/Bogus/Extensions/Romania/ExtensionsForRomania.cs:61

      var randomizer = p.Random;

      var judet = randomizer.Enum<RomanianBirthCounty>();
      int sex;
      //persoanele rezidente
      //sex = p.Gender == Gender.Male ? 7 : 8;
      switch (p.DateOfBirth.Year)
      {
         case >= 1900 and <= 1999:
            sex = p.Gender == Name.Gender.Male ? 1 : 2;
            break;
         case >= 1800 and <= 1899:
            sex = p.Gender == Name.Gender.Male ? 3 : 4;
            break;
         case >= 2000 and <= 2099:
            sex = p.Gender == Name.Gender.Male ? 5 : 6;
            break;
         default:
            throw new ArgumentOutOfRangeException(nameof(p.DateOfBirth.Year),
               $"{nameof(p.DateOfBirth.Year)} must be between 1900 and 2099.");
      }

      var seq = randomizer.Int(1, 999);
      var cnp = sex +
                p.DateOfBirth.ToString("yyMMdd") +
                ((int)judet).ToString("00") +
                seq.ToString("000");

      var checksum = GenerateChecksum(cnp);
      var final = cnp + checksum;
      p.context[Key] = final;

      return final;
   }

   private static string GenerateChecksum(string cnp)
   {

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Keep Person.DateOfBirth.Year within 1800-2099 (despite the message saying 1900-2099).
  2. Use f.Date.Between(new DateTime(1900,1,1), DateTime.Today) for realistic dates.
  3. Avoid future years beyond 2099 for Romanian CNP fixtures.

Example fix

// before
person.DateOfBirth = new DateTime(2100, 1, 1);
var cnp = person.Cnp();

// after
person.DateOfBirth = f.Date.Between(new DateTime(1970,1,1), DateTime.Today);
var cnp = person.Cnp();
Defensive patterns

Strategy: validation

Validate before calling

// Real supported window is 1800-2099 despite the message text.
int year = person.DateOfBirth.Year;
if (year < 1800 || year > 2099)
    throw new ArgumentOutOfRangeException(nameof(year), "Romanian CNP requires 1800-2099.");
var cnp = person.Cnp();

Type guard

static bool CnpYearOk(int y) => y >= 1800 && y <= 2099;

Prevention

When it happens

Trigger: Calling person.Cnp() on a Person whose DateOfBirth.Year is below 1800 or above 2099.

Common situations: Setting DateOfBirth to a far-future date (>2099) or pre-1800 historical date, then generating a Romanian CNP. Developers may also be confused by the message claiming 1900 is the lower bound when 1800-1899 actually works.

Related errors


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