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

Public Member Functions

 object (json &sch, root_schema *root, const std::vector< nlohmann::json_uri > &uris)
 object (json &sch, root_schema *root, const std::vector< nlohmann::json_uri > &uris)
Public Member Functions inherited from anonymous_namespace{json-validator.cpp}::schema
virtual ~schema ()=default
 schema (root_schema *root)
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 const jsondefault_value (const json::json_pointer &, const json &, error_handler &) const
void set_default_value (const json &v)

Private Member Functions

void validate (const json::json_pointer &ptr, const json &instance, json_patch &patch, error_handler &e) const override
void validate (const json::json_pointer &ptr, const json &instance, json_patch &patch, error_handler &e) const override

Private Attributes

std::pair< bool, size_t > maxProperties_ {false, 0}
std::pair< bool, size_t > minProperties_ {false, 0}
std::vector< std::string > required_
std::map< std::string, std::shared_ptr< schema > > properties_
std::vector< std::pair< std::regex, std::shared_ptr< schema > > > patternProperties_
std::shared_ptr< schemaadditionalProperties_
std::map< std::string, std::shared_ptr< schema > > dependencies_
std::shared_ptr< schemapropertyNames_

Additional Inherited Members

Static Public Member Functions inherited from anonymous_namespace{json-validator.cpp}::schema
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 inherited from anonymous_namespace{json-validator.cpp}::schema
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 inherited from anonymous_namespace{json-validator.cpp}::schema
root_schemaroot_
json default_value_ = nullptr

Detailed Description

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

Constructor & Destructor Documentation

◆ object() [1/2]

anonymous_namespace{json-validator.cpp}::object::object ( json & sch,
root_schema * root,
const std::vector< nlohmann::json_uri > & uris )
inline

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

1120 : schema(root)
1121 {
1122 auto attr = sch.find("maxProperties");
1123 if (attr != sch.end()) {
1124 maxProperties_ = {true, attr.value().get<size_t>()};
1125 sch.erase(attr);
1126 }
1127
1128 attr = sch.find("minProperties");
1129 if (attr != sch.end()) {
1130 minProperties_ = {true, attr.value().get<size_t>()};
1131 sch.erase(attr);
1132 }
1133
1134 attr = sch.find("required");
1135 if (attr != sch.end()) {
1136 required_ = attr.value().get<std::vector<std::string>>();
1137 sch.erase(attr);
1138 }
1139
1140 attr = sch.find("properties");
1141 if (attr != sch.end()) {
1142 for (auto prop : attr.value().items())
1143 properties_.insert(
1144 std::make_pair(
1145 prop.key(),
1146 schema::make(prop.value(), root, {"properties", prop.key()}, uris)));
1147 sch.erase(attr);
1148 }
1149
1150#ifndef NO_STD_REGEX
1151 attr = sch.find("patternProperties");
1152 if (attr != sch.end()) {
1153 for (auto prop : attr.value().items())
1154 patternProperties_.push_back(
1155 std::make_pair(
1156 REGEX_NAMESPACE::regex(prop.key(), REGEX_NAMESPACE::regex::ECMAScript),
1157 schema::make(prop.value(), root, {prop.key()}, uris)));
1158 sch.erase(attr);
1159 }
1160#endif
1161
1162 attr = sch.find("additionalProperties");
1163 if (attr != sch.end()) {
1164 additionalProperties_ = schema::make(attr.value(), root, {"additionalProperties"}, uris);
1165 sch.erase(attr);
1166 }
1167
1168 attr = sch.find("dependencies");
1169 if (attr != sch.end()) {
1170 for (auto &dep : attr.value().items())
1171 switch (dep.value().type()) {
1172 case json::value_t::array:
1173 dependencies_.emplace(dep.key(),
1174 std::make_shared<required>(
1175 dep.value().get<std::vector<std::string>>(), root));
1176 break;
1177
1178 default:
1179 dependencies_.emplace(dep.key(),
1180 schema::make(dep.value(), root, {"dependencies", dep.key()}, uris));
1181 break;
1182 }
1183 sch.erase(attr);
1184 }
1185
1186 attr = sch.find("propertyNames");
1187 if (attr != sch.end()) {
1188 propertyNames_ = schema::make(attr.value(), root, {"propertyNames"}, uris);
1189 sch.erase(attr);
1190 }
1191
1192 attr = sch.find("default");
1193 if (attr != sch.end()) {
1194 set_default_value(*attr);
1195 }
1196 }
std::map< std::string, std::shared_ptr< schema > > dependencies_
std::vector< std::pair< std::regex, std::shared_ptr< schema > > > patternProperties_
std::map< std::string, std::shared_ptr< schema > > properties_
static std::shared_ptr< schema > make(json &schema, root_schema *root, const std::vector< std::string > &key, std::vector< nlohmann::json_uri > uris)
auto schema
Definition id-ref.cpp:69

References additionalProperties_, dependencies_, anonymous_namespace{json-validator.cpp}::schema::make(), maxProperties_, minProperties_, patternProperties_, properties_, propertyNames_, required_, anonymous_namespace{json-validator.cpp}::schema::schema(), and anonymous_namespace{json-validator.cpp}::schema::set_default_value().

Here is the call graph for this function:

◆ object() [2/2]

anonymous_namespace{json-validator.cpp}::object::object ( json & sch,
root_schema * root,
const std::vector< nlohmann::json_uri > & uris )
inline

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

1120 : schema(root)
1121 {
1122 auto attr = sch.find("maxProperties");
1123 if (attr != sch.end()) {
1124 maxProperties_ = {true, attr.value().get<size_t>()};
1125 sch.erase(attr);
1126 }
1127
1128 attr = sch.find("minProperties");
1129 if (attr != sch.end()) {
1130 minProperties_ = {true, attr.value().get<size_t>()};
1131 sch.erase(attr);
1132 }
1133
1134 attr = sch.find("required");
1135 if (attr != sch.end()) {
1136 required_ = attr.value().get<std::vector<std::string>>();
1137 sch.erase(attr);
1138 }
1139
1140 attr = sch.find("properties");
1141 if (attr != sch.end()) {
1142 for (auto prop : attr.value().items())
1143 properties_.insert(
1144 std::make_pair(
1145 prop.key(),
1146 schema::make(prop.value(), root, {"properties", prop.key()}, uris)));
1147 sch.erase(attr);
1148 }
1149
1150#ifndef NO_STD_REGEX
1151 attr = sch.find("patternProperties");
1152 if (attr != sch.end()) {
1153 for (auto prop : attr.value().items())
1154 patternProperties_.push_back(
1155 std::make_pair(
1156 REGEX_NAMESPACE::regex(prop.key(), REGEX_NAMESPACE::regex::ECMAScript),
1157 schema::make(prop.value(), root, {prop.key()}, uris)));
1158 sch.erase(attr);
1159 }
1160#endif
1161
1162 attr = sch.find("additionalProperties");
1163 if (attr != sch.end()) {
1164 additionalProperties_ = schema::make(attr.value(), root, {"additionalProperties"}, uris);
1165 sch.erase(attr);
1166 }
1167
1168 attr = sch.find("dependencies");
1169 if (attr != sch.end()) {
1170 for (auto &dep : attr.value().items())
1171 switch (dep.value().type()) {
1172 case json::value_t::array:
1173 dependencies_.emplace(dep.key(),
1174 std::make_shared<required>(
1175 dep.value().get<std::vector<std::string>>(), root));
1176 break;
1177
1178 default:
1179 dependencies_.emplace(dep.key(),
1180 schema::make(dep.value(), root, {"dependencies", dep.key()}, uris));
1181 break;
1182 }
1183 sch.erase(attr);
1184 }
1185
1186 attr = sch.find("propertyNames");
1187 if (attr != sch.end()) {
1188 propertyNames_ = schema::make(attr.value(), root, {"propertyNames"}, uris);
1189 sch.erase(attr);
1190 }
1191
1192 attr = sch.find("default");
1193 if (attr != sch.end()) {
1194 set_default_value(*attr);
1195 }
1196 }

Member Function Documentation

◆ validate() [1/2]

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

Implements anonymous_namespace{json-validator.cpp}::schema.

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

1056 {
1057 if (maxProperties_.first && instance.size() > maxProperties_.second)
1058 e.error(ptr, instance, "too many properties");
1059
1060 if (minProperties_.first && instance.size() < minProperties_.second)
1061 e.error(ptr, instance, "too few properties");
1062
1063 for (auto &r : required_)
1064 if (instance.find(r) == instance.end())
1065 e.error(ptr, instance, "required property '" + r + "' not found in object");
1066
1067 // for each property in instance
1068 for (auto &p : instance.items()) {
1069 if (propertyNames_)
1070 propertyNames_->validate(ptr, p.key(), patch, e);
1071
1072 bool a_prop_or_pattern_matched = false;
1073 auto schema_p = properties_.find(p.key());
1074 // check if it is in "properties"
1075 if (schema_p != properties_.end()) {
1076 a_prop_or_pattern_matched = true;
1077 schema_p->second->validate(ptr / p.key(), p.value(), patch, e);
1078 }
1079
1080#ifndef NO_STD_REGEX
1081 // check all matching patternProperties
1082 for (auto &schema_pp : patternProperties_)
1083 if (REGEX_NAMESPACE::regex_search(p.key(), schema_pp.first)) {
1084 a_prop_or_pattern_matched = true;
1085 schema_pp.second->validate(ptr / p.key(), p.value(), patch, e);
1086 }
1087#endif
1088
1089 // check additionalProperties as a last resort
1090 if (!a_prop_or_pattern_matched && additionalProperties_) {
1091 first_error_handler additional_prop_err;
1092 additionalProperties_->validate(ptr / p.key(), p.value(), patch, additional_prop_err);
1093 if (additional_prop_err)
1094 e.error(ptr, instance, "validation failed for additional property '" + p.key() + "': " + additional_prop_err.message_);
1095 }
1096 }
1097
1098 // reverse search
1099 for (auto const &prop : properties_) {
1100 const auto finding = instance.find(prop.first);
1101 if (instance.end() == finding) { // if the prop is not in the instance
1102 const auto &default_value = prop.second->default_value(ptr, instance, e);
1103 if (!default_value.is_null()) { // if default value is available
1104 patch.add((ptr / prop.first), default_value);
1105 }
1106 }
1107 }
1108
1109 for (auto &dep : dependencies_) {
1110 auto prop = instance.find(dep.first);
1111 if (prop != instance.end()) // if dependency-property is present in instance
1112 dep.second->validate(ptr / dep.first, instance, patch, e); // validate
1113 }
1114 }
virtual const json & default_value(const json::json_pointer &, const json &, error_handler &) const
json_patch & add(const json::json_pointer &, json value)
virtual void error(const json::json_pointer &, const json &, const std::string &)=0
static const auto instance
Definition issue-93.cpp:14

References nlohmann::json_patch::add(), additionalProperties_, anonymous_namespace{json-validator.cpp}::schema::default_value(), dependencies_, nlohmann::json_schema::error_handler::error(), maxProperties_, anonymous_namespace{json-validator.cpp}::first_error_handler::message_, minProperties_, patternProperties_, properties_, propertyNames_, required_, and anonymous_namespace{json-validator.cpp}::schema::validate().

Here is the call graph for this function:

◆ validate() [2/2]

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

Implements anonymous_namespace{json-validator.cpp}::schema.

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

1056 {
1057 if (maxProperties_.first && instance.size() > maxProperties_.second)
1058 e.error(ptr, instance, "too many properties");
1059
1060 if (minProperties_.first && instance.size() < minProperties_.second)
1061 e.error(ptr, instance, "too few properties");
1062
1063 for (auto &r : required_)
1064 if (instance.find(r) == instance.end())
1065 e.error(ptr, instance, "required property '" + r + "' not found in object");
1066
1067 // for each property in instance
1068 for (auto &p : instance.items()) {
1069 if (propertyNames_)
1070 propertyNames_->validate(ptr, p.key(), patch, e);
1071
1072 bool a_prop_or_pattern_matched = false;
1073 auto schema_p = properties_.find(p.key());
1074 // check if it is in "properties"
1075 if (schema_p != properties_.end()) {
1076 a_prop_or_pattern_matched = true;
1077 schema_p->second->validate(ptr / p.key(), p.value(), patch, e);
1078 }
1079
1080#ifndef NO_STD_REGEX
1081 // check all matching patternProperties
1082 for (auto &schema_pp : patternProperties_)
1083 if (REGEX_NAMESPACE::regex_search(p.key(), schema_pp.first)) {
1084 a_prop_or_pattern_matched = true;
1085 schema_pp.second->validate(ptr / p.key(), p.value(), patch, e);
1086 }
1087#endif
1088
1089 // check additionalProperties as a last resort
1090 if (!a_prop_or_pattern_matched && additionalProperties_) {
1091 first_error_handler additional_prop_err;
1092 additionalProperties_->validate(ptr / p.key(), p.value(), patch, additional_prop_err);
1093 if (additional_prop_err)
1094 e.error(ptr, instance, "validation failed for additional property '" + p.key() + "': " + additional_prop_err.message_);
1095 }
1096 }
1097
1098 // reverse search
1099 for (auto const &prop : properties_) {
1100 const auto finding = instance.find(prop.first);
1101 if (instance.end() == finding) { // if the prop is not in the instance
1102 const auto &default_value = prop.second->default_value(ptr, instance, e);
1103 if (!default_value.is_null()) { // if default value is available
1104 patch.add((ptr / prop.first), default_value);
1105 }
1106 }
1107 }
1108
1109 for (auto &dep : dependencies_) {
1110 auto prop = instance.find(dep.first);
1111 if (prop != instance.end()) // if dependency-property is present in instance
1112 dep.second->validate(ptr / dep.first, instance, patch, e); // validate
1113 }
1114 }

Member Data Documentation

◆ additionalProperties_

std::shared_ptr< schema > anonymous_namespace{json-validator.cpp}::object::additionalProperties_
private

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

Referenced by object(), and validate().

◆ dependencies_

std::map< std::string, std::shared_ptr< schema > > anonymous_namespace{json-validator.cpp}::object::dependencies_
private

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

Referenced by object(), and validate().

◆ maxProperties_

std::pair< bool, size_t > anonymous_namespace{json-validator.cpp}::object::maxProperties_ {false, 0}
private

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

1041{false, 0};

Referenced by object(), and validate().

◆ minProperties_

std::pair< bool, size_t > anonymous_namespace{json-validator.cpp}::object::minProperties_ {false, 0}
private

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

1042{false, 0};

Referenced by object(), and validate().

◆ patternProperties_

std::vector< std::pair< std::regex, std::shared_ptr< schema > > > anonymous_namespace{json-validator.cpp}::object::patternProperties_
private

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

Referenced by object(), and validate().

◆ properties_

std::map< std::string, std::shared_ptr< schema > > anonymous_namespace{json-validator.cpp}::object::properties_
private

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

Referenced by object(), and validate().

◆ propertyNames_

std::shared_ptr< schema > anonymous_namespace{json-validator.cpp}::object::propertyNames_
private

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

Referenced by object(), and validate().

◆ required_

std::vector< std::string > anonymous_namespace{json-validator.cpp}::object::required_
private

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

Referenced by object(), and validate().


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