MQTTSuite
Loading...
Searching...
No Matches
anonymous_namespace{json-validator.cpp}::schema Class Referenceabstract
Inheritance diagram for anonymous_namespace{json-validator.cpp}::schema:
Collaboration diagram for anonymous_namespace{json-validator.cpp}::schema:

Public Member Functions

virtual ~schema ()=default
 schema (root_schema *root)
virtual void validate (const json::json_pointer &ptr, const json &instance, json_patch &patch, error_handler &e) const =0
virtual const jsondefault_value (const json::json_pointer &, const json &, error_handler &) const
void set_default_value (const json &v)
virtual ~schema ()=default
 schema (root_schema *root)
virtual void validate (const json::json_pointer &ptr, const json &instance, json_patch &patch, error_handler &e) const =0
virtual const jsondefault_value (const json::json_pointer &, const json &, error_handler &) const
void set_default_value (const json &v)

Static Public Member Functions

static std::shared_ptr< schemamake (json &schema, root_schema *root, const std::vector< std::string > &key, std::vector< nlohmann::json_uri > uris)
static std::shared_ptr< schemamake (json &schema, root_schema *root, const std::vector< std::string > &key, std::vector< nlohmann::json_uri > uris)

Protected Member Functions

virtual std::shared_ptr< schemamake_for_default_ (std::shared_ptr<::schema > &, root_schema *, std::vector< nlohmann::json_uri > &, nlohmann::json &) const
virtual std::shared_ptr< schemamake_for_default_ (std::shared_ptr<::schema > &, root_schema *, std::vector< nlohmann::json_uri > &, nlohmann::json &) const

Protected Attributes

root_schemaroot_
json default_value_ = nullptr

Detailed Description

Definition at line 38 of file json-validator.cpp.

Constructor & Destructor Documentation

◆ ~schema() [1/2]

virtual anonymous_namespace{json-validator.cpp}::schema::~schema ( )
virtualdefault

◆ schema() [1/2]

◆ ~schema() [2/2]

virtual anonymous_namespace{json-validator.cpp}::schema::~schema ( )
virtualdefault

◆ schema() [2/2]

anonymous_namespace{json-validator.cpp}::schema::schema ( root_schema * root)
inline

Definition at line 57 of file json-validator.cpp.

58 : root_(root) {}

Member Function Documentation

◆ default_value() [1/2]

virtual const json & anonymous_namespace{json-validator.cpp}::schema::default_value ( const json::json_pointer & ,
const json & ,
error_handler &  ) const
inlinevirtual

◆ default_value() [2/2]

virtual const json & anonymous_namespace{json-validator.cpp}::schema::default_value ( const json::json_pointer & ,
const json & ,
error_handler &  ) const
inlinevirtual

◆ make() [1/2]

std::shared_ptr< schema > anonymous_namespace{json-validator.cpp}::schema::make ( json & schema,
root_schema * root,
const std::vector< std::string > & key,
std::vector< nlohmann::json_uri > uris )
static

Definition at line 1355 of file json-validator.cpp.

1359{
1360 // remove URIs which contain plain name identifiers, as sub-schemas cannot be referenced
1361 for (auto uri = uris.begin(); uri != uris.end();)
1362 if (uri->identifier() != "")
1363 uri = uris.erase(uri);
1364 else
1365 uri++;
1366
1367 // append to all URIs the keys for this sub-schema
1368 for (auto &key : keys)
1369 for (auto &uri : uris)
1370 uri = uri.append(key);
1371
1372 std::shared_ptr<::schema> sch;
1373
1374 // boolean schema
1375 if (schema.type() == json::value_t::boolean)
1376 sch = std::make_shared<boolean>(schema, root);
1377 else if (schema.type() == json::value_t::object) {
1378
1379 auto attr = schema.find("$id"); // if $id is present, this schema can be referenced by this ID
1380 // as an additional URI
1381 if (attr != schema.end()) {
1382 if (std::find(uris.begin(),
1383 uris.end(),
1384 attr.value().get<std::string>()) == uris.end())
1385 uris.push_back(uris.back().derive(attr.value().get<std::string>())); // so add it to the list if it is not there already
1386 schema.erase(attr);
1387 }
1388
1389 auto findDefinitions = [&](const std::string &defs) -> bool {
1390 attr = schema.find(defs);
1391 if (attr != schema.end()) {
1392 for (auto &def : attr.value().items())
1393 schema::make(def.value(), root, {defs, def.key()}, uris);
1394 schema.erase(attr);
1395 return true;
1396 }
1397 return false;
1398 };
1399 if (!findDefinitions("$defs")) {
1400 findDefinitions("definitions");
1401 }
1402
1403 attr = schema.find("$ref");
1404 if (attr != schema.end()) { // this schema is a reference
1405 // the last one on the uri-stack is the last id seen before coming here,
1406 // so this is the origial URI for this reference, the $ref-value has thus be resolved from it
1407 auto id = uris.back().derive(attr.value().get<std::string>());
1408 sch = root->get_or_create_ref(id);
1409
1410 schema.erase(attr);
1411
1412 // special case where we break draft-7 and allow overriding of properties when a $ref is used
1413 attr = schema.find("default");
1414 if (attr != schema.end()) {
1415 // copy the referenced schema depending on the underlying type and modify the default value
1416 if (auto new_sch = sch->make_for_default_(sch, root, uris, attr.value())) {
1417 sch = new_sch;
1418 }
1419 schema.erase(attr);
1420 }
1421 } else {
1422 sch = std::make_shared<type_schema>(schema, root, uris);
1423 }
1424
1425 schema.erase("$schema");
1426 schema.erase("title");
1427 schema.erase("description");
1428 } else {
1429 throw std::invalid_argument("invalid JSON-type for a schema for " + uris[0].to_string() + ", expected: boolean or object");
1430 }
1431
1432 for (auto &uri : uris) { // for all URIs this schema is referenced by
1433 root->insert(uri, sch);
1434
1435 if (schema.type() == json::value_t::object)
1436 for (auto &u : schema.items())
1437 root->insert_unknown_keyword(uri, u.key(), u.value()); // insert unknown keywords for later reference
1438 }
1439 return sch;
1440}
static std::shared_ptr< schema > make(json &schema, root_schema *root, const std::vector< std::string > &key, std::vector< nlohmann::json_uri > uris)
void insert_unknown_keyword(const json_uri &uri, const std::string &key, json &value)
void insert(const json_uri &uri, const std::shared_ptr< schema > &s)
std::shared_ptr< schema > get_or_create_ref(const json_uri &uri)
const std::string logical_combination< allOf >::key

References nlohmann::json_uri::append(), nlohmann::json_uri::derive(), nlohmann::json_schema::root_schema::get_or_create_ref(), nlohmann::json_schema::root_schema::insert(), nlohmann::json_schema::root_schema::insert_unknown_keyword(), make(), make_for_default_(), and nlohmann::json_uri::to_string().

Referenced by anonymous_namespace{json-validator.cpp}::array::array(), nlohmann::json_schema::root_schema::get_or_create_ref(), nlohmann::json_schema::root_schema::insert_unknown_keyword(), anonymous_namespace{json-validator.cpp}::logical_combination< combine_logic >::logical_combination(), anonymous_namespace{json-validator.cpp}::logical_not::logical_not(), make(), anonymous_namespace{json-validator.cpp}::object::object(), nlohmann::json_schema::root_schema::set_root_schema(), and anonymous_namespace{json-validator.cpp}::type_schema::type_schema().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make() [2/2]

std::shared_ptr< schema > anonymous_namespace{json-validator.cpp}::schema::make ( json & schema,
root_schema * root,
const std::vector< std::string > & key,
std::vector< nlohmann::json_uri > uris )
static

◆ make_for_default_() [1/2]

virtual std::shared_ptr< schema > anonymous_namespace{json-validator.cpp}::schema::make_for_default_ ( std::shared_ptr<::schema > & ,
root_schema * ,
std::vector< nlohmann::json_uri > & ,
nlohmann::json &  ) const
inlineprotectedvirtual

Reimplemented in anonymous_namespace{json-validator.cpp}::schema_ref, anonymous_namespace{json-validator.cpp}::schema_ref, anonymous_namespace{json-validator.cpp}::type_schema, and anonymous_namespace{json-validator.cpp}::type_schema.

Definition at line 45 of file json-validator.cpp.

50 {
51 return nullptr;
52 };

Referenced by make().

Here is the caller graph for this function:

◆ make_for_default_() [2/2]

virtual std::shared_ptr< schema > anonymous_namespace{json-validator.cpp}::schema::make_for_default_ ( std::shared_ptr<::schema > & ,
root_schema * ,
std::vector< nlohmann::json_uri > & ,
nlohmann::json &  ) const
inlineprotectedvirtual

◆ set_default_value() [1/2]

void anonymous_namespace{json-validator.cpp}::schema::set_default_value ( const json & v)
inline

Definition at line 67 of file json-validator.cpp.

67{ default_value_ = v; }

References default_value_.

Referenced by anonymous_namespace{json-validator.cpp}::schema_ref::make_for_default_(), anonymous_namespace{json-validator.cpp}::type_schema::make_for_default_(), anonymous_namespace{json-validator.cpp}::object::object(), and anonymous_namespace{json-validator.cpp}::type_schema::type_schema().

Here is the caller graph for this function:

◆ set_default_value() [2/2]

void anonymous_namespace{json-validator.cpp}::schema::set_default_value ( const json & v)
inline

Definition at line 67 of file json-validator.cpp.

67{ default_value_ = v; }

◆ validate() [1/2]

virtual void anonymous_namespace{json-validator.cpp}::schema::validate ( const json::json_pointer & ptr,
const json & instance,
json_patch & patch,
error_handler & e ) const
pure virtual

Implemented in anonymous_namespace{json-validator.cpp}::array, anonymous_namespace{json-validator.cpp}::array, anonymous_namespace{json-validator.cpp}::boolean, anonymous_namespace{json-validator.cpp}::boolean, anonymous_namespace{json-validator.cpp}::boolean_type, anonymous_namespace{json-validator.cpp}::boolean_type, anonymous_namespace{json-validator.cpp}::logical_combination< combine_logic >, anonymous_namespace{json-validator.cpp}::logical_combination< combine_logic >, anonymous_namespace{json-validator.cpp}::logical_not, anonymous_namespace{json-validator.cpp}::logical_not, anonymous_namespace{json-validator.cpp}::null, anonymous_namespace{json-validator.cpp}::null, anonymous_namespace{json-validator.cpp}::numeric< T >, anonymous_namespace{json-validator.cpp}::numeric< T >, anonymous_namespace{json-validator.cpp}::object, anonymous_namespace{json-validator.cpp}::object, anonymous_namespace{json-validator.cpp}::required, anonymous_namespace{json-validator.cpp}::required, anonymous_namespace{json-validator.cpp}::schema_ref, anonymous_namespace{json-validator.cpp}::schema_ref, anonymous_namespace{json-validator.cpp}::string, anonymous_namespace{json-validator.cpp}::string, anonymous_namespace{json-validator.cpp}::type_schema, and anonymous_namespace{json-validator.cpp}::type_schema.

Referenced by anonymous_namespace{json-validator.cpp}::array::validate(), anonymous_namespace{json-validator.cpp}::logical_combination< combine_logic >::validate(), anonymous_namespace{json-validator.cpp}::logical_not::validate(), anonymous_namespace{json-validator.cpp}::object::validate(), anonymous_namespace{json-validator.cpp}::schema_ref::validate(), anonymous_namespace{json-validator.cpp}::type_schema::validate(), and nlohmann::json_schema::root_schema::validate().

Here is the caller graph for this function:

◆ validate() [2/2]

Member Data Documentation

◆ default_value_

json anonymous_namespace{json-validator.cpp}::schema::default_value_ = nullptr
protected

◆ root_

root_schema * anonymous_namespace{json-validator.cpp}::schema::root_
protected

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