AndrewKeig/express-validation

Joi.boolean().default(true) leaves unspecified field undefined

yonirab opened this issue · 5 comments

Thank you very much for this extremely useful package!

Either I am missing something, or Joi.boolean().default(true) doesn't seem to be working properly.
When a request is sent with an unspecified field, the validation is not filling in the default value.

Reproduction steps:

  1. Run the following code:
const express = require('express')
const bodyParser = require('body-parser')
const { validate, ValidationError, Joi } = require('express-validation')

const loginValidation = {
  body: Joi.object({
     text: Joi.string(),
     mybool: Joi.boolean().default(true)
  }),
}

const app = express();
app.use(bodyParser.json())

app.post('/test', validate(loginValidation, {}, {}), (req, res) => {
  console.log(JSON.stringify(req.body));
  res.json(200)
})

app.use(function(err, req, res, next) {
  if (err instanceof ValidationError) {
    return res.status(err.statusCode).json(err)
  }

  return res.status(500).json(err)
})

app.listen(3000)
  1. Send a request with:
curl -X POST "http://localhost:3000/test" -H "Content-Type: application/json" -d "{ \"text\": \"hello\"  }"

Actual result: {"text":"hello"} printed in terminal
Expected result: {"text":"hello","mybool":true} printed in terminal

I believe this is not only for .default in boolean. I tried for string, it also become undefined

Thank you very much for this extremely useful package!

Either I am missing something, or Joi.boolean().default(true) doesn't seem to be working properly. When a request is sent with an unspecified field, the validation is not filling in the default value.

Reproduction steps:

  1. Run the following code:
const express = require('express')
const bodyParser = require('body-parser')
const { validate, ValidationError, Joi } = require('express-validation')

const loginValidation = {
  body: Joi.object({
     text: Joi.string(),
     mybool: Joi.boolean().default(true)
  }),
}

const app = express();
app.use(bodyParser.json())

app.post('/test', validate(loginValidation, {}, {}), (req, res) => {
  console.log(JSON.stringify(req.body));
  res.json(200)
})

app.use(function(err, req, res, next) {
  if (err instanceof ValidationError) {
    return res.status(err.statusCode).json(err)
  }

  return res.status(500).json(err)
})

app.listen(3000)
  1. Send a request with:
curl -X POST "http://localhost:3000/test" -H "Content-Type: application/json" -d "{ \"text\": \"hello\"  }"

Actual result: {"text":"hello"} printed in terminal Expected result: {"text":"hello","mybool":true} printed in terminal

Hi, You will define a option "context: true" in express-validation, in the second param from function "validate", add this param such as:

app.post('/test', validate(loginValidation, {context: true}, {}), (req, res) => {
  console.log(JSON.stringify(req.body));
  res.json(200)
})

Hello @AndrewKeig default() is not working for any type, can you please check!!

Hello @AndrewKeig default() is not working for any type, can you please check!!

Hi, you must define option "context: true"

I tried that but, I am passing that into the JoiRoot that is why I am not getting any results but after that, I realized there is also one option coming from your package and I passed there, and booooom, it's working.

Thanks :)