Skip to content
Snippets Groups Projects
Commit ac72687a authored by Jakob Olsson's avatar Jakob Olsson
Browse files

add json-validator source...

parent 29304388
Branches
No related tags found
No related merge requests found
#include <iostream>
#include <iomanip>
#include <stdbool.h>
#include <libubox/avl.h>
#include <libubox/avl-cmp.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubox/utils.h>
#include <libubus.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>
#include <json-schema.hpp>
#include "schema.h"
#include "json-validator.h"
using nlohmann::json;
using nlohmann::json_uri;
using nlohmann::json_schema::json_validator;
int schema_validator_destroy(void)
{
schema_flush_objects();
return 0;
}
int schema_validator_init(void)
{
schema_setup_json();
return 0;
}
bool json_object_validate_schema_inject_definitions(struct json_object *j_object, struct json_object *definitions, struct json_object *j_schema)
{
json_object_object_add(j_object, "definitions", definitions);
return json_object_validate_schema(j_object, j_schema);
}
bool json_object_validate_schema(struct json_object *j_object, struct json_object *j_schema)
{
std::cout << "hello" << std::endl;
const char *sch_str, *obj_str;
obj_str = json_object_get_string(j_object);
sch_str = json_object_get_string(j_schema);
std::cout << "obj_str " << obj_str << std::endl;
std::cout << "sch_str " << sch_str << std::endl;
json sch = json::parse(sch_str);
json obj = json::parse(obj_str);
/* json-parse the schema */
json_validator validator; // create validator
try {
validator.set_root_schema(sch); // insert root-schema
} catch (const std::exception &e) {
std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
return 0;
}
/* json-parse the people - with custom error handler */
class custom_error_handler : public nlohmann::json_schema::basic_error_handler
{
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
{
nlohmann::json_schema::basic_error_handler::error(ptr, instance, message);
std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n";
}
};
custom_error_handler err;
validator.validate(obj, err); // validate the document
if (err)
return 0;
return 1;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment