[5/11] Add oneof checker - #228
Merged
Merged
Conversation
Without oneof, every enum-shaped field needs a hand-written
RegisterMaker call -- the single most commonly cited DX gap across the
source reports.
- IsOneOf is generic (IsOneOf[T comparable]) so it works standalone
for any comparable type, not just strings.
- The reflect-based maker splits its config on comma, not space:
checker tag fields are already split on whitespace before an
individual checker's own params are parsed, so "oneof:foo bar baz"
as originally proposed can't actually be parsed by the existing tag
DSL. Followed the precedent already set by
credit-card:visa,mastercard instead: oneof:admin,user,guest.
- The error message includes the allowed values via a generic
newOneOfError helper (same pattern as newGteError/newEqFieldError),
so failures are actionable instead of a bare code.
- schema_maker.go maps oneof to JSON Schema's Enum, which nothing
previously populated.
- NOT_ONE_OF message with an {{ .allowed }} placeholder added to all
23 locale files plus en-US.
- Documented in the README's checker table, with the comma-vs-space
syntax called out explicitly.
Fixes #202
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FLdVmP5daHiknrTW4Geh2i
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #228 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 53 54 +1
Lines 958 983 +25
=========================================
+ Hits 958 983 +25 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Without
oneof, every enum-shaped field needs a hand-writtenRegisterMakercall — the single most commonly cited DX gap across every one of the six source reports behind this issue list.Fix
Add
oneof, following the established checker file pattern:IsOneOfis generic (IsOneOf[T comparable]) so it's usable standalone for any comparable type, not just strings.oneof:admin,user,guest(comma-separated), notoneof:admin user guest(space-separated) as originally proposed. Checker tag fields are already split on whitespace before an individual checker's own params are parsed (maker.go'smakeChecksdoesstrings.Fields(config)over the whole tag first), so a space-separated value list literally can't be parsed by the existing tag DSL — it would be seen as multiple unrelated checker names. Followed the precedent already set bycredit-card:visa,mastercardinstead.newOneOfErrorhelper (same pattern as the existingnewGteError/newEqFieldError), so a failure is actionable instead of a bare code:"Value must be one of admin, user, guest.".schema_maker.gomapsoneofto JSON Schema'sEnum— a field that existed onSchemabut nothing previously populated.NOT_ONE_OFmessage with an{{ .allowed }}placeholder added to all 23 locale files plus en-US.Testing
oneof_test.go: valid/invalid, generic non-string usage (int), error message content, empty-config panic, non-string-field panic, struct-tag valid/invalid.TestJSONSchemaOneOfEnumfor theEnummapping.go vet,gosec,reviveclean.Fixes #202
Chain note: PR 5 of 11 — branched locally on top of the now-merged uuid PR, then rebased onto current
mainso the diff here is only this PR's own changes.🤖 Generated with Claude Code
https://claude.ai/code/session_01FLdVmP5daHiknrTW4Geh2i