int main(int argc, char *argv[]) { // Load the document that is to be validated rapidjson::Document targetDocument; if (!valijson::utils::loadDocument(argv[1], targetDocument)) { cerr << "Failed to load target document." << endl; return 1; } // Populate a schema Schema schema; addPropertiesConstraint(schema); addRequiredConstraint(schema); addTypeConstraint(schema); // Perform validation Validator validator(schema); ValidationResults results; RapidJsonAdapter targetDocumentAdapter(targetDocument); if (!validator.validate(targetDocumentAdapter, &results)) { std::cerr << "Validation failed." << endl; ValidationResults::Error error; unsigned int errorNum = 1; while (results.popError(error)) { cerr << "Error #" << errorNum << std::endl << " context: " << error.context << endl << " desc: " << error.description << endl; ++errorNum; } return 1; } return 0; }
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); } catch (std::exception &e) { cerr << "Failed to parse schema: " << e.what() << endl; return 1; } // Perform validation Validator validator(schema); validator.setStrict(false); ValidationResults results; RapidJsonAdapter targetDocumentAdapter(targetDocument); if (!validator.validate(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; }
TEST_F(TestValidationErrors, AllOfConstraintFailure) { // Load schema document rapidjson::Document schemaDocument; ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/schemas/allof_integers_and_numbers.schema.json", schemaDocument) ); RapidJsonAdapter schemaAdapter(schemaDocument); // Parse schema document Schema schema; SchemaParser schemaParser; ASSERT_NO_THROW( schemaParser.populateSchema(schemaAdapter, schema) ); // Load test document rapidjson::Document testDocument; ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/documents/array_doubles_1_2_3.json", testDocument) ); RapidJsonAdapter testAdapter(testDocument); Validator validator; ValidationResults results; EXPECT_FALSE( validator.validate(schema, testAdapter, &results) ); ValidationResults::Error error; EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(2), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "[0]", error.context[1] ); EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(1), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "Failed to validate item #0 in array.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(2), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "[1]", error.context[1] ); EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(1), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "Failed to validate item #1 in array.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(2), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "[2]", error.context[1] ); EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(1), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "Failed to validate item #2 in array.", error.description ); EXPECT_TRUE( results.popError(error) ); EXPECT_EQ( size_t(1), error.context.size() ); EXPECT_EQ( "<root>", error.context[0] ); EXPECT_EQ( "Failed to validate against child schema #0.", error.description ); EXPECT_FALSE( results.popError(error) ); while (results.popError(error)) { //std::cerr << error.context << std::endl; std::cerr << error.description << std::endl; } }