我正在创建一个接受发布请求的函数。发布请求还包含一个转换为字符串的 JSON 对象。在后端,我需要验证此字符串是否确实可以解析为 JSON,然后才能继续。有路由功能:
var express = require('express');
var validate = require('express-validator');
var hotelController = require('../controllers/hotel-controller');
var hotelValidation = require('../validations/hotel-validation');
var router = express.Router();
router.route('/create')
.post(validate(hotelValidation.createHotel),
hotelController.create);
这是验证对象:
var Joi = require('joi');
module.exports = {
createHotel: {
body: {
//NEED TO CHECK IF THE BELOW FIELD
//CAN BE CONVERTED TO JSON FROM STRING
data: Joi.string().required()
}
}
};