1

I'm using koa-joi-validator in KOA js.

JOI validation works properly for GET method. But when POST method triggers it just receives the request and that's it, No validation triggered, No response sent.

GET

<-- GET /user/1
--> GET /user/1 200 107ms 4b

POST

<-- POST /create

If i remove JOI validation from POST request, then the route works fine.

app.js

var bodyparser = require('koa-bodyparser')

app.use(bodyparser())

routes.js

var validator = require('koa-joi-validator')

route.get('/user/:userid', validator.validate({
        type: 'json',
        params: { userid: validator.Joi.string().required() }
    }, function *() { this.body = 'test' })

route.post('/create', validator.validate({
        type: 'json',
        body: { username: validator.Joi.string().required() }
    }), function *() { this.body = 'test' })
sravis
  • 3,562
  • 6
  • 36
  • 73
  • I just copy-pasted your code on a very basic koa app and it just work. If I POST without content it returns status code 400 and this body message: child "username" fails because ["username" is required]. POSTing with { "username": "John" } returns status code 200 and body: test. Do you have an `app.on('error', ..)` to capture possible errors? – Diego Fernandez Oct 07 '16 at 17:17
  • 1
    In case you are using body-parser middleware that might be what causing your code to break – dimitrisk Aug 25 '17 at 21:15

0 Answers0