Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 不允许使用父数组JOI npm库_Node.js_Json_Joi - Fatal编程技术网

Node.js 不允许使用父数组JOI npm库

Node.js 不允许使用父数组JOI npm库,node.js,json,joi,Node.js,Json,Joi,Hi-im使用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.s

Hi-im使用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添加新国家/地区时,会抛出错误: 不允许使用父数组


有什么想法吗?

他是个十足的错误追踪者

{
    "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"
            }
        ]
    }
}

解决后,我验证的是一个Country对象,而不是json主体。因此,解决办法是改变

var newCountry = new Country(req.body);

var newCountry = new Country(req.body);
var newCountry = req.body;