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 ,
  kOutOfMemory,
  kFail,
  kInvalidSchema,
  kVersionNotSupported
}
 The result of the function executed. More...
 

Public Member Functions

constexpr JsonSchemaValidator () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~JsonSchemaValidator () noexcept
 Destructor.
 
void Reset () noexcept
 Sets the object to the same state as the one obtained after the default constructor has run.
 
Result Init (const nlib_byte_t *schema_bytecode, size_t n) noexcept
 Initializes an object by specifying JSON Schema. More...
 
Result Validate (const MpObject &obj, Detail *detail) noexcept
 Validates using 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.

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.

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