json-schema-validator/test/errors.cpp
Cristian Le 0d60d48a58
Modernize cmake script (#262)
* Remove travis file
* Apply pre-commit fixes
* Modernize cmake file

- Added JSON_VALIDATOR_SHARED_LIBS to properly handle shared-library
- Bumped minimum cmake to 3.11 to use no-source add_library
- Bumped minimum cmake to 3.14 to properly support FetchContent (FetchContent_MakeAvailable)
- Converted Hunter package manager to FetchContent (It is plenty mature these days)
- Added namespace to exported target
- Made the cmake file compatible with FetchContent

* Use simplified FetchContent CI
* Add simple status messages
* Handle nlohmann dependency

Not an ideal approach, but required in order for the exported target to have appropriate linkage.
Maybe this can be designed to become a PRIVATE link library, but then how does it ensure the target is installed?

* Remove CMake-install test

This will be moved to packaging integration tests

* Enable code coverage

* Reconfigure ci presets

Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
2023-05-11 12:07:56 +02:00

131 lines
3.6 KiB
C++

#include <nlohmann/json-schema.hpp>
#include <iostream>
static int error_count;
#define EXPECT_EQ(a, b) \
do { \
if (a != b) { \
std::cerr << "Failed: '" << a << "' != '" << b << "'\n"; \
error_count++; \
} \
} while (0)
using nlohmann::json;
using nlohmann::json_schema::json_validator;
namespace
{
// The schema is defined based upon a string literal
static json person_schema = R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "A person",
"properties": {
"name": {
"description": "Name",
"type": "string"
},
"age": {
"description": "Age of the person",
"type": "number",
"minimum": 2,
"maximum": 200
},
"phones": {
"type": "array",
"items": {
"type": "number"
}
}
},
"required": [
"name",
"age"
],
"additionalProperties": false,
"type": "object"
})"_json;
class store_ptr_err_handler : public nlohmann::json_schema::basic_error_handler
{
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
{
nlohmann::json_schema::basic_error_handler::error(ptr, instance, message);
std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n";
failed_pointers.push_back(ptr);
}
public:
std::vector<nlohmann::json::json_pointer> failed_pointers;
void reset() override
{
nlohmann::json_schema::basic_error_handler::reset();
failed_pointers.clear();
}
};
} // namespace
int main(void)
{
json_validator validator;
try {
validator.set_root_schema(person_schema); // insert root-schema
} catch (const std::exception &e) {
std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
return EXIT_FAILURE;
}
store_ptr_err_handler err;
validator.validate({{"age", 42}, {"name", "John"}}, err); // OK
EXPECT_EQ(err.failed_pointers.size(), 0);
err.reset();
validator.validate({{"age", 42}}, err); // no name
EXPECT_EQ(err.failed_pointers.size(), 1);
EXPECT_EQ(err.failed_pointers[0].to_string(), "");
err.reset();
validator.validate({{"street", "Boulevard"}}, err); // no name and no age
EXPECT_EQ(err.failed_pointers.size(), 3);
EXPECT_EQ(err.failed_pointers[0].to_string(), "");
EXPECT_EQ(err.failed_pointers[1].to_string(), "");
EXPECT_EQ(err.failed_pointers[2].to_string(), "");
err.reset();
validator.validate({{"age", 42}, {"name", 12}}, err); // name must be a string
EXPECT_EQ(err.failed_pointers.size(), 1);
EXPECT_EQ(err.failed_pointers[0].to_string(), "/name");
err.reset();
validator.validate({
{"age", 42},
{"name", "John"},
{"phones", {1234, "223"}},
},
err); // name must be a string
EXPECT_EQ(err.failed_pointers.size(), 1);
EXPECT_EQ(err.failed_pointers[0].to_string(), "/phones/1");
err.reset();
validator.validate({
{"age", 42},
{"name", "John"},
{"phones", {0}},
{"post-code", 12345},
},
err); // name must be a string
EXPECT_EQ(err.failed_pointers.size(), 1);
EXPECT_EQ(err.failed_pointers[0].to_string(), "");
err.reset();
return error_count;
}