feat: add validateDecoded
option for JWT payload validation
#377
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.
This PR introduces a new
validateDecoded
option to the@fastify/jwt
plugin.It allows developers to define custom validation logic that runs after the JWT is decoded and verified, but before assigning
request.user
.This enables scenarios where claims alone are insufficient, such as:
Checking flags (e.g.,
isVerified
)Validating roles or permissions
Applying business rules or JSON Schema validation
Performing async checks (e.g., database lookups)
Example: synchronous usage
Example: asynchronous usage
Implementation notes
This change includes the following:
Adds
validateDecoded(payload)
support to plugin optionsExecuted after token verification in
request.jwtVerify()
If validation fails, responds with 400 Bad Request
Includes test coverage for both sync and async cases
Type definitions updated (
types/jwt.d.ts
)Documentation updated in
README.md
Related
Checklist
npm run test
andnpm run benchmark
and the Code of conduct