[rig-sampler] Add s.literal helper for single-value enum constraints - #34
Conversation
Adds s.literal(value, description?) as a more expressive alternative
to s.enum(value) when only one exact value is valid. The inferred
TypeScript type is the literal itself (e.g. s.literal("done") infers
as "done").
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅ |
There was a problem hiding this comment.
Skills-Based Review
Applied /tdd - approving with one non-blocking suggestion on test duplication.
- Minor test duplication: the 'converts literal schemas' case in the toJsonSchema block repeats three assertions already in the new s.literal suite. Low risk but worth cleaning.
Positive highlights:
- Clean thin-wrapper, no new logic, delegates to existing EnumSchema path
- const T generic correctly narrows the inferred type to the literal
- Tests cover serialization, validation accept/reject, and description propagation
- JSDoc examples and header summary updated
🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · 27.2 AIC · ⌖ 5.76 AIC · ⊞ 6.3K
Comment /matt to run again
| expect(toJsonSchema(s.literal("done"))).toEqual({ type: "string", enum: ["done"] }); | ||
| expect(toJsonSchema(s.literal(42))).toEqual({ enum: [42] }); | ||
| expect(toJsonSchema(s.literal(true, "must be true"))).toEqual({ enum: [true], description: "must be true" }); | ||
| }); |
There was a problem hiding this comment.
[/tdd] These three assertions duplicate the ones in the new s.literal describe block below (lines ~1186–1219). Keeping them in both places will cause drift if the behaviour changes.
💡 Suggestion
Remove this "converts literal schemas" test case from the toJsonSchema describe block entirely — the dedicated s.literal suite already covers serialization exhaustively. Alternatively, replace the three expects with a single representative assert here and add a comment pointing to the full suite.
Sample run
Ran sample
14-changelog-categorizer.ts(a validation-plan agent) through the stub harness. The agent produced valid JSON in 1 turn with no repair. Output schema useds.array(s.string)forcommandsandmanualChecks, ands.stringforrationale.What the run revealed
The schema helpers are well-used in samples, and enum-based constraint is a recurring pattern. However, when a field must be exactly one specific value (e.g. a
"done"status sentinel or a fixed protocol token), authors must writes.enum("done")— which works but doesn't clearly signal "single literal" intent. It can also be confused with "one of many choices".Change
Added
s.literal<T>(value: T, description?: string): EnumSchema<[T]>to theshelpers:EnumSchema/serializeSchemapath (no new logic).T, not a union.rig.ts.src/rig.test.tscovering serialization, validation (pass/fail), and description propagation.