nlib
nn::nlib::msgpack::JsonSchemaValidator Class Referencefinal

These classes are used to validate JSON and msgpack using JSON Schema. More...

#include "nn/nlib/msgpack/JsonSchema.h"

Classes

struct  Detail
 If the validation with JSON Schema has failed, a string representing the location where the validation has failed is written into a member named path. More...
 

Public Types

enum  Result {
  kOk = 0,
  kInvalidParam,
  kAlreadyInitialized,
  kOutOfMemory,
  kFail,
  kInvalidSchema,
  kVersionNotSupported
}
 The result of the function executed. More...
 

Public Member Functions

Result Validate (const MpObject &obj, Detail *detail) noexcept
 Validates using JSON Schema. More...
 
Constructor, Destructor, and Initialization
constexpr JsonSchemaValidator () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~JsonSchemaValidator () noexcept
 Destructor.
 
 JsonSchemaValidator (JsonSchemaValidator &&rhs)
 Instantiates the object (move constructor).
 
JsonSchemaValidatoroperator= (JsonSchemaValidator &&rhs)
 Move assignment operator.
 
void Reset () noexcept
 Resets this object to the state immediately after the default constructor was executed.
 
Result Init (const nlib_byte_t *schema_bytecode, size_t n) noexcept
 Initializes an object by specifying JSON Schema. More...
 

Detailed Description

These classes are used to validate JSON and msgpack using JSON Schema.

Description
You can use this class to validate MpObject constructed from JSON or msgpack. Use JSON Schema that is converted to a byte-code format using JsonSchemaConverter. Converting to the byte-code format allows the validation to run faster than using JSON Schema directly.
About JSON Schema
Using JSON Schema, you can describe JSON document structures and determine whether the JSON document is written according to the specified structure. Specifically, JSON Schema enables you to describe the following criteria for JSON documents and evaluate the documents against the criteria.
  • Whether they should contain a string, number, or other values; whether they should contain an object or array.
  • The range of numeric values or the length of strings.
  • The length of arrays or values they contain.
  • Names and values that objects contain.
  • Conjunction, selection or negation of above mentioned criteria.
JSON Schema can also be written with JSON, and its specification allows humans to easily read it and programs to easily process it. For more information, see the following URLs:
Note
For validations with "pattern" and "patternProperties," use std::regex. In an environment where no std::regex is provided, any validation with "pattern" and "patternProperties" successfully completes.
See also
http://json-schema.org/
http://json-schema.org/latest/json-schema-validation.html
https://spacetelescope.github.io/understanding-json-schema/
https://tools.ietf.org/html/draft-zyp-json-schema-04

Definition at line 32 of file JsonSchema.h.

Member Enumeration Documentation

◆ Result

The result of the function executed.

Enumerator
kOk 

Successfully completed. The validation with JSON Schema was successfully completed.

kInvalidParam 

Invalid argument.

kAlreadyInitialized 

Already initialized.

kOutOfMemory 

Failed to allocate memory.

kFail 

The validation with JSON Schema has failed.

kInvalidSchema 

Invalid JSON Schema.

kVersionNotSupported 

This version of JSON Schema contains byte-code generated with JsonSchemaConverter and is not supported.

Definition at line 34 of file JsonSchema.h.

Member Function Documentation

◆ Init()

nn::nlib::msgpack::JsonSchemaValidator::Init ( const nlib_byte_t schema_bytecode,
size_t  n 
)
noexcept

Initializes an object by specifying JSON Schema.

Parameters
[in]schema_bytecodeJSON Schema converted to byte-code with JsonSchemaConverter.
[in]nSize of schema_bytecode.
Return values
kOkSuccess.
kInvalidParamschema_bytecode is NULL or n is 0.
kAlreadyInitializedThe object has been already initialized.
kOutOfMemoryMemory allocation failed.
kInvalidSchemaInvalid schema.
kVersionNotSupportedUnsupported format.

◆ Validate()

nn::nlib::msgpack::JsonSchemaValidator::Validate ( const MpObject obj,
Detail detail 
)
noexcept

Validates using JSON Schema.

Parameters
[in]objData to be validated with JSON Schema.
[in]detailIf not NULL, additional information is provided when the validation has failed.
Return values
kOkThe validation has been successfully completed.
kFailThe validation has failed.
kOutOfMemoryMemory allocation failed.
kInvalidSchemaInvalid schema.
Description
The following sample code shows how JSON Schema is compiled to verify two JSON.
auto schema = ToMpObject(R"({
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "integer"
},
"name": {
"description": "Name of the product",
"type": "string"
},
"price": {
"type": "number",
"exclusiveMinimum": 0
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
"required": ["id", "name", "price"]
})");
JsonSchemaConverter converter;
SUCCEED_IF(converter.Init() == JsonSchemaConverter::kOk);
SUCCEED_IF(converter.Convert(schema.get(), nullptr) == JsonSchemaConverter::kOk);
auto bin_schema = converter.Export();
SUCCEED_IF(std::get<0>(bin_schema) == JsonSchemaConverter::kOk);
SUCCEED_IF(validator.Init(std::get<1>(bin_schema).get(), std::get<2>(bin_schema)) == JsonSchemaValidator::kOk);
auto conformant_json = ToMpObject(R"({
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"]
})");
SUCCEED_IF(validator.Validate(*conformant_json, nullptr) == JsonSchemaValidator::kOk);
// 'price' is not conformat to the schema
auto non_conformant_json = ToMpObject(R"({
"id": 1,
"name": "A green door",
"price": -3.0,
"tags": ["home", "green"]
})");
SUCCEED_IF(validator.Validate(*non_conformant_json, nullptr) == JsonSchemaValidator::kFail);

The documentation for this class was generated from the following files: