Can you give an example of Sanitizing Validator on Route


#1

Please help me understand how sanitation works on routes with an example. thank you in advance :grin:


#2

You have example in docs:

https://adonisjs.com/docs/4.1/validator#_route_validator

To make route validator also sanitize, read this:
https://adonisjs.com/docs/4.1/validator#_sanitizing_user_input


#3

This my route

Route.get('/number', 'TestConstroller.number').validator('Test')

This my validator

class Test {
  get validateAll () {
    return true
  }
  get sanitizationRules () {
    return {
      value: 'to_int'
    }
  }
  get rules () {
    return {
      value: 'required|number'
    }
  }  
  async fails (errorMessages) {
    return this.ctx.response.send(errorMessages)
  }
}

This my controller

number({ request, response }) {
        return response.send({
            value: request.input('value'),
            type: (typeof request.input('value')
        })
    }

But i get error

   {
        "message": "number validation failed on value",
        "field": "value",
        "validation": "number"
    }

Why I don’t understand? :thinking::thinking:


#4

Hey @KemBeka! :wave:

What’s the value of value?


#5

The value, I use to send data through the request, with postman.


#6

Of course :smiley:

But could you give us this value here?


#7

I apologize for my fault. I’m understand how work sanitization . It turns out that sanitization does not work without representations.
Thank you all for your time. :joy::joy:


#8

me interested in can be whether use sanitation without views?


#9

Of course you can use sanitisation without view.

Let me rephrase my question.

Could you give us the request you are sending to your application?


#10

I apologize for my fault. I figured it out. It was necessary to send through the body and not through the parameters. :rofl: