diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 29f009b97b42056ec92f165e34273c227717303f..f8fbc9d2e8342fa9efdf62f6d9da368543b856fe 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -158,12 +158,13 @@ out: return rv; } -/* -bool schema_validator_validate(struct json_object *j_object, char *object, char *method, enum schema_call_t type) + +bool schema_validator_validate_jobj(struct json_object *j_object, char *object, char *method, enum schema_call_t type) { - struct json_object *definitions, *schema; struct schema_object *s_object; struct schema_method *s_method; + json obj, sch; + char *s; int rv = 1; s_object = schema_get_object_schema(object); @@ -177,76 +178,29 @@ bool schema_validator_validate(struct json_object *j_object, char *object, char goto out; if (type == SCHEMA_INPUT_CALL) - schema = json_tokener_parse(s_method->input); + s = blobmsg_format_json(s_method->b_input, true); else if (type == SCHEMA_OUTPUT_CALL) - schema = json_tokener_parse(s_method->output); + s = blobmsg_format_json(s_method->b_output, true); else goto out; - if (!schema) + if (!s) + goto out; + sch = json::parse(s); + + if (!sch) goto out; if (s_object->definitions) { - definitions = json_tokener_parse(s_object->definitions); - if (definitions) - json_object_object_add(schema, "definitions", definitions); + json definitions = json::parse(s_object->definitions); + sch += json::object_t::value_type("definitions", definitions); } - std::cout << "jobject" << json_object_get_string(j_object) << std::endl; - std::cout << "schema" << json_object_get_string(schema) << std::endl; + obj = json::parse(json_object_get_string(j_object)); - rv = json_object_validate_schema(j_object, schema); + rv = json_object_validate_schema(obj, sch); - json_object_put(schema); out: return rv; } -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(json obj, json sch) -{ - 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; -} diff --git a/src/schema.cpp b/src/schema.cpp index d52bb00ec3963e228de78b505a32a6637857307a..1cb593d78e7f96a965aeacf16aaea9a8dd672325 100644 --- a/src/schema.cpp +++ b/src/schema.cpp @@ -21,10 +21,11 @@ static void schema_flush_method(struct schema_method *s_method) { fprintf(stderr, "cleaning method %s\n", (char *)s_method->avl.key); - if (s_method->input) - free(s_method->input); - if (s_method->output) - free(s_method->output); + if (s_method->b_input) + free(s_method->b_input); + if (s_method->b_output) + free(s_method->b_output); + if (s_method->avl.key) free((void *) s_method->avl.key); @@ -97,12 +98,20 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob blobmsg_for_each_attr(method, properties, rem2) { //fprintf(stderr, "%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method)); if (!strncmp(blobmsg_name(method), "input", 6)) { - s_method->input = blobmsg_format_json(method, true); - //fprintf(stderr, "input = %s\n", s_method->input); + int len = blob_raw_len(method); + + s_method->b_input = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len); + memcpy(s_method->b_input, method, len); + + fprintf(stderr, "b_input = %s\n", blobmsg_format_json(s_method->b_input, true)); } else if (!strncmp(blobmsg_name(method), "output", 7)) { - s_method->output = blobmsg_format_json(method, true); - //fprintf(stderr, "output = %s\n", s_method->output); + int len = blob_raw_len(method); + + s_method->b_output = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len); + memcpy(s_method->b_output, method, len); + + fprintf(stderr, "b_output = %s\n", blobmsg_format_json(s_method->b_output, true)); } } } diff --git a/src/schema.h b/src/schema.h index 3fd15f5a1580cd3f474d1026ba6e61ee5fa44161..a2b6802ece46e5d1382704b324216b50c44f98e8 100644 --- a/src/schema.h +++ b/src/schema.h @@ -16,11 +16,8 @@ struct schema_context { struct schema_method { char method_name[METHOD_NAME_MAX_LEN]; - //struct blob_attr input; - //struct blob_attr output; - - char *input; - char *output; + struct blob_attr *b_input; + struct blob_attr *b_output; struct avl_node avl; };