简体   繁体   English

AJV - 复杂模式的验证

[英]AJV - Validation of complex schema

I am using AJV as my schema validator for API inputs.我使用AJV作为 API 输入的模式验证器。

I need to validate if I received data in this format:我需要验证我是否收到了这种格式的数据:

floors: [
    {
        name: "Floor 1",
        rooms: [
            {
                name: "Room 1"
            },
            ... more rooms
        ]
    },
    ... more floors
]

Basically it is Array of Objects and those Objects also have Array of Objects .基本上它是Array of Objects并且这些 Objects 也有Array of Objects Each floor has one or more rooms.每层都有一个或多个房间。

Is it possible to validate this data with a clean JSON Schema using AJV or do I need to write some custom keyword/validator for AJV?是否可以使用 AJV 使用干净的 JSON Schema 验证这些数据,还是我需要为 AJV 编写一些自定义关键字/验证器?

I tried to do it but I ended up with this, but it doesn't seem to work:我试图这样做,但我最终得到了这个,但它似乎不起作用:

floors: {
    type: "object",
    minProperties: 1,
    properties: {
        name: {
            type: "string"
        },
        rooms: {
            type: "object",
            minProperties: 1,
            properties: {
                name: {
                    type: "string"
                }
            },
            required: ["name"]
        }
    },
    required: ["name", "rooms"]
}

I think I found solution myself:我想我自己找到了解决方案:

floors: {
    type: "array",
    minItems: 1,
    items: [
        {
            type: "object",
            properties: {
                name: {
                    type: "string"
                },
                rooms: {
                    type: "array",
                    minItems: 1,
                    items: [
                        {
                            type: "object",
                            properties: {
                                name: {
                                    type: "string"
                                }
                            },
                            required: ["name"],
                            additionalProperties: false,
                        }
                    ],
                }
            },
            required: ["name", "rooms"],
            additionalProperties: false,
        }
    ]
}

Correct me if it have there some security holes where you can leak some invalid stuff:)如果它有一些安全漏洞,你可以泄露一些无效的东西,请纠正我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM