Add a runnable and useful exampe
This commit is contained in:
parent
ad9e9923ab
commit
aa61d3933e
69
README.md
69
README.md
@ -72,45 +72,72 @@ add_subdirectory(path-to-this-project json-schema-validator)
|
|||||||
See also `app/json-schema-validate.cpp`.
|
See also `app/json-schema-validate.cpp`.
|
||||||
|
|
||||||
```C++
|
```C++
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "json-schema.hpp"
|
#include "json-schema.hpp"
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
using nlohmann::json_uri;
|
using nlohmann::json_uri;
|
||||||
using nlohmann::json_schema_draft4::json_validator;
|
using nlohmann::json_schema_draft4::json_validator;
|
||||||
|
|
||||||
static void loader(const json_uri &uri, json &schema)
|
// The schema is defined based upon a string literal
|
||||||
|
static json person_schema = R"(
|
||||||
{
|
{
|
||||||
// get the schema from uri and feed it into schema
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
// if not possible, otherwise, throw an excpetion
|
"title": "A person",
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"description": "Name",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"age": {
|
||||||
|
"description": "Age of the person",
|
||||||
|
"type": "number",
|
||||||
|
"minimum": 2,
|
||||||
|
"maximum": 200
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"name",
|
||||||
|
"age"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
)"_json;
|
||||||
{
|
|
||||||
json schema;
|
|
||||||
|
|
||||||
|
// The people are defined with brace initialization
|
||||||
|
static json bad_person = {{"age", 42}};
|
||||||
|
static json good_person = {{"name", "Albert"}, {"age", 42}};
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
/* json-parse the schema */
|
/* json-parse the schema */
|
||||||
|
|
||||||
json_validator validator(loader); // create validator with a loader-callback
|
json_validator validator; // create validator
|
||||||
|
|
||||||
try {
|
try {
|
||||||
validator.set_root_schema(schema); // insert root-schema
|
validator.set_root_schema(person_schema); // insert root-schema
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
std::cerr << "Validation failed, here is why: " << e.what() << "\n";
|
std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
json document;
|
/* json-parse the people */
|
||||||
|
|
||||||
/* json-parse the document */
|
for (auto &person : {bad_person, good_person})
|
||||||
|
{
|
||||||
try {
|
std::cout << "About to validate this person:\n" << std::setw(2) << person << std::endl;
|
||||||
validator.validate(document); // validate the document
|
try {
|
||||||
} catch (const std::exception &e) {
|
validator.validate(person); // validate the document
|
||||||
std::cerr << "Validation failed, here is why: " << e.what() << "\n";
|
std::cout << "Validation succeeded\n";
|
||||||
return EXIT_FAILURE;
|
} catch (const std::exception &e) {
|
||||||
|
std::cerr << "Validation failed, here is why: " << e.what() << "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Compliance
|
# Compliance
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user