简体   繁体   English

使用javascript + tv4验证JSON方案

[英]Validating JSON scheme using javascript + tv4

I am trying to validate JSON scheme using TV4 . 我正在尝试使用TV4验证JSON方案。

My validation is using hierarchical JSON and is based on this basic example : 我的验证是使用分层JSON并基于这个基本示例

var data = {
    "foo": "bar"
};

var schema = {
    "type": "object",
    "properties": {
        "foo": {
            "type": "string"
        }
    },
    "required": ["foo"]
};

var result = tv4.validateResult(data, schema);

In my test I want to add one more hierarchy level: 在我的测试中,我想再添加一个层次结构级别:

 var data = {
        "foo": {
           "test": "bar"
        }
    };

    var schema = {
        "type": "object",
        "properties": {
            "foo": {
                    "test": {
                       "type": "string"
                     }
            }
        },
        "required": ["foo"]
    };

    var result = tv4.validateResult(data, schema);

This validation does not work (if I put an integer instead of a string it passes the validation ) 此验证不起作用(如果我放置一个整数而不是一个字符串,它通过验证

What am I doing wrong here? 我在这做错了什么?

Disclaimer: I have never used TV4 before. 免责声明:我之前从未使用过TV4。

I'd guess that the schema should specify the foo property as an object with a string property... Something like: 我猜测架构应该将foo属性指定为具有string属性的object ...类似于:

{
    "type": "object",
    "properties": {
        "foo": {
            "properties": {
                "test": {
                   "type": "string"
                 }
            },
            "type": "object"
        }
    },
    "required": ["foo"]
}

After a quick look at this forum question I figured out that I am missing the "properties" attribute for the sub tree. 在快速浏览一下这个论坛问题之后,我发现我错过了子树的“属性”属性。 Now it will work (when the value is an integer it will fail the validation. 现在它将起作用 (当值为整数时,它将无法通过验证。

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

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