bchavez/Bogus · error · ArgumentOutOfRangeException

Can only accept registration dates between {EarliestRegistra

Error message

Can only accept registration dates between {EarliestRegistration:yyyy-MM-dd} and {LatestRegistration:yyyy-MM-dd}.

What it means

ExtensionsForGreatBritainRegistrationPlate only models the current UK plate scheme, so GenerateRegistrationDate rejects any dateFrom outside 2001-09-01 to 2051-02-28. The bounds are the static fields EarliestRegistration (StartOfCurrentStyle = 2001-09-01) and LatestRegistration (2051-02-28). This overload throws on the dateFrom argument specifically.

Source

Thrown at Source/Bogus/Extensions/UnitedKingdom/ExtensionsForGreatBritainRegistrationPlate.cs:134

               return 'C';
            case 6:
            case 12:
               return 'D';
         }

         throw new InvalidOperationException(
            $"This code path should never be accessible. {nameof(primaryLocation)}={primaryLocation}; {nameof(registrationDate)}={registrationDate:O}");
      }
      
      char[] secondaryLocationChoices = SecondaryLocations[primaryLocation];
      char secondaryLocation = vehicle.Random.ArrayElement(secondaryLocationChoices);
      return secondaryLocation;
   }

   private static DateTime GenerateRegistrationDate(Vehicle vehicle, DateTime dateFrom, DateTime dateTo)
   {
      if (dateFrom < EarliestRegistration || dateFrom > LatestRegistration)
          throw new ArgumentOutOfRangeException(nameof(dateFrom), $"Can only accept registration dates between {EarliestRegistration:yyyy-MM-dd} and {LatestRegistration:yyyy-MM-dd}.");
      if (dateTo < EarliestRegistration || dateTo > LatestRegistration)
          throw new ArgumentOutOfRangeException(nameof(dateTo),$"Can only accept registration dates between {EarliestRegistration:yyyy-MM-dd} and {LatestRegistration:yyyy-MM-dd}.");
      
      // Swap the values of the to and from dates if they're the wrong way around.
      if (dateFrom > dateTo)
      {
         // Syntax not supported: (dateFrom, dateTo) = (dateTo, dateFrom);
         DateTime valueHolder = dateFrom;
         dateFrom = dateTo;
         dateTo = valueHolder;
      }
      
      dateFrom = dateFrom.Date;
      dateTo = dateTo.Date;
      int duration = (int) (dateTo - dateFrom).TotalDays;
      int offset = vehicle.Random.Int(0, duration);
      DateTime registrationDate = dateFrom.AddDays(offset);
      return registrationDate;

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Pass a dateFrom within 2001-09-01 .. 2051-02-28.
  2. Use a recent range such as new DateTime(2010,1,1) to DateTime.Today.
  3. For pre-2001 vehicles, use a different generator - this API only covers the current scheme.

Example fix

// before
var plate = vehicle.GbRegistrationPlate(new DateTime(1995,1,1), new DateTime(2000,1,1));

// after
var plate = vehicle.GbRegistrationPlate(new DateTime(2010,1,1), new DateTime(2020,1,1));
Defensive patterns

Strategy: validation

Validate before calling

DateTime earliest = new(2001,9,1), latest = new(2051,2,28);
if (dateFrom < earliest || dateFrom > latest)
    throw new ArgumentOutOfRangeException(nameof(dateFrom), $"Use {earliest:yyyy-MM-dd}..{latest:yyyy-MM-dd}");
var plate = vehicle.GbRegistrationPlate(dateFrom, dateTo);

Type guard

static bool WithinGbWindow(DateTime d) => d >= new DateTime(2001,9,1) && d <= new DateTime(2051,2,28);

Prevention

When it happens

Trigger: Calling vehicle.GbRegistrationPlate(dateFrom, dateTo) where dateFrom is before 2001-09-01 or after 2051-02-28 (e.g. new DateTime(1999,1,1) or new DateTime(2060,1,1)).

Common situations: Generating plates for older vehicles (pre-2001 suffix/prefix plates) which this extension does not support, or passing a future date range past 2051.

Related errors


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