koajs / joi-router

Configurable, input and output validated routing for koa
MIT License
450 stars 96 forks source link

Body validator doesn't work as well #90

Closed toto6321 closed 5 years ago

toto6321 commented 5 years ago

Again, another weird problem.

const Koa = require('koa')
const Router = require('koa-joi-router')

const Joi = Router.Joi

let router = new Router()

router.route({
  path: '/user/login',
  method: 'post',
  validate: {
    body: {
      email: Joi.string().email(),
      password: Joi.string()
    },
    type: 'json',
  },
  handler: async ctx => {
    ctx.body = 'success'
  }
})
const route_prefix = '/api/v1'
router.prefix(route_prefix)
app.use(router.middleware())

To be sure, I also print the router to debug:

Router {
  routes: 
   [ { path: '/user/login',
       method: [Array],
       validate: [Object],
       handler: [Array] } ],
  router: 
   Router {
     opts: { prefix: '/api/v1' },
     methods: [ 'HEAD', 'OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE' ],
     params: {},
     stack: [ [Object] ]
 }
 }

then I got

image

pretty upset...

toto6321 commented 5 years ago

validator need to append .required() ..