Skip to content

Commit c6f4c37

Browse files
committed
Inlining application-specific validation
1 parent 9fa0fd4 commit c6f4c37

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

example_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ type MyCustomClaims struct {
121121
jwt.RegisteredClaims
122122
}
123123

124-
func (m MyCustomClaims) CustomValidation() error {
124+
// Validate can be used to execute additional application-specific claims
125+
// validation.
126+
func (m MyCustomClaims) Validate() error {
125127
if m.Foo != "bar" {
126128
return errors.New("must be foobar")
127129
}

validator.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ type validator struct {
3939
expectedSub string
4040
}
4141

42-
// customClaims represents a custom claims interface, which can be built upon the integrated
43-
// claim types, such as map claims or registered claims.
44-
type customClaims interface {
45-
// CustomValidation can be implemented by a user-specific claim to support
46-
// additional validation steps in addition to the regular validation.
47-
CustomValidation() error
48-
}
49-
5042
// newValidator can be used to create a stand-alone validator with the supplied
5143
// options. This validator can then be used to validate already parsed claims.
5244
func newValidator(opts ...ParserOption) *validator {
@@ -102,10 +94,12 @@ func (v *validator) Validate(claims Claims) error {
10294
}
10395

10496
// Finally, we want to give the claim itself some possibility to do some
105-
// additional custom validation based on a custom function
106-
cvt, ok := claims.(customClaims)
97+
// additional custom validation based on a custom Validate function.
98+
cvt, ok := claims.(interface {
99+
Validate() error
100+
})
107101
if ok {
108-
if err := cvt.CustomValidation(); err != nil {
102+
if err := cvt.Validate(); err != nil {
109103
errs = append(errs, err)
110104
}
111105
}

0 commit comments

Comments
 (0)