Help - Configure setValidatorCompiler in Typescript
- Dominant language
- No language data
- Stars
- 68
- Forks
- 8
- Avg merge
- 11h 2m
- Merged PRs (30d)
- 2
Description
## 💬 Question here
Following [docs](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#:~:text=For%20example%2C%20the%20following%20code%20disable%20type%20coercion%20only%20for%20the%20body%20parameters%2C%20changing%20the%20ajv%20default%20options%3A), I'm trying to setValidatorCompiler like
```typescript
const schemaCompilers: Record = {
body: new Ajv({
removeAdditional: 'all',
useDefaults: true,
coerceTypes: false,
addUsedSchema: false,
allErrors: false,
}),
others: new Ajv({
removeAdditional: 'all',
useDefaults: true,
coerceTypes: 'array',
addUsedSchema: false,
allErrors: false,
}),
};
server.setValidatorCompiler((req) => {
if (!req.httpPart) {
throw new Error('Missing httpPart');
}
const compiler = schemaCompilers[req.httpPart] || schemaCompilers['others'];
if (!compiler) {
throw new Error(`Missing compiler for ${req.httpPart}`);
}
return compiler.compile(req.schema);
});
````
But I'm getting the below typescript error
```typescript
Argument of type '(req: FastifyRouteSchemaDef) => ValidateFunction' is not assignable to parameter of type 'FastifySchemaCompiler'.
Call signature return types 'ValidateFunction' and 'FastifyValidationResult' are incompatible.
The types of 'errors' are incompatible between these types.
Type 'ErrorObject[] | null | undefined' is not assignable to type 'FastifySchemaValidationError[] | null | undefined'.
Type 'ErrorObject[]' is not assignable to type 'FastifySchemaValidationError[]'.
Property 'instancePath' is missing in type 'ErrorObject' but required in type 'FastifySchemaValidationError'.
```
How do I setup different compilers for schema validation?
PS: I don't want to coerce the body json, which is why I'm trying to setup different compilers.
## Your Environment
- *node version*: v22.10.0
- *fastify version*: 5.0.0
- *typescript*: 5.6.3
- *os*: Mac
Contributor guide
Assessment
This issue has not been assessed yet.