Replies: 1 comment
-
|
The behavior you’re seeing is expected: z.templateLiteral(['Bearer ', z.jwt()]) does not enforce validation of the inner z.jwt() component beyond string concatenation. Template literal schemas in Zod (v4) concatenate the internal regexes of their components, but for string formats like z.jwt(), only the underlying regular expression is enforced—custom validation logic (such as decoding and structural checks for JWTs) is not applied inside template literals(1). As a result, z.templateLiteral(['Bearer ', z.jwt()]) only checks that the string matches the regex for a JWT after the "Bearer " prefix, but it does not actually parse or validate the JWT structure. This is why 'Bearer not-a-jwt' passes: the regex is not sufficient to catch all invalid JWTs(1). Your workaround using z.codec and explicit extraction/validation is required for full JWT validation in the authorization header. This approach ensures the JWT parser runs on the token portion, not just a regex match(1). 📚 Sources: Have another question? Just tag @inkeep. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Now that zod supports template literals, I tried
However, zod doesn't validate the
jwtpartA workaround is to write
I assumed the original template literal version would work, is there something I'm missing about using
z.templateLiteral?Beta Was this translation helpful? Give feedback.
All reactions