{"record":{"id":"d5ae63df60199e4c","repo":"bchavez/Bogus","slug":"cannot-create-a-rule-set-within-a-rule-set","errorCode":null,"errorMessage":"Cannot create a rule set within a rule set.","messagePattern":"Cannot create a rule set within a rule set\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Faker[T].cs","lineNumber":353,"sourceCode":"         return pi.PropertyType;\n      }\n      if( mi is FieldInfo fi )\n      {\n         return fi.FieldType;\n      }\n      return null;\n   }\n\n   /// <summary>\n   /// Defines a set of rules under a specific name. Useful for defining\n   /// rules for special cases. Note: The name `default` is the name of all rules that are\n   /// defined without an explicit rule set.\n   /// </summary>\n   /// <param name=\"ruleSetName\">The rule set name.</param>\n   /// <param name=\"action\">The set of rules to apply when this rules set is specified.</param>\n   public virtual Faker<T> RuleSet(string ruleSetName, Action<IRuleSet<T>> action)\n   {\n      if( currentRuleSet != Default ) throw new ArgumentException(\"Cannot create a rule set within a rule set.\");\n      currentRuleSet = ruleSetName;\n      action(this);\n      currentRuleSet = Default;\n      return this;\n   }\n\n   /// <summary>\n   /// Ensures a member exists provided by the IBinder.\n   /// </summary>\n   protected virtual void EnsureMemberExists(string propNameOrField, string exceptionMessage)\n   {\n      if (!this.TypeProperties.TryGetValue(propNameOrField, out MemberInfo mi))\n      {\n         throw new ArgumentException(exceptionMessage);\n      }\n   }\n\n   /// <summary>","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Faker[T].cs#L335-L371","documentation":"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.","triggerScenarios":"Calling faker.RuleSet(\"A\", set => { set.RuleSet(\"B\", ...); }) i.e. invoking RuleSet inside the Action<IRuleSet<T>> callback of an outer RuleSet.","commonSituations":"Refactoring rule definitions and accidentally nesting calls, or attempting to compose named rule sets by calling one RuleSet from inside another.","solutions":["Flatten the nested RuleSet calls into sibling sequential RuleSet invocations at the top level.","If composition is needed, define each RuleSet separately and select multiple sets via comma-separated names at generate time.","Move the inner rules into a helper method that uses RuleFor rather than RuleSet."],"exampleFix":"// before\nf.RuleSet(\"A\", set => {\n    set.RuleFor(x => x.A, _ => 1);\n    set.RuleSet(\"B\", s2 => s2.RuleFor(x => x.B, _ => 2));\n});\n// after\nf.RuleSet(\"A\", set => set.RuleFor(x => x.A, _ => 1));\nf.RuleSet(\"B\", set => set.RuleFor(x => x.B, _ => 2));","handlingStrategy":"validation","validationCode":"// before defining rulesets, ensure you are at top-level Faker configuration\n// Bogus exposes no public accessor for currentRuleSet, so structure code to call RuleSet only sequentially","typeGuard":null,"tryCatchPattern":"try { f.RuleSet(name, action); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"within a rule set\")) {\n    // move the inner RuleSet out to the top level and re-run config\n}","preventionTips":["Keep all RuleSet calls as siblings, never nested.","Review the Action body for any RuleSet invocation.","Use a builder helper that enforces flat RuleSet registration."],"tags":["bogus","ruleset","nested-config"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}