Skip to content
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

Closed
2 tasks done
budarin opened this issue Sep 15, 2022 · 5 comments
Labels
good first issue Good for newcomers typescript TypeScript related

Comments

@budarin
Copy link
Contributor

budarin commented Sep 15, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

馃殌 Feature Proposal

I suggest adding to request.ValidateInput, as well as the functions returned by compilerValidationSchema and getValidationFunction, the errors property after performing the validation, as does ajv:

const validate = ajv.compile({
  type: 'object', 
  properties: { 
    foo: { 
      type: 'string' 
    } 
  } 
})

validate({ foo: 1 }) // false
validate.errors  // validation errors

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:

const validate = request.getValidationFunction({
  type: 'object', 
  properties: { 
    foo: { 
      type: 'string' 
    } 
  } 
})

validate.errors // undefined
validate({ foo: 0.5 }) // false
validate.errors // validation errors 

validate({ foo: 'bar' }) // true
validate.errors // undefined

for compileValidationSchema:

const validate = request.compileValidationSchema({
  type: 'object', 
  properties: { 
    foo: { 
      type: 'string' 
    } 
  } 
})

validate.errors // undefined
validate({ hello: 'world' }) // false
validate.errors // validation errors

for request.validateInput:

request.validateInput.errors // undefined
request.validateInput({ foo: 'bar'}, {  
  type: 'object', 
  properties: { 
    foo: { 
      type: 'string' 
    } 
  } 
}) // true
request.validateInput.errors // undefined

request.validateInput({ hello: 'world'}, 'query') // false
request.validateInput.errors //validation errors
@budarin budarin changed the title Is it possible for the request function.validateInput add the errors property by analogy as ajv.compile does? Is it possible for the request.validateInput (and other such funs) add the errors property by analogy as ajv.compile does? Sep 15, 2022
@budarin budarin changed the title Is it possible for the request.validateInput (and other such funs) add the errors property by analogy as ajv.compile does? Is it possible for the request.validateInput (and other such funcs) add the errors property by analogy as ajv.compile does? Sep 15, 2022
@budarin budarin changed the title Is it possible for the request.validateInput (and other such funcs) add the errors property by analogy as ajv.compile does? Is it possible for the request.validateInput (and other such funcs) add the errors property by analogy as ajv does? Sep 15, 2022
@metcoder95
Copy link
Member

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'))

and then prints:
image

@budarin
Copy link
Contributor Author

budarin commented Sep 16, 2022

To be honest, I haven't tried with compilerValidationSchema and getValidationFunction - I checked with request.ValidateInput and in this case there are definitely no errors, which is why I assumed that other methods have the same situation

@budarin
Copy link
Contributor Author

budarin commented Sep 16, 2022

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

@climba03003
Copy link
Member

climba03003 commented Sep 16, 2022

validateInput shouldn't have the errors property and never should be there.
It will mess up with multiple usage of same function.

If the problem is only incorrect typings. Would you like to send a PR to address the issue?

@climba03003 climba03003 added good first issue Good for newcomers typescript TypeScript related labels Sep 16, 2022
@budarin
Copy link
Contributor Author

budarin commented Sep 16, 2022

here is the PR #4283

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers typescript TypeScript related
Projects
None yet
Development

No branches or pull requests

3 participants