简体   繁体   English

Postman 中的嵌套 JSON 模式验证

[英]Nested JSON Schema Validation in Postman

I want to validate a Nested JSON Schema in Postman .我想验证Postman 中嵌套 JSON 模式

Here is the code.这是代码。

const testSchema = {
    "name": [
        {
            "first_name": "Alpha",
            "last_name": "Bravo"
        },
        {
            "first_name": "Charlie",
            "last_name": "Delta"
        },
    ],
    "age": "23",
    "color": "black"
};

const showData = { 
    "required": ["name", "age"],
    "properties": {
        "name": [
            {
                "required": ["first_name"]
            }
        ],
    },
};

pm.test("Nested Schema Test", function () {
    pm.expect(tv4.validate(testSchema, showData)).to.be.true;
});

Currently, this code returns test as true .目前,此代码返回 test 为true

I am unable to test the "name" array objects' keys .我无法测试“名称”数组对象的键

Even upon passing this:即使通过了这个:

"required": ["fst_nae"] //wrong key name

it returns true.它返回真。

I would just check in easy way via:我会通过以下简单的方式办理登机手续:

pm.test("your name", function () {
   pm.expect(testSchema.name[0].first_name && testSchema.name[1].first_name
   ).to.eql('Alpha' && 'Charlie') 
});

and you successfully validated these fields or use this expect to organize your code of your choice并且您成功验证了这些字段或使用它来组织您选择的代码

tiny validator ie tv4.validate is having issues in their library.微型验证器,即tv4.validate在他们的库中存在问题。 Another option is to use AJV (you can search it on github).另一种选择是使用AJV (你可以在 github 上搜索它)。

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

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