MySQL 8.4.0
Source Code Documentation
json_schema.h File Reference

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

#include "my_rapidjson_size_t.h"
#include <rapidjson/schema.h>
#include <cstddef>
#include <string>
#include "my_alloc.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
 Json_schema_validator is an object that contains a JSON Schema that can be re-used multiple times. More...
 
class  Json_schema_validator::My_remote_schema_document_provider
 This object acts as a handler/callback for the JSON schema validator and is called whenever a schema reference is encountered in the JSON document. 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 char *function_name, 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...
 
unique_ptr_destroy_only< const Json_schema_validatorcreate_json_schema_validator (MEM_ROOT *mem_root, const char *json_schema_str, size_t json_schema_length, const char *function_name)
 Create a Json_schema_validator, allocated on a given MEM_ROOT. 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

◆ create_json_schema_validator()

unique_ptr_destroy_only< const Json_schema_validator > create_json_schema_validator ( MEM_ROOT mem_root,
const char *  json_schema_str,
size_t  json_schema_length,
const char *  function_name 
)

Create a Json_schema_validator, allocated on a given MEM_ROOT.

Parameters
mem_rootThe MEM_ROOT to allocate the validator on
json_schema_strA pointer to the JSON Schema
json_schema_lengthThe length of the JSON Schema input
function_nameThe function name of the caller (to be used in error reporting)
Return values
nullptron error (my_error has been called)

◆ 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 char *  function_name,
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.
function_nameThe name of the SQL function calling this function. Used in error reporting.
[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".