fastify / fastify/help

Access request data from inside JSON schema or keyword validator

Open
#1,007 3 comments 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
No language data
Stars
68
Forks
8
Avg merge
11h 2m
Merged PRs (30d)
2

Description

## 💬 Question here

I'd like to use data from an earlier hook as part of the schema for a later route. Is this possible to do? For example:

```js
fastify.post(
'/',
{
schema: {
body: {
type: 'object',
properties: {
myCoolProp: { enum: req.arrayValueFromEarlierHook },
},
required: ['myCoolProp'],
},
},
},
async (req, res) => {
// bleh
},
);
````

I know I could also do the validation manually in the route handler itself, but it'd be nice to get the built-in error messaging, etc. by using a schema. I realize it's not possible to simply declare it the way I did in the example above since they're compiled on server start, but the example is just to get the use case across. I was looking at adding a custom keyword as well, but it doesn't appear the keyword validate function has access to the request object either. Is there a straightforward way to achieve this, or is this simply not possible?

## Your Environment

- *node version*: latest
- *fastify version*: latest
- *os*: Mac and Linux

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.