MySQL 9.0.0
Source Code Documentation
json_schema.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_JSON_SCHEMA_H_INCLUDED
25#define SQL_JSON_SCHEMA_H_INCLUDED
26
27/**
28 @file json_schema.h
29
30 Functions for validating a string against a JSON Schema
31
32 A JSON Schema is a way to describe the structure of a JSON document. The JSON
33 Schema is a JSON document in itself, and allows you to define required
34 names/attributes, data types etc. As an example, here is a minimal example of
35 a JSON Schema describing that the JSON document MUST be an object:
36
37 {
38 "type": "object"
39 }
40
41 If the JSON document to be validated is anything else than an object (array,
42 scalar), the validation will fail.
43
44 This file contains one class for validation JSON documents against a cached
45 JSON Schema, and free functions for validation any string input against a
46 (unparsed) JSON Schema. We use the rapidjson library to do the actual
47 validation with the following notable behaviors:
48
49 1) Remote references are not supported. If the user provides a JSON Schema
50 with a remote reference, an error will be raised.
51 2) JSON Schema supports regex patterns, and we use std::regex as the regex
52 engine. If an invalid regex pattern is provided in the JSON Schema, the
53 regex pattern will be silently ignored.
54 3) rapidjson currently supports JSON Schema draft-v4, while there are newer
55 versions available (as of writing, draft-v7 is the latest version).
56 */
57
58#include <cstddef>
59#include <string>
60
61#include "my_alloc.h"
63
65struct MEM_ROOT;
66
67/**
68 Json_schema_validation_report contains a more detailed report about a failed
69 JSON Schema validation. It's mainly used by the function
70 JSON_SCHEMA_VALIDATION_REPORT to print out a more detailed report to the user.
71*/
73 public:
74 /// @returns a human readable reason why the validation failed
75 std::string human_readable_reason() const;
76
77 /**
78 @returns a JSON pointer in URI format, pointing to where in the JSON
79 Schema the validation failed
80 */
81 const std::string &schema_location() const { return m_schema_location; }
82
83 /**
84 @returns a string describing the name of the JSON Schema keyword that
85 failed validation
86 */
87 const std::string &schema_failed_keyword() const {
89 }
90
91 /**
92 @returns a JSON pointer in URI format, pointing to where in the JSON
93 document the validation failed
94 */
95 const std::string &document_location() const { return m_document_location; }
96
97 /**
98 Populates the object with validation information.
99
100 @param schema_location a JSON pointer in URI format, pointing to where in
101 the JSON Schema the validation failed
102 @param schema_failed_keyword a string describing the name of the JSON Schema
103 keyword that failed validation
104 @param document_location a JSON pointer in URI format, pointing to where in
105 the JSON document the validation failed
106 */
108 const char *schema_failed_keyword,
109 std::string &&document_location) {
113 }
114
115 private:
116 std::string m_schema_location;
119};
120
121/**
122 This function will validate a JSON document against a JSON Schema using the
123 validation provided by rapidjson.
124
125 @param document_str A pointer to the JSON document to be validated.
126 @param document_length The length of the JSON document to be validated.
127 @param json_schema_str A pointer to the JSON Schema.
128 @param json_schema_length The length of the JSON Schema.
129 @param error_handler Error handlers to be called when parsing errors occur.
130 @param depth_handler Pointer to a function that should handle error
131 occurred when depth is exceeded.
132 @param[out] is_valid A variable containing the result of the validation. If
133 true, the JSON document is valid according to the given
134 JSON Schema.
135 @param[out] report A structure containing a detailed report from the
136 validation. Is only populated if is_valid is set to
137 "false". Can be nullptr if a detailed report isn't needed.
138
139 @retval true if anything went wrong (like parsing the JSON inputs). my_error
140 has been called with an appropriate error message.
141 @retval false if the validation succeeded. The result of the validation can be
142 found in the output variable "is_valid".
143*/
144bool is_valid_json_schema(const char *document_str, size_t document_length,
145 const char *json_schema_str,
146 size_t json_schema_length,
147 const JsonSchemaErrorHandler &error_handler,
148 const JsonErrorHandler &depth_handler, bool *is_valid,
150
151/**
152 This is just a facade to the Json_schema_validator and it is used to
153 hide the dependency on the rapidjson lib.
154*/
156 private:
158
159 public:
160 /**
161 Initialize a Json_schema_validator_impl, allocated on a given MEM_ROOT
162
163 @param mem_root The MEM_ROOT to allocate the validator on
164 @param json_schema_str A pointer to the JSON Schema
165 @param json_schema_length The length of the JSON Schema input
166 @param error_handler Error handlers to be called when parsing errors occur.
167 @param depth_handler Pointer to a function that should handle error
168 occurred when depth is exceeded.
169
170 @retval true on error (my_error has been called)
171 */
172 bool initialize(MEM_ROOT *mem_root, const char *json_schema_str,
173 size_t json_schema_length,
174 const JsonSchemaErrorHandler &error_handler,
175 const JsonErrorHandler &depth_handler);
176
177 bool is_valid(const char *document_str, size_t document_length,
178 const JsonSchemaErrorHandler &error_handler,
179 const JsonErrorHandler &depth_handler, bool *is_valid,
180 Json_schema_validation_report *report) const;
181 bool is_initialized() const { return m_json_schema_validator != nullptr; }
183};
184
185#endif
Error handler to be used when parsing JSON schemas and validating JSON objects using a JSON schema.
Definition: json_error_handler.h:79
Json_schema_validation_report contains a more detailed report about a failed JSON Schema validation.
Definition: json_schema.h:72
const std::string & schema_location() const
Definition: json_schema.h:81
const std::string & document_location() const
Definition: json_schema.h:95
std::string m_schema_location
Definition: json_schema.h:116
std::string human_readable_reason() const
Definition: json_schema.cc:296
const std::string & schema_failed_keyword() const
Definition: json_schema.h:87
std::string m_document_location
Definition: json_schema.h:118
void set_error_report(std::string &&schema_location, const char *schema_failed_keyword, std::string &&document_location)
Populates the object with validation information.
Definition: json_schema.h:107
std::string m_schema_failed_keyword
Definition: json_schema.h:117
Json_schema_validator_impl is an object that contains a JSON Schema that can be re-used multiple time...
Definition: json_schema.cc:49
This is just a facade to the Json_schema_validator and it is used to hide the dependency on the rapid...
Definition: json_schema.h:155
Json_schema_validator_impl * m_json_schema_validator
Definition: json_schema.h:157
bool is_valid(const char *document_str, size_t document_length, const JsonSchemaErrorHandler &error_handler, const JsonErrorHandler &depth_handler, bool *is_valid, Json_schema_validation_report *report) const
Definition: json_schema.cc:199
~Json_schema_validator()
Definition: json_schema.cc:209
bool initialize(MEM_ROOT *mem_root, const char *json_schema_str, size_t json_schema_length, const JsonSchemaErrorHandler &error_handler, const JsonErrorHandler &depth_handler)
Initialize a Json_schema_validator_impl, allocated on a given MEM_ROOT.
Definition: json_schema.cc:184
bool is_initialized() const
Definition: json_schema.h:181
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
std::function< void()> JsonErrorHandler
Definition: json_error_handler.h:36
bool is_valid_json_schema(const char *document_str, size_t document_length, const char *json_schema_str, size_t json_schema_length, const JsonSchemaErrorHandler &error_handler, const JsonErrorHandler &depth_handler, bool *is_valid, Json_schema_validation_report *report)
This function will validate a JSON document against a JSON Schema using the validation provided by ra...
Definition: json_schema.cc:162
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
bool is_valid(const dd::Spatial_reference_system *srs, const Geometry *g, const char *func_name, bool *is_valid) noexcept
Decides if a geometry is valid.
Definition: is_valid.cc:95
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83