即使缺少必填字段,Rapidjson也可以使验证成功

时间:2018-11-21 14:06:41

标签: jsonschema rapidjson

我期望rapidjson给出验证错误,因为我的json文件不包含架构中提到的“ required”字段之一。但是,由于某些原因,这种情况不会发生。

dbconf.json(json文件)

with toupdate as (
      select t2.*,
             row_number() over (partition by accname order by id) as seqnum
      from table2 t2
     )
update toupdate
    set CustID = t1.ID
    from toupdate join
         (select t1.*,
                 row_number() over (partition by accname order by id) as seqnum
          from table1 t1
         ) t1
         on toupdate.accnum = t1.accnum and toupdate.seqnum = t1.seqnum;

这是测试代码以及​​架构。

{
        "MAX_CONNECTION_PER_HOST":20,
        "QUEUE_IO_SIZE":10485,
        "Garbage":50000
}

我得到以下输出

#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "rapidjson/schema.h"
#include <rapidjson/stringbuffer.h>
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

const char g_plJsonSchema[]="{\
                             \"$schema\": \"http://json-schema.org/draft-04/schema#\",\
                             \"title\": \"Schema\",\
                             \"description\": \"JSON schema for validating Json file\",\
                             \"type\": \"object\",\
                             \"properties\": {\
                             \"MAX_CONNECTION_PER_HOST\": { \"type\": \"number\" },\
                              \"QUEUE_IO_SIZE\": { \"type\": \"number\" },\
                              \"REQUEST_LOW_WATER_MARK\": { \"type\": \"number\" },\
\"required\": [\
\"MAX_CONNECTION_PER_HOST\",\
\"QUEUE_IO_SIZE\",\
\"REQUEST_LOW_WATER_MARK\"\
]\
      }\
}";

int main()

{
        rapidjson::Document l_peerAddSchemaDoc, l_peerAddDataDoc;
        l_peerAddSchemaDoc.Parse(g_plJsonSchema);
        if(l_peerAddSchemaDoc.HasParseError())
        {
                printf("JSON schema file is not a valid JSON file\n");
                return -1;
        }
        std::ifstream l_confDataIStream("dbconf.json");
        std::string l_confDataIStreamStr((std::istreambuf_iterator<char>(l_confDataIStream)),(std::istreambuf_iterator<char>()));
        l_peerAddDataDoc.Parse(l_confDataIStreamStr.c_str());


        rapidjson::SchemaDocument l_schemaDocument(l_peerAddSchemaDoc);
        rapidjson::SchemaValidator l_SchemaValidator(l_schemaDocument);

        if(!l_peerAddDataDoc.Accept(l_SchemaValidator))
        {
                rapidjson::StringBuffer sb;
                l_SchemaValidator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
                printf("Invalid schema: %s\n", sb.GetString());
                printf("Invalid keyword: %s\n", l_SchemaValidator.GetInvalidSchemaKeyword());
                sb.Clear();
                l_SchemaValidator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
                printf("Invalid document: %s\n", sb.GetString());
        }
else
printf("\nJson file validated with the given schema successfully\n");

        return 0;
}

1 个答案:

答案 0 :(得分:2)

您的问题是required应该在根级别,而不是properties内部。实际上,您当前有一个无效的架构,因为properties内的所有键值都只能是对象。

{
  "$schema": "json-schema.org/draft-04/schema#",
  "title": "Schema",
  "description": "JSON schema for validating Json file",
  "type": "object",
  "properties": {
    "MAX_CONNECTION_PER_HOST": {
      "type": "number"
    },
    "QUEUE_IO_SIZE": {
      "type": "number"
    },
    "REQUEST_LOW_WATER_MARK": {
      "type": "number"
    }
  },
  "required": [
      "MAX_CONNECTION_PER_HOST",
      "QUEUE_IO_SIZE",
      "REQUEST_LOW_WATER_MARK"
    ]
}

我使用https://www.jsonschemavalidator.net进行了针对实例的架构验证。