bchavez/Bogus · error · ArgumentException

Cannot create a rule set within a rule set.

Error message

Cannot create a rule set within a rule set.

What it means

Thrown by Faker<T>.RuleSet when RuleSet is invoked while another RuleSet is still being defined (currentRuleSet != Default). Bogus disallows nested rule sets because each call resets currentRuleSet to Default, so nesting would corrupt the active set tracking.

Source

Thrown at Source/Bogus/Faker[T].cs:353

         return pi.PropertyType;
      }
      if( mi is FieldInfo fi )
      {
         return fi.FieldType;
      }
      return null;
   }

   /// <summary>
   /// Defines a set of rules under a specific name. Useful for defining
   /// rules for special cases. Note: The name `default` is the name of all rules that are
   /// defined without an explicit rule set.
   /// </summary>
   /// <param name="ruleSetName">The rule set name.</param>
   /// <param name="action">The set of rules to apply when this rules set is specified.</param>
   public virtual Faker<T> RuleSet(string ruleSetName, Action<IRuleSet<T>> action)
   {
      if( currentRuleSet != Default ) throw new ArgumentException("Cannot create a rule set within a rule set.");
      currentRuleSet = ruleSetName;
      action(this);
      currentRuleSet = Default;
      return this;
   }

   /// <summary>
   /// Ensures a member exists provided by the IBinder.
   /// </summary>
   protected virtual void EnsureMemberExists(string propNameOrField, string exceptionMessage)
   {
      if (!this.TypeProperties.TryGetValue(propNameOrField, out MemberInfo mi))
      {
         throw new ArgumentException(exceptionMessage);
      }
   }

   /// <summary>

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Flatten the nested RuleSet calls into sibling sequential RuleSet invocations at the top level.
  2. If composition is needed, define each RuleSet separately and select multiple sets via comma-separated names at generate time.
  3. Move the inner rules into a helper method that uses RuleFor rather than RuleSet.

Example fix

// before
f.RuleSet("A", set => {
    set.RuleFor(x => x.A, _ => 1);
    set.RuleSet("B", s2 => s2.RuleFor(x => x.B, _ => 2));
});
// after
f.RuleSet("A", set => set.RuleFor(x => x.A, _ => 1));
f.RuleSet("B", set => set.RuleFor(x => x.B, _ => 2));
Defensive patterns

Strategy: validation

Validate before calling

// before defining rulesets, ensure you are at top-level Faker configuration
// Bogus exposes no public accessor for currentRuleSet, so structure code to call RuleSet only sequentially

Try / catch

try { f.RuleSet(name, action); }
catch (ArgumentException ex) when (ex.Message.Contains("within a rule set")) {
    // move the inner RuleSet out to the top level and re-run config
}

Prevention

When it happens

Trigger: Calling faker.RuleSet("A", set => { set.RuleSet("B", ...); }) i.e. invoking RuleSet inside the Action<IRuleSet<T>> callback of an outer RuleSet.

Common situations: Refactoring rule definitions and accidentally nesting calls, or attempting to compose named rule sets by calling one RuleSet from inside another.

Related errors


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