-
Notifications
You must be signed in to change notification settings - Fork 212
refactor: PR 1.3 Refactor validator to use pure options pattern #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
developerkunal
wants to merge
4
commits into
v3-phase1-pr2-core-package
Choose a base branch
from
v3-phase1-pr3-validator-options
base: v3-phase1-pr2-core-package
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
refactor: PR 1.3 Refactor validator to use pure options pattern #357
developerkunal
wants to merge
4
commits into
v3-phase1-pr2-core-package
from
v3-phase1-pr3-validator-options
Conversation
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
Refactors validator.New() from positional parameters to pure options pattern, improving API consistency and extensibility.
Breaking Changes:
- validator.New() now takes only options (no positional parameters)
- All parameters must now use option functions
Before:
validator.New(keyFunc, algorithm, issuer, audience, opts...)
After:
validator.New(
validator.WithKeyFunc(keyFunc),
validator.WithAlgorithm(validator.RS256),
validator.WithIssuer("https://issuer.example.com/"),
validator.WithAudience("my-api"),
)
New Options:
- WithKeyFunc: Required key function
- WithAlgorithm: Required signature algorithm
- WithIssuer: Required issuer URL (with validation)
- WithAudience/WithAudiences: Required audience(s)
Coverage:
- validator package: 100.0%
- All tests passing
- All examples updated
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v3-phase1-pr2-core-package #357 +/- ##
==============================================================
+ Coverage 97.72% 97.95% +0.23%
==============================================================
Files 13 13
Lines 396 441 +45
==============================================================
+ Hits 387 432 +45
Misses 6 6
Partials 3 3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Introduces generics to WithCustomClaims for improved type safety, cleaner API ergonomics and better developer experience.
Summary of Improvements
What Changed
Before (non-generic):
validator.WithCustomClaims(func() validator.CustomClaims {
return &MyClaims{}
})
After (with generics):
validator.WithCustomClaims(func() *MyClaims {
return &MyClaims{}
})
Benefits
1. Type Safety. Compiler ensures T implements CustomClaims at compile time.
2. Cleaner API. Users no longer need to return interface types explicitly.
3. Better IDE Support. Autocomplete works with concrete types.
4. Flexible. Allows nil returns for conditional custom claims with identical runtime behavior.
5. Full Coverage. All tests pass.
Implementation Details
- Introduces WithCustomClaims[T CustomClaims](f func() T)
- Wraps user function internally to return the interface
- No breaking changes. All existing usage continues to work
- Type parameter is inferred from function return type
- Nil functions require explicit type: WithCustomClaims[*MyClaims](nil)
Test Results
- validator package: 100.0 percent coverage
- All tests passing
Updates all example projects to use the new generic WithCustomClaims API and to reference the v3 module path. All example builds succeed with the updated validator version. Changes included: 1. Updated every example to use the new generic WithCustomClaims syntax across gin, echo, http and iris. 2. Updated each example go.mod file to reference v3 rather than v2 and added the appropriate replace directives. 3. Verified that all examples build correctly with the revised API.
2 tasks
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.
📝 Checklist
🔧 Changes
Refactors
validator.New()from positional parameters to a pure options pattern. Introduces generic type-safe custom claims handling.Breaking Changes:
New Options:
Required:
WithKeyFunc(func)- Key function for token verificationWithAlgorithm(SignatureAlgorithm)- Signature algorithmWithIssuer(string)- Expected issuer claimWithAudience(string)/WithAudiences([]string)- Expected audience(s)Optional:
WithCustomClaims[T CustomClaims](func() T)- Type-safe custom claims (now generic)WithAllowedClockSkew(duration)- Clock skew toleranceType-Safe Custom Claims:
Files Modified:
validator/option.go- Required options + generic WithCustomClaimsvalidator/validator.go- Refactored constructorvalidator/validator_test.go- Updated testsvalidator/security_test.go- Updated CVE testmiddleware_test.go- Updated tests📚 References
🔬 Testing
All examples verified to build successfully.