Created
March 23, 2018 22:31
-
-
Save foxtacles/0deaa03be09cf9820e49cc590ee020c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unordered_map> | |
#include <rapidjson/schema.h> | |
typedef std::unordered_map<std::string, rapidjson::SchemaDocument> SchemaCollection; | |
class SchemaDocumentProvider : public rapidjson::IRemoteSchemaDocumentProvider { | |
SchemaCollection* schema_collection; | |
public: | |
SchemaDocumentProvider(SchemaCollection* schema_collection) : schema_collection(schema_collection) { } | |
virtual const rapidjson::SchemaDocument* GetRemoteDocument(const char* uri, rapidjson::SizeType length) { | |
std::string s(uri, length); | |
return &schema_collection->find(s)->second; | |
} | |
}; | |
struct SchemaManager { | |
SchemaCollection collection; | |
SchemaDocumentProvider provider; | |
SchemaManager() : provider(&collection) { } | |
}; | |
int main() { | |
const char* x = R"({"required":["country"],"properties":{"country":{"$ref":"y.json#/definitions/country"}},"type":"object"})"; | |
const char* y = R"({"definitions":{"country":{"$ref":"z.json#/definitions/country_list"}}})"; | |
const char* z = R"({"definitions":{"country_list":{"enum":["US"]}}})"; | |
const char* content = R"({"country":"UK"})"; | |
SchemaManager schema_manager; | |
rapidjson::Document document_z; | |
document_z.Parse(z, strlen(z)); | |
rapidjson::SchemaDocument schema_z(document_z, nullptr, 0, &schema_manager.provider); | |
schema_manager.collection.emplace("z.json", std::move(schema_z)); | |
rapidjson::Document document_y; | |
document_y.Parse(y, strlen(y)); | |
rapidjson::SchemaDocument schema_y(document_y, nullptr, 0, &schema_manager.provider); | |
schema_manager.collection.emplace("y.json", std::move(schema_y)); | |
rapidjson::Document document_x; | |
document_x.Parse(x, strlen(x)); | |
rapidjson::SchemaDocument schema_x(document_x, nullptr, 0, &schema_manager.provider); | |
schema_manager.collection.emplace("x.json", std::move(schema_x)); | |
rapidjson::Document document; | |
document.Parse(content, strlen(content)); | |
rapidjson::SchemaValidator validator(schema_x); | |
if (document.Accept(validator)) | |
fprintf(stderr, "valid"); | |
else | |
fprintf(stderr, "invalid"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment