MCPcopy Create free account
hub / github.com/pboettch/json-schema-validator / validate

Method validate

src/json-validator.cpp:1212–1265  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1210 std::shared_ptr<schema> contains_;
1211
1212 void validate(const json::json_pointer &ptr, const json &instance, json_patch &patch, error_handler &e) const override
1213 {
1214 if (maxItems_.first && instance.size() > maxItems_.second)
1215 e.error(ptr, instance, "array has too many items");
1216
1217 if (minItems_.first && instance.size() < minItems_.second)
1218 e.error(ptr, instance, "array has too few items");
1219
1220 if (uniqueItems_) {
1221 for (auto it = instance.cbegin(); it != instance.cend(); ++it) {
1222 auto v = std::find(it + 1, instance.end(), *it);
1223 if (v != instance.end())
1224 e.error(ptr, instance, "items have to be unique for this array");
1225 }
1226 }
1227
1228 size_t index = 0;
1229 if (items_schema_)
1230 for (auto &i : instance) {
1231 items_schema_->validate(ptr / index, i, patch, e);
1232 index++;
1233 }
1234 else {
1235 auto item = items_.cbegin();
1236 for (auto &i : instance) {
1237 std::shared_ptr<schema> item_validator;
1238 if (item == items_.cend())
1239 item_validator = additionalItems_;
1240 else {
1241 item_validator = *item;
1242 item++;
1243 }
1244
1245 if (!item_validator)
1246 break;
1247
1248 item_validator->validate(ptr / index, i, patch, e);
1249 }
1250 }
1251
1252 if (contains_) {
1253 bool contained = false;
1254 for (auto &item : instance) {
1255 first_error_handler local_e;
1256 contains_->validate(ptr, item, patch, local_e);
1257 if (!local_e) {
1258 contained = true;
1259 break;
1260 }
1261 }
1262 if (!contained)
1263 e.error(ptr, instance, "array does not contain required element as per 'contains'");
1264 }
1265 }
1266
1267public:
1268 array(json &sch, root_schema *root, const std::vector<nlohmann::json_uri> &uris)

Callers

nothing calls this directly

Calls 2

errorMethod · 0.45
validateMethod · 0.45

Tested by

no test coverage detected