Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
name: design-value-objects
description: "Replace primitive obsession with focused value objects that make domain invariants explicit and difficult to bypass. Use when a primitive carries domain meaning, when validation is repeated, or when designing construction, equality, mutation, serialization, and boundary APIs for domain values. Pair with parse-dont-validate when the value is created from untrusted input. Do not use for ordinary data containers, external facts that change over time, or framework-specific schema configuration."
allowed-tools:
- AskUserQuestion
- Read
- Write
- Bash
---

# Design Value Objects

Give domain concepts their own types instead of passing primitives whose meaning exists only in names, comments, or calling conventions. A value object combines a representation with the stable invariant that makes the value safe to use.

## Core Principle

Primitive obsession occurs when domain concepts such as email addresses, money, identifiers, or bounded quantities remain strings and numbers throughout the system. Replace the primitive with a value object when the concept has an invariant, domain behavior, or a meaningful boundary.

```text
primitive -> constructor/parser -> ValueObject -> domain operation
```

Use [parse-dont-validate](../parse-dont-validate/SKILL.md) when the value comes from untrusted input. Parsing establishes the value object's invariant at the boundary; this skill defines what the value object should guarantee after construction.

## Procedure

1. Name the domain concept currently represented by a primitive.
2. State the stable invariant that every valid instance must preserve.
3. Choose a representation that cannot be confused with neighboring concepts.
4. Restrict construction to a named constructor or parser that establishes the invariant.
5. Make the representation immutable when possible. If mutation is required, expose only invariant-preserving operations.
6. Define equality and serialization in terms of the domain concept, not incidental representation details.
7. Change downstream APIs to accept the value object and remove checks that merely repeat its stable invariant.
8. Keep checks for mutable state and external facts at the operation boundary.

Read [the checklist](references/checklist.md) before considering the design complete.

## Relationship to Existing Guidance

- `design-value-objects` provides the modeling and API procedure.
- `parse-dont-validate` handles construction from untrusted or weakly typed input.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Value Object Checklist

- [ ] The primitive represents a named domain concept.
- [ ] The stable invariant is stated explicitly.
- [ ] Construction cannot bypass the invariant.
- [ ] The representation is immutable or mutation preserves the invariant.
- [ ] Equality reflects the domain concept.
- [ ] Serialization and deserialization preserve the concept's contract.
- [ ] Downstream APIs accept the value object instead of the primitive.
- [ ] Checks for mutable state and external facts remain at operation boundaries.
- [ ] Repeated stable-invariant checks have been removed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: parse-dont-validate
description: "Turn untrusted or weakly typed input into a type that carries its invariants, so downstream code can rely on the result without repeating validation. Use when designing parsers, boundary APIs, value objects, domain types, deserialization, configuration loading, or review guidance about validation. Do not use for sanitization that intentionally changes data, validation-only reporting, or choosing a framework-specific schema library."
allowed-tools:
- AskUserQuestion
- Read
- Write
- Bash
---

# Parse, Don't Validate

Parse external data once into a representation that makes the required invariants explicit. After parsing succeeds, downstream functions should accept the refined type and should not need to re-check the same conditions.

## Core Principle

Validation answers whether a value is acceptable while leaving the caller with the same weak type. Parsing answers by producing a stronger value or an explicit failure. Prefer APIs that make the valid state easy to use and the unparsed state impossible to pass accidentally.

```text
raw input -> parse -> Result<ParseError, RefinedValue> -> domain operations
```

An `Email` value should be constructible only through its parser, and `send_email` should accept `Email`, not `String`. A successful parse is evidence carried by the type, rather than a fact a comment or a prior call must preserve. Read [value objects](references/value-objects.md) when choosing the refined type or deciding how it may change after construction.

## Procedure

1. Identify the boundary where data first enters the trusted part of the system.
2. List the invariants the downstream operation requires, including shape, syntax, ranges, and cross-field relationships.
3. Define a refined value object or domain type whose construction is restricted to the parser. Keep it immutable or make every mutation preserve its invariant. Read [value objects](references/value-objects.md) for the relationship between parsing and refined types.
4. Return an explicit success-or-failure result with actionable parse errors. Do not hide failure in a boolean, assertion, nullable value, or exception unless the surrounding API requires it.
5. Make downstream functions accept the refined type so callers cannot skip the parse step.
6. Remove duplicate validation from downstream code. Keep checks for mutable state or external facts that can change after parsing, and perform those checks at the operation boundary.
7. Test both rejection cases and the guarantee that successful parsing produces a value accepted by downstream operations.

Read [review guidance](references/review.md) when reviewing an implementation or writing a finding. Read [the checklist](references/checklist.md) before considering the work complete.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Parse Don't Validate Checklist

- [ ] Parsing occurs at the earliest trusted boundary.
- [ ] The refined type carries the stable invariant in its API.
- [ ] Construction bypasses are restricted or impossible.
- [ ] The representation is immutable or mutation preserves its invariant.
- [ ] Failure is explicit and preserves useful context.
- [ ] Downstream APIs accept refined values instead of raw primitives.
- [ ] Checks for mutable state and external facts remain at the operation boundary.
- [ ] Repeated checks enforce distinct invariants or have been removed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Parse Don't Validate Review

## Questions

- Can an unparsed primitive reach code that assumes the invariant?
- Can callers construct the refined type without parsing?
- Is the refined value immutable or protected by invariant-preserving operations?
- Does the parser preserve enough context to explain failure?
- Are repeated checks proving the same stable invariant, or do they protect mutable state or a new boundary?
- Is sanitization being confused with parsing? Sanitization transforms data; parsing establishes what the data means.

## Report Format

```markdown
### <boundary or invariant>

- Raw input: <current weak type and source>
- Refined type: <type that carries the invariant>
- Parser: <function or boundary that constructs it>
- Failure: <explicit error/result shape>
- Downstream change: <functions that now accept the refined type>
- Checks retained: <mutable or external facts still checked at the operation boundary>
- Duplicate checks removed: <same-invariant checks, or "none">
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Parse Results and Value Objects

Parsing and value objects are two parts of the same design. Parsing checks raw input and constructs a refined value object. The value object carries the stable invariant so downstream APIs can require it by type.

**Related skills:** `design-value-objects` is the canonical guide for replacing primitive obsession with domain types. Use this parse skill when deciding how those values are constructed and verified at an input boundary.

```text
raw input -> parser -> Result<ParseError, Email> -> send_email(Email)
```

## Construction

- Restrict construction so callers cannot create an unchecked value.
- Keep the representation immutable when possible.
- If mutation is required, expose operations that preserve the invariant or return a new parsed value.

## Boundary of the Guarantee

The value object guarantees facts established during parsing. It cannot guarantee external state that changes later. A parsed account identifier does not prove that an account still exists, and a parsed purchase amount does not prove that the current balance can cover it. Check those facts at the operation boundary, atomically with the state change when needed.

## Relationship to Validation

Validation can answer whether a raw value is acceptable, but it leaves the caller with the same weak type. Parsing turns that answer into a value whose API records what has been established. Do not remove a check merely because a value was parsed; remove it only when it repeats the same stable invariant.