Apbln
Apbln

Reputation: 177

Configure JSON response

I created a schema with Joi and want a JSON response (when POSTing) that matches my schema. Problem is: I have to give it a parent(in my example "data:" (I know, I can name it how I want) which I don't want to. The schema's attributes should stand alone inside the JSON. But without giving it a parent, it (Object.assign(value)) doesn't work. Any ideas?

.post((req,res,next) => {
  let data = req.body;
  Joi.validate(data, schema, (err, value) => {

    res.json({
              data: Object.assign(value)
            });
  });

})

expected:

{
    "title": "dasdawdasfasd",
    "textshort": "wasser",
    "textlong": "",
    "imgwidth": null,
    "imgheight": null,
    "imgsrc": "",
    "views": 0,
    "keywords": []
}

actual:

{
  "data": {
    "title": "dasdawdasfasd",
    "textshort": "wasser",
    "textlong": "",
    "imgwidth": null,
    "imgheight": null,
    "imgsrc": "",
    "views": 0,
    "keywords": []
  }
}

Upvotes: 0

Views: 57

Answers (1)

Ahmed Ghoniem
Ahmed Ghoniem

Reputation: 681

Just return data

.post((req,res,next) => {
  Joi.validate(data, schema, (err, value) => {
       res.json(value);
  });

 })

Upvotes: 1

Related Questions