MySQL 9.0.0
Source Code Documentation
json_schema.h File Reference

Functions for validating a string against a JSON Schema. More...

#include <cstddef>
#include <string>
#include "my_alloc.h"
#include "sql-common/json_error_handler.h"

Go to the source code of this file.

Classes

class  Json_schema_validation_report
 Json_schema_validation_report contains a more detailed report about a failed JSON Schema validation. More...
 
class  Json_schema_validator
 This is just a facade to the Json_schema_validator and it is used to hide the dependency on the rapidjson lib. More...
 

Functions

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 rapidjson. More...
 

Detailed Description

Functions for validating a string against a JSON Schema.

A JSON Schema is a way to describe the structure of a JSON document. The JSON Schema is a JSON document in itself, and allows you to define required names/attributes, data types etc. As an example, here is a minimal example of a JSON Schema describing that the JSON document MUST be an object:

{ "type": "object" }

If the JSON document to be validated is anything else than an object (array, scalar), the validation will fail.

This file contains one class for validation JSON documents against a cached JSON Schema, and free functions for validation any string input against a (unparsed) JSON Schema. We use the rapidjson library to do the actual validation with the following notable behaviors:

1) Remote references are not supported. If the user provides a JSON Schema with a remote reference, an error will be raised. 2) JSON Schema supports regex patterns, and we use std::regex as the regex engine. If an invalid regex pattern is provided in the JSON Schema, the regex pattern will be silently ignored. 3) rapidjson currently supports JSON Schema draft-v4, while there are newer versions available (as of writing, draft-v7 is the latest version).

Function Documentation

◆ is_valid_json_schema()

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 rapidjson.

Parameters
document_strA pointer to the JSON document to be validated.
document_lengthThe length of the JSON document to be validated.
json_schema_strA pointer to the JSON Schema.
json_schema_lengthThe length of the JSON Schema.
error_handlerError handlers to be called when parsing errors occur.
depth_handlerPointer to a function that should handle error occurred when depth is exceeded.
[out]is_validA variable containing the result of the validation. If true, the JSON document is valid according to the given JSON Schema.
[out]reportA structure containing a detailed report from the validation. Is only populated if is_valid is set to "false". Can be nullptr if a detailed report isn't needed.
Return values
trueif anything went wrong (like parsing the JSON inputs). my_error has been called with an appropriate error message.
falseif the validation succeeded. The result of the validation can be found in the output variable "is_valid".