26#ifndef ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_SCHEMA_VALIDATOR_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_SCHEMA_VALIDATOR_H_
32#include <rapidjson/document.h>
33#include <rapidjson/schema.h>
34#include <rapidjson/stringbuffer.h>
43 const std::string &json,
const std::string &schema,
44 std::string *error_description =
nullptr) {
45 using namespace std::string_literals;
46 auto get_pointer_string =
47 [](
const rapidjson::Pointer &pointer) -> std::string {
48 rapidjson::StringBuffer buff;
49 pointer.StringifyUriFragment(buff);
50 return {buff.GetString(), buff.GetSize()};
53 rapidjson::Document rjschema;
54 rapidjson::Document rjjson;
56 rjschema.Parse(schema.c_str(), schema.length());
57 rjjson.Parse(json.c_str(), json.length());
59 if (rjschema.HasParseError()) {
60 *error_description =
"Validation schema, parsing error: "s +
65 if (rjjson.HasParseError()) {
66 *error_description =
"Json object, parsing error: "s +
71 rapidjson::SchemaDocument schema_document{rjschema};
72 rapidjson::SchemaValidator validator{schema_document};
74 if (!validator.IsValid()) {
75 if (error_description) {
76 *error_description =
"Validator is invalid.";
81 if (rjjson.Accept(validator))
return true;
83 if (error_description) {
84 using namespace std::string_literals;
86 "JSON validation location "s +
87 get_pointer_string(validator.GetInvalidDocumentPointer()) +
88 " failed requirement: '" + validator.GetInvalidSchemaKeyword() +
89 "' at meta schema location '" +
90 get_pointer_string(validator.GetInvalidSchemaPointer()) +
"'";
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
Define rapidjson::SizeType to be std::uint64_t.
bool validate_json_with_schema(const std::string &json, const std::string &schema, std::string *error_description=nullptr)
Definition: schema_validator.h:42