jsonschema-master验证json请求未按预期响应

时间:2018-01-10 09:40:53

标签: javascript json node.js express jsonschema

我正在尝试使用jsonschema-master来验证通过POST请求使用express输入的json请求。请参阅下面的代码和示例。

如果属性标签丢失或拼写错误,它会选中,例如“model”,“areas”,“id”,但如果这些属性的值符合规范,则不会拾取。例如,“model”属性被定义为枚举类型“premium”或“basic”,但我似乎能够在其中放置任何旧字符串并且无论如何都会打开,也将坐标定义为类型编号,但它又忽略了这一点,然后错误传递给验证器,并进一步导致问题。不确定我错过了什么。

node.js代码:

var Validator = require('jsonschema-master').Validator;
var v = new Validator();
var bodySchema = {
      "model": { 
        "enum": [ "premium","basic" ] 
      },
      "areas": {
        "type":"array",
        "items": {
          "id": {"type": "string"},
          "geometry": {
            "type": { "type":"string"},
            "coordinates": {
               "type":"array",
               "items": {
                 "type":"array",
                 "items": [
                   {"type":"number"},
                   {"type":"number"},
                   {"type":"number"}
                 ]
               }
            },
            "required" : ["type","coordinates"]
          },
          "required" : ["id","geometry"]
        }
      },
      "required" : ["model","areas"]
};
var valResult = v.validate(doc.request, bodySchema);
if (valResult.errors.length) {
  // Validation failed.
  // All processing will now stop.
  console.log('Request invalid: '+ doc._id +" - "+valResult.errors);
}

SAMPLE CORRECT JSON请求(在doc.request中)

{
  "model": "premium",
  "areas": [
    {
      "id": "1234",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              453600.0,
              181100.0,
              0
            ],
            [
              453600.0,
              181200.0,
              0
            ],
            [
              453700.0,
              181200.0,
              0
            ],
            [
              453700.0,
              181100.0,
              0
            ],
            [
              453600.0,
              181100.0,
              0
            ]
          ]
        ]
      }
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

谢谢大家,但我最终还是把它解决了。以下是架构的更正语法:

var bodySchema = {
    "type" : "object",
    "properties": {
        "model": { 
            "enum": [ "premium","basic" ] 
        },
        "areas": {
            "type":"array",
            "items": {
                "type" : "object",
                "properties": {
                    "id": {"type": "string"},
                    "geometry": {
                        "type" : "object",
                        "properties": {
                            "type": { "type":"string"},
                            "coordinates": {
                                "type":"array",
                                "items": {
                                    "type":"array",
                                    "items": {
                                        "type":"array",
                                        "items": [
                                            {"type":"number"},
                                            {"type":"number"},
                                            {"type":"number"}
                                        ]
                                    }
                                }
                            }
                        },
                        "required" : ["type","coordinates"]
                    }
                },
                "required" : ["id","geometry"]
            }
        }
    },
    "required" : ["model","areas"]
};