mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-03-03 04:38:40 +01:00
Add example showing local file resolution
This commit is contained in:
parent
7a52fc88cd
commit
94cef2fa8d
@ -144,14 +144,18 @@ if(valijson_BUILD_EXAMPLES)
|
||||
examples/json_pointers.cpp
|
||||
)
|
||||
|
||||
add_executable(remote_resolution_local_file
|
||||
examples/remote_resolution_local_file.cpp
|
||||
)
|
||||
|
||||
if(curlpp_FOUND)
|
||||
include_directories(${curlpp_INCLUDE_DIR})
|
||||
|
||||
add_executable(remote_resolution
|
||||
examples/remote_resolution.cpp
|
||||
add_executable(remote_resolution_url
|
||||
examples/remote_resolution_url.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(remote_resolution curl ${curlpp_LIBRARIES})
|
||||
target_link_libraries(remote_resolution_url curl ${curlpp_LIBRARIES})
|
||||
endif()
|
||||
|
||||
target_link_libraries(custom_schema ${Boost_LIBRARIES})
|
||||
|
104
examples/remote_resolution_local_file.cpp
Normal file
104
examples/remote_resolution_local_file.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief Demonstrates resolution of remote JSON references using cURLpp
|
||||
*
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
#include <valijson/adapters/rapidjson_adapter.hpp>
|
||||
#include <valijson/utils/rapidjson_utils.hpp>
|
||||
#include <valijson/schema.hpp>
|
||||
#include <valijson/schema_parser.hpp>
|
||||
#include <valijson/validation_results.hpp>
|
||||
#include <valijson/validator.hpp>
|
||||
|
||||
using std::cerr;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
using valijson::Schema;
|
||||
using valijson::SchemaParser;
|
||||
using valijson::Validator;
|
||||
using valijson::ValidationResults;
|
||||
using valijson::adapters::RapidJsonAdapter;
|
||||
|
||||
const rapidjson::Document * fetchDocument(const std::string &uri)
|
||||
{
|
||||
cout << "Fetching " << uri << "..." << endl;
|
||||
rapidjson::Document *fetchedRoot = new rapidjson::Document();
|
||||
if (!valijson::utils::loadDocument(uri.substr(0, 7) == "file://" ? uri.substr(7, uri.size()) : uri, *fetchedRoot)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return fetchedRoot;
|
||||
}
|
||||
|
||||
void freeDocument(const rapidjson::Document *adapter)
|
||||
{
|
||||
delete adapter;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 3) {
|
||||
cerr << "Usage: " << argv[0] << " <schema document> <test/target document>" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Load the document containing the schema
|
||||
rapidjson::Document schemaDocument;
|
||||
if (!valijson::utils::loadDocument(argv[1], schemaDocument)) {
|
||||
cerr << "Failed to load schema document." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Load the document that is to be validated
|
||||
rapidjson::Document targetDocument;
|
||||
if (!valijson::utils::loadDocument(argv[2], targetDocument)) {
|
||||
cerr << "Failed to load target document." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Parse the json schema into an internal schema format
|
||||
Schema schema;
|
||||
SchemaParser parser;
|
||||
RapidJsonAdapter schemaDocumentAdapter(schemaDocument);
|
||||
try {
|
||||
parser.populateSchema(schemaDocumentAdapter, schema, fetchDocument, freeDocument);
|
||||
} catch (std::exception &e) {
|
||||
cerr << "Failed to parse schema: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Perform validation
|
||||
Validator validator(Validator::kWeakTypes);
|
||||
ValidationResults results;
|
||||
RapidJsonAdapter targetDocumentAdapter(targetDocument);
|
||||
if (!validator.validate(schema, targetDocumentAdapter, &results)) {
|
||||
std::cerr << "Validation failed." << endl;
|
||||
ValidationResults::Error error;
|
||||
unsigned int errorNum = 1;
|
||||
while (results.popError(error)) {
|
||||
|
||||
std::string context;
|
||||
std::vector<std::string>::iterator itr = error.context.begin();
|
||||
for (; itr != error.context.end(); itr++) {
|
||||
context += *itr;
|
||||
}
|
||||
|
||||
cerr << "Error #" << errorNum << std::endl
|
||||
<< " context: " << context << endl
|
||||
<< " desc: " << error.description << endl;
|
||||
++errorNum;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user