New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible for the request.validateInput (and other such funcs) add the errors property by analogy as ajv does? #4281
Comments
Hey! That should be already possible as the new APIs don't mess with the Ajv or any other validation function compiled, meaning that you can still access such errors and result properties. Here's an example: const fastify = require('fastify')
const app = fastify({ logger: true })
app.post(
'/',
{
schema: {
body: {
type: 'object',
required: ['name'],
properties: {
name: { type: 'string' }
}
}
}
},
(request, reply) => {
const customCompiled = request.compileValidationSchema({
type: 'object',
required: ['something'],
properties: {
name: { type: 'string' },
something: { type: 'string' }
}
})
const routeValidation = request.getValidationFunction('body')
const result = routeValidation({ something: 'something' })
const customResult = customCompiled({ name: 'John' })
// console.log(routeValidation)
console.log(routeValidation.errors)
console.log(customCompiled.errors)
reply.send({ hello: 'world' })
}
)
app.inject({
method: 'POST',
url: '/',
payload: {
name: 'John'
}
}).then(() => console.log('done')) |
To be honest, I haven't tried with |
the description of the types of these functions also reflects the fact that functions do not have this property getValidationFunction(httpPart: HTTPRequestPart): (input: any) => boolean
getValidationFunction(schema: {[key: string]: any}): (input: any) => boolean
compileValidationSchema(schema: {[key: string]: any}, httpPart?: HTTPRequestPart): (input: any) => boolean
validateInput(input: any, schema: {[key: string]: any}, httpPart?: HTTPRequestPart): boolean
validateInput(input: any, httpPart?: HTTPRequestPart): boolean |
If the problem is only incorrect typings. Would you like to send a PR to address the issue? |
here is the PR #4283 |
Prerequisites
馃殌 Feature Proposal
I suggest adding to
request.ValidateInput
, as well as the functions returned bycompilerValidationSchema
andgetValidationFunction
, theerrors
property after performing the validation, as doesajv
:Motivation
It is important for us to receive not only the result of the check, but also errors of data inconsistency with the scheme
Example
for getValidationFunction:
for compileValidationSchema:
for request.validateInput:
The text was updated successfully, but these errors were encountered: