From 1e16f55059b4ef3ce614e7d5b22bc347cb253ef3 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Sun, 19 Feb 2023 19:56:30 +0100 Subject: [PATCH] Inlining application-specific validation --- example_test.go | 4 +++- validator.go | 16 +++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/example_test.go b/example_test.go index 650132a..58fdea4 100644 --- a/example_test.go +++ b/example_test.go @@ -121,7 +121,9 @@ type MyCustomClaims struct { jwt.RegisteredClaims } -func (m MyCustomClaims) CustomValidation() error { +// Validate can be used to execute additional application-specific claims +// validation. +func (m MyCustomClaims) Validate() error { if m.Foo != "bar" { return errors.New("must be foobar") } diff --git a/validator.go b/validator.go index 6097d7d..5146965 100644 --- a/validator.go +++ b/validator.go @@ -38,14 +38,6 @@ type validator struct { expectedSub string } -// customClaims represents a custom claims interface, which can be built upon the integrated -// claim types, such as map claims or registered claims. -type customClaims interface { - // CustomValidation can be implemented by a user-specific claim to support - // additional validation steps in addition to the regular validation. - CustomValidation() error -} - // newValidator can be used to create a stand-alone validator with the supplied // options. This validator can then be used to validate already parsed claims. func newValidator(opts ...ParserOption) *validator { @@ -105,10 +97,12 @@ func (v *validator) Validate(claims Claims) error { } // Finally, we want to give the claim itself some possibility to do some - // additional custom validation based on a custom function - cvt, ok := claims.(customClaims) + // additional custom validation based on a custom Validate function. + cvt, ok := claims.(interface { + Validate() error + }) if ok { - if err := cvt.CustomValidation(); err != nil { + if err := cvt.Validate(); err != nil { vErr.Inner = err vErr.Errors |= ValidationErrorClaimsInvalid }