AJV 6.10.0 ReferenceError:验证时未定义refVal3

时间:2019-03-19 15:41:49

标签: node.js json ajv

我在使用AJV 6.10.0进行自递归json验证时遇到问题。 我尝试从上面验证示例,并且该示例必须是通用代码,以便有效地从Messenger api更新。现在,我可以使其工作了,但是我的解决方案需要复制相同的代码,因此效果不佳。 如果验证较深,则1或2个循环就可以了,但是最多2个会产生错误:

ReferenceError: refVal3 is not defined at validate

我的代码如下:

const persistentMenu = {
    type: "object",
    properties: {
        locale: {
            type: "string"
        },
        composer_input_disabled: {
            type: "boolean"
        },
        call_to_actions: {
            type: "array",
            items: {
                oneOf: [{
                        $ref: "#definitions/postback"
                    },
                    {
                        $ref: "#definitions/web_url"
                    },
                    {
                        $ref: "#definitions/call_to_actions"
                    }
                ]
            }
        }
    },
    required: ["locale", "composer_input_disabled", "call_to_actions"],
    definitions: {
        definitions: {
            postback: {
                $id: "#definitions/postback",
                type: "object",
                properties: {
                    type: {
                        type: "string",
                        const: "postback"
                    },
                    title: {
                        type: "string"
                    },
                    payload: {
                        type: "string"
                    }
                },
                required: ["type", "title", "payload"]
            },
            call_to_actions: {
                $id: "#definitions/call_to_actions",
                type: "object",
                properties: {
                    type: {
                        type: "string",
                        const: "nested"
                    },
                    title: {
                        type: "string"
                    },
                    call_to_actions: {
                        type: "array",
                        items: {
                            oneOf: [{
                                    $ref: "#definitions/postback"
                                },
                                {
                                    $ref: "#definitions/web_url"
                                },
                                {
                                    $ref: "#definitions/call_to_actions"
                                }
                            ]
                        }
                    }
                },
                required: ["type", "title", "call_to_actions"]
            },
            web_url: {
                $id: "#definitions/web_url",
                type: "object",
                properties: {
                    type: {
                        type: "string",
                        const: "web_url"
                    },
                    title: {
                        type: "string"
                    },
                    url: {
                        type: "string"
                    }
                },
                required: ["type", "title", "url"]
            }
        }
    }
};

module.exports = {
    type: "object",
    required: ["persistent_menu"],
    properties: {
        persistent_menu: {
            type: "array",
            items: persistentMenu
        }
    }
};

和示例数据:

{
    "persistent_menu": [
        {
            "locale": "default",
            "composer_input_disabled": false,
            "call_to_actions": [
                {
                    "type": "postback",
                    "title": "Nested Item One",
                    "payload": "q",
                    "url": "testo"
                },
                {
                    "type": "web_url",
                    "title": "Nested Item One",
                    "payload": "d",
                    "url": "testo"
                },
                {
                    "type": "nested",
                    "title": "Nested Item One",
                    "payload": "a",
                    "url": "testo",
                    "call_to_actions": [
                        {
                            "type": "postback",
                            "title": "Nested Item One",
                            "payload": "a",
                            "url": "testo"
                        },
                        {
                            "type": "web_url",
                            "title": "Nested Item One",
                            "payload": "a",
                            "url": "testo"
                        },
                        {
                            "type": "postback",
                            "title": "Nested Item One",
                            "payload": "s"
                        },
                        {
                            "type": "nested",
                            "title": "Nested Item One",
                            "payload": "s",
                            "url": "testo",
                            "call_to_actions": [
                                {
                                    "type": "postback",
                                    "title": "Nested Item One",
                                    "payload": "s",
                                    "url": "testo"
                                },
                                {
                                    "type": "web_url",
                                    "title": "Nested Item One",
                                    "payload": "s",
                                    "url": "testo"
                                },
                                {
                                    "type": "postback",
                                    "title": "Nested Item One",
                                    "payload": "a"
                                },
                                        {
                                            "type": "postback",
                                            "title": "Nested Item One",
                                            "payload": "a",
                                            "url": "testo"
                                        },
                                {
                                    "type": "nested",
                                    "title": "Nested Item One",
                                    "payload": "j",
                                    "url": "testo",
                                    "call_to_actions": [
                                        {
                                            "type": "postback",
                                            "title": "Nested Item One",
                                            "payload": "a",
                                            "url": "testo"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
};

和验证者代码:

const ajv = new Ajv({verbose: true, multipleOfPrecision: 12,  extendRefs: true});

0 个答案:

没有答案