cratis/ai.distribution/add-business-rule
Claude Code and agent skill by cratis/ai.distribution
- community
Checking that the SKILL.md file is still in the repository.
Use this skill when asked to add a validation rule, business rule, or uniqueness constraint to an existing command in a Cratis-based project.
Install
npx vskill@latest install cratis/ai.distribution/add-business-ruleSKILL.md
The same SKILL.md was published earlier as cratis/ai.distribution/add-business-rule.
Add a business rule or event-store constraint to an existing command.
Choose the right mechanism
Pick by what the decision is — see vertical-slices.md "The decision matrix":
| Scenario | Use | |
|---|---|---|
| Reusable value invariant (length, format, range) | ConceptValidator<T> on the concept type | |
| Command-input / cross-field / pre-handler rule (incl. injected read-model/service checks) | CommandValidator<TCommand> with RuleFor(...) | |
| Handler needs fetched/computed data before it can build the event | Provide() — fetch the data; short-circuit with ValidationResult.Error(...) if unusable | |
| State-dependent rule that must hold under concurrency | inject the read model into Handle(), return Result<TEvent, ValidationResult> | |
| Single-event uniqueness (most cases) | [Unique] attribute on the event type | |
Multi-event uniqueness or needs RemovedWith | IConstraint | |
| Genuinely exceptional failure (bug, missing infra) | throw a custom domain exception |
Never throw for normal business rejection. A thrown exception fromProvide()/Handle()surfaces asHasExceptions/ HTTP 500 — not a validation result. Recoverable, user-facing rejections are validation: return them via a validator,Provide(), orResult<TEvent, ValidationResult>.
Business rules via DCB (read model as Handle() argument)
Use when the rule depends on Chronicle event-sourced state (current count, accumulated value) and must hold under concurrency. The framework injects the current read-model snapshot, resolved by the command's event-source id, before Handle() runs. Return a Result<TEvent, ValidationResult> — success carries the event, failure carries a typed validation error.
The read model must already exist in the slice ([ReadModel] + a model-bound projection). If it doesn't, add it first — see the add-projection / cratis-readmodel skills.
Key rules:
- The read-model parameter type must be a
[ReadModel]in the same slice/feature; the framework resolves the instance by the command's event-source key. To read a read model keyed differently, useProvide()withIReadModels.GetInstanceById<T>((EventSourceId)key). - Return
Result<TEvent, ValidationResult>— never throw for the rejection. - One parameter per logical read model; multiple reads → multiple parameters.
Pre-handler rules — CommandValidator<T>
For rules that don't depend on race-sensitive state, put them in the validator that sits beside the command. Constructor dependencies (read models, services) are injected and resolved by the command's event-source id.
Single-property intrinsic rules belong on ConceptValidator<T> instead, so they travel with the value everywhere.
Event-store constraints — [Unique] (preferred)
For uniqueness on a single event type, adorn the event type or one of its properties with [Unique]. No separate constraint class is needed; constraints are discovered automatically.
Stack multiple [RemoveConstraint("...")] attributes on one removal event to release several claimed values at once. To enforce the same property across multiple event types, give each its [Unique(name: "UniqueEmail")] with the same name.
Event-store constraints — IConstraint (advanced)
Use IConstraint when uniqueness spans event types with different property names, needs .IgnoreCasing(), or a RemovedWith event must release it. Define is declarative — member-access lambdas only, no DI or side effects.
- The
.On<TEvent>(e => e.Prop)lambda is parsed for member access only —e => e.Email.ToLower()is a pitfall; use.IgnoreCasing(). builder.Unique<TEvent>(name:, message:)enforces "one event of this type per event source" via the fluent builder (distinct from class-level[Unique]).- Treat a constraint violation as a validation result on the command append — never as a thrown exception.
Spec the constraint with EventScenario: seed the conflicting state, append again, and assert ShouldHaveConstraintViolationFor(<Module>ConstraintNames.UniqueX) — the constraint name, never the message. Use per-test unique values ($"{Guid.NewGuid():N}") so specs aren't order-dependent. For a release, append the removal event then assert the value can be re-claimed.
After adding
- Add a spec for the failure case — assert both
ShouldNotBeSuccessful()andShouldHaveValidationErrors()(orShouldHaveConstraintViolationFor(name)for constraints). See thewrite-specs/write-specs-eventsskills. - Build clean (Debug and Release) and run the specs.
- Fix all failures before completing.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/add-business-rule/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/cratis/ai.distribution/add-business-rule)