2013-10-29 21:51:11 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @brief Demonstrates validation against a schema loaded from a file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#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::endl;
|
|
|
|
|
|
|
|
using valijson::Schema;
|
|
|
|
using valijson::SchemaParser;
|
|
|
|
using valijson::Validator;
|
|
|
|
using valijson::ValidationResults;
|
|
|
|
using valijson::adapters::RapidJsonAdapter;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-06-15 07:24:26 +02:00
|
|
|
if (argc != 3) {
|
|
|
|
cerr << "Usage: " << argv[0] << " <schema document> <test/target document>" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:51:11 +01:00
|
|
|
// 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);
|
2015-10-13 04:04:14 +02:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
cerr << "Failed to parse schema: " << e.what() << endl;
|
2013-10-29 21:51:11 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform validation
|
2021-11-05 22:53:39 +01:00
|
|
|
Validator validator(Validator::kStrongTypes);
|
2013-10-29 21:51:11 +01:00
|
|
|
ValidationResults results;
|
|
|
|
RapidJsonAdapter targetDocumentAdapter(targetDocument);
|
2015-12-23 04:34:04 +01:00
|
|
|
if (!validator.validate(schema, targetDocumentAdapter, &results)) {
|
2013-10-29 21:51:11 +01:00
|
|
|
std::cerr << "Validation failed." << endl;
|
|
|
|
ValidationResults::Error error;
|
|
|
|
unsigned int errorNum = 1;
|
|
|
|
while (results.popError(error)) {
|
2021-01-24 04:06:15 +01:00
|
|
|
|
2014-06-15 14:09:38 +02:00
|
|
|
std::string context;
|
|
|
|
std::vector<std::string>::iterator itr = error.context.begin();
|
|
|
|
for (; itr != error.context.end(); itr++) {
|
|
|
|
context += *itr;
|
|
|
|
}
|
2021-01-24 04:06:15 +01:00
|
|
|
|
2013-10-29 21:51:11 +01:00
|
|
|
cerr << "Error #" << errorNum << std::endl
|
2014-06-15 14:09:38 +02:00
|
|
|
<< " context: " << context << endl
|
2013-10-29 21:51:11 +01:00
|
|
|
<< " desc: " << error.description << endl;
|
|
|
|
++errorNum;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-06-15 07:24:26 +02:00
|
|
|
}
|