MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
schema_validator.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_SCHEMA_VALIDATOR_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_SCHEMA_VALIDATOR_H_
28
29#include <string>
30
31#include <my_rapidjson_size_t.h>
32#include <rapidjson/document.h>
33#include <rapidjson/schema.h>
34#include <rapidjson/stringbuffer.h>
35
37#include "helper/json/text_to.h"
38
39namespace helper {
40namespace json {
41
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()};
51 };
52
53 rapidjson::Document rjschema;
54 rapidjson::Document rjjson;
55
56 rjschema.Parse(schema.c_str(), schema.length());
57 rjjson.Parse(json.c_str(), json.length());
58
59 if (rjschema.HasParseError()) {
60 *error_description = "Validation schema, parsing error: "s +
61 std::to_string(rjschema.GetParseError());
62 return false;
63 }
64
65 if (rjjson.HasParseError()) {
66 *error_description = "Json object, parsing error: "s +
67 std::to_string(rjjson.GetParseError());
68 return false;
69 }
70
71 rapidjson::SchemaDocument schema_document{rjschema};
72 rapidjson::SchemaValidator validator{schema_document};
73
74 if (!validator.IsValid()) {
75 if (error_description) {
76 *error_description = "Validator is invalid.";
77 }
78 return false;
79 }
80
81 if (rjjson.Accept(validator)) return true;
82
83 if (error_description) {
84 using namespace std::string_literals;
85 *error_description =
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()) + "'";
91 }
92
93 return false;
94}
95
96} // namespace json
97} // namespace helper
98
99#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_SCHEMA_VALIDATOR_H_
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
Definition: cache.h:33