【问题标题】:Parent array is not allowed JOI npm library父数组不允许 JOI npm 库
【发布时间】:2020-02-19 00:32:14
【问题描述】:

您好,我使用 Joi 在一个简单的 API 上验证 JSON 对象,我的验证模式是:

let holidays = Joi.object().keys({
    day: Joi.number().required(),
    month: Joi.number().required(),
    description: Joi.string().required()
})

const countryValidateSchema = Joi.object().keys({
    name: Joi.string().required(),
    holidays: Joi.array().items(holidays),
    states: Joi.array().required().items(Joi.string())
});

当添加一个新国家时,我会这样验证它:

function newCountry(req, res) {
    //TODO validate
    var newCountry = new Country(req.body);

    const result = Joi.validate(newCountry, validate.countryValidateSchema);
    const { value, error } = result; 
    const valid = error == null; 

    if (!valid) { 
      res.status(422).json({ 
        message: result, 
        data: newCountry 
      }) 
    } else { 
        //Store on DB
        newCountry.save((err,country) =>{
            if(err){
                res.send(err);
            }else{
                res.status(201).json({message: "Country successfully added!", country})
            }
        });
    }
}

当我尝试使用此 JSON 添加新国家/地区时,它会引发错误: ParentArray 不允许

{
    "name": "TestCountry",
    "holidays": [
        {
            "day": 1,
            "month": 2,
            "description": "test"
        }
    ],
    "states": [
         "test1",
         "test2",
         "test3",
         "test34"
        ]
}

有什么想法吗?

【问题讨论】:

    标签: node.js json joi


    【解决方案1】:

    已解决,我正在验证 Country 对象而不是 json 正文。所以解决方法是改变

    var newCountry = new Country(req.body);
    

    var newCountry = req.body;
    

    【讨论】:

      【解决方案2】:

      他是完整的错误轨迹

      {
          "message": {
              "error": {
                  "isJoi": true,
                  "name": "ValidationError",
                  "details": [
                      {
                          "message": "\"__parentArray\" is not allowed",
                          "path": [
                              "holidays",
                              0,
                              "__parentArray"
                          ],
                          "type": "object.allowUnknown",
                          "context": {
                              "child": "__parentArray",
                              "value": [
                                  {
                                      "_id": "5daf5e657f19e312de414613",
                                      "day": 1,
                                      "month": 2,
                                      "description": "test"
                                  }
                              ],
                              "key": "__parentArray",
                              "label": "__parentArray"
                          }
                      },
                      {
                          "message": "\"__index\" is not allowed",
                          "path": [
                              "holidays",
                              0,
                              "__index"
                          ],
                          "type": "object.allowUnknown",
                          "context": {
                              "child": "__index",
                              "value": 0,
                              "key": "__index",
                              "label": "__index"
                          }
                      },
                      {
                          "message": "\"$isDocumentArrayElement\" is not allowed",
                          "path": [
                              "holidays",
                              0,
                              "$isDocumentArrayElement"
                          ],
                          "type": "object.allowUnknown",
                          "context": {
                              "child": "$isDocumentArrayElement",
                              "value": true,
                              "key": "$isDocumentArrayElement",
                              "label": "$isDocumentArrayElement"
                          }
                      },
                      {
                          "message": "\"$__\" is not allowed",
                          "path": [
                              "holidays",
                              0,
                              "$__"
                          ],
                          "type": "object.allowUnknown",
                          "context": {
                              "child": "$__",
                              "value": {
                                  "strictMode": true,
                                  "getters": {},
                                  "_id": "5daf5e657f19e312de414613",
                                  "wasPopulated": false,
                                  "activePaths": {
                                      "paths": {
                                          "description": "modify",
                                          "month": "modify",
                                          "day": "modify",
                                          "_id": "default"
                                      },
                                      "states": {
                                          "ignore": {},
                                          "default": {
                                              "_id": true
                                          },
                                          "init": {},
                                          "modify": {
                                              "day": true,
                                              "month": true,
                                              "description": true
                                          },
                                          "require": {}
                                      },
                                      "stateNames": [
                                          "require",
                                          "modify",
                                          "init",
                                          "default",
                                          "ignore"
                                      ]
                                  },
                                  "pathsToScopes": {},
                                  "cachedRequired": {},
                                  "session": null,
                                  "$setCalled": {},
                                  "ownerDocument": {
                                      "states": [
                                          "test1",
                                          "test2",
                                          "test3",
                                          "test34"
                                      ],
                                      "_id": "5daf5e657f19e312de414612",
                                      "name": "TestCountry",
                                      "holidays": [
                                          {
                                              "_id": "5daf5e657f19e312de414613",
                                              "day": 1,
                                              "month": 2,
                                              "description": "test"
                                          }
                                      ]
                                  },
                                  "fullPath": "holidays",
                                  "emitter": {
                                      "_events": {},
                                      "_eventsCount": 2,
                                      "_maxListeners": 0
                                  },
                                  "$options": {}
                              },
                              "key": "$__",
                              "label": "$__"
                          }
                      },
                      {
                          "message": "\"isNew\" is not allowed",
                          "path": [
                              "holidays",
                              0,
                              "isNew"
                          ],
                          "type": "object.allowUnknown",
                          "context": {
                              "child": "isNew",
                              "value": true,
                              "key": "isNew",
                              "label": "isNew"
                          }
                      },
                      {
                          "message": "\"errors\" is not allowed",
                          "path": [
                              "holidays",
                              0,
                              "errors"
                          ],
                          "type": "object.allowUnknown",
                          "context": {
                              "child": "errors",
                              "key": "errors",
                              "label": "errors"
                          }
                      },
                      {
                          "message": "\"_doc\" is not allowed",
                          "path": [
                              "holidays",
                              0,
                              "_doc"
                          ],
                          "type": "object.allowUnknown",
                          "context": {
                              "child": "_doc",
                              "value": {
                                  "_id": "5daf5e657f19e312de414613",
                                  "day": 1,
                                  "month": 2,
                                  "description": "test"
                              },
                              "key": "_doc",
                              "label": "_doc"
                          }
                      },
                      {
                          "message": "\"$locals\" is not allowed",
                          "path": [
                              "holidays",
                              0,
                              "$locals"
                          ],
                          "type": "object.allowUnknown",
                          "context": {
                              "child": "$locals",
                              "value": {},
                              "key": "$locals",
                              "label": "$locals"
                          }
                      }
                  ],
                  "_object": {
                      "states": [
                          "test1",
                          "test2",
                          "test3",
                          "test34"
                      ],
                      "_id": "5daf5e657f19e312de414612",
                      "name": "TestCountry",
                      "holidays": [
                          {
                              "_id": "5daf5e657f19e312de414613",
                              "day": 1,
                              "month": 2,
                              "description": "test"
                          }
                      ]
                  }
              },
              "value": {
                  "states": [
                      "test1",
                      "test2",
                      "test3",
                      "test34"
                  ],
                  "_id": "5daf5e657f19e312de414612",
                  "name": "TestCountry",
                  "holidays": [
                      {
                          "_id": "5daf5e657f19e312de414613",
                          "day": 1,
                          "month": 2,
                          "description": "test"
                      }
                  ]
              }
          },
          "data": {
              "states": [
                  "test1",
                  "test2",
                  "test3",
                  "test34"
              ],
              "_id": "5daf5e657f19e312de414612",
              "name": "TestCountry",
              "holidays": [
                  {
                      "_id": "5daf5e657f19e312de414613",
                      "day": 1,
                      "month": 2,
                      "description": "test"
                  }
              ]
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-05-19
        • 2018-01-12
        • 2019-06-01
        • 2021-07-06
        • 1970-01-01
        • 2020-12-03
        • 1970-01-01
        • 2021-08-01
        • 2017-11-03
        相关资源
        最近更新 更多