Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
json-schema-validator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IOPSYS
json-schema-validator
Commits
0daab4a8
Commit
0daab4a8
authored
5 years ago
by
Jakob Olsson
Browse files
Options
Downloads
Patches
Plain Diff
wip validator accepting blob_attr as args
parent
e693dd13
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/json-validator.cpp
+125
-5
125 additions, 5 deletions
src/json-validator.cpp
src/json-validator.h
+9
-3
9 additions, 3 deletions
src/json-validator.h
with
134 additions
and
8 deletions
src/json-validator.cpp
+
125
−
5
View file @
0daab4a8
...
@@ -28,6 +28,8 @@ using nlohmann::json;
...
@@ -28,6 +28,8 @@ using nlohmann::json;
using
nlohmann
::
json_uri
;
using
nlohmann
::
json_uri
;
using
nlohmann
::
json_schema
::
json_validator
;
using
nlohmann
::
json_schema
::
json_validator
;
bool
json_object_validate_schema
(
nlohmann
::
json
obj
,
nlohmann
::
json
sch
);
int
schema_validator_destroy
(
void
)
int
schema_validator_destroy
(
void
)
{
{
schema_flush_objects
();
schema_flush_objects
();
...
@@ -42,6 +44,121 @@ int schema_validator_init(void)
...
@@ -42,6 +44,121 @@ int schema_validator_init(void)
return
0
;
return
0
;
}
}
/*
{"jsonrpc":"2.0","id":2,"result":[0,{"netmode":{"repeat":{"network":"string","wireless":"object","connect_bssid":"string"},"repeat_wifilife":{"wifilife":"object"},"sync":{},"sync_wifilife":{}},...}
*/
/*
bool schema_validator_validate_rpcd(struct json_object *in, enum schema_call_t type)
{
char *obj, *method;
struct json_object *type, *params, *j_obj, *j_method, *args;
if (type == SCHEMA_INPUT_CALL) {
// TODO: what should be 'failure' policy?
if (!json_object_object_get_ex(in, "method", &type))
return 1;
if (strncmp(json_object_get_string(type), "call", 5) != 0)
return 1;
if (!json_object_object_get_ex(in, "params", ¶ms))
return 1;
object = json_object_array_get_idx(params, 1);
if (!object)
return 0;
obj = json_object_get_string(object);
if (!obj)
return 0;
j_method = json_object_array_get_idx(params, 2);
if (!j_method)
return 0;
method = json_object_get_string(j_method)
if (!method)
return 0;
args = json_object_array_get_idx(params, 3);
if (!args)
return 0;
} else if (type == SCHEMA_OUTPUT_CALL) {
if (!json_object_object_get_ex(in, "method", &type))
return 1;
if (strncmp(json_object_get_string(type), "call", 5) != 0)
return 1;
if (!json_object_object_get_ex(in, "params", ¶ms))
return 1;
object = json_object_array_get_idx(params, 1);
if (!object)
return 0;
obj = json_object_get_string(object);
if (!obj)
return 0;
j_method = json_object_array_get_idx(params, 2);
if (!j_method)
return 0;
method = json_object_get_string(j_method)
if (!method)
return 0;
args = json_object_array_get_idx(params, 3);
if (!args)
return 0;
}
rv = schema_validator_validate(in, obj, , SCHEMA_INPUT_CALL);
}
*/
bool
schema_validator_validate_blob
(
struct
blob_attr
*
msg
,
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
schema
,
definitions
,
obj
;
int
rv
=
1
;
char
*
str
;
s_object
=
schema_get_object_schema
(
object
);
if
(
!
s_object
)
return
rv
;
rv
=
0
;
s_method
=
schema_get_method_schema
(
object
,
method
);
if
(
!
s_method
)
goto
out
;
if
(
type
==
SCHEMA_INPUT_CALL
)
schema
=
json
::
parse
(
s_method
->
input
);
//schema = json_tokener_parse(s_method->input);
else
if
(
type
==
SCHEMA_OUTPUT_CALL
)
schema
=
json
::
parse
(
s_method
->
output
);
//schema = json_tokener_parse(s_method->output);
else
goto
out
;
if
(
!
schema
)
goto
out
;
str
=
blobmsg_format_json
(
msg
,
true
);
obj
=
json
::
parse
(
str
);
if
(
s_object
->
definitions
)
{
//definitions = json_tokener_parse(s_object->definitions);
definitions
=
json
::
parse
(
s_object
->
definitions
);
if
(
definitions
)
//json_object_object_add(schema, "definitions", definitions);
obj
+=
definitions
;
}
/*std::cout << "jobject" << json_object_get_string(j_object) << std::endl;
std::cout << "schema" << json_object_get_string(schema) << std::endl;*/
rv
=
json_object_validate_schema
(
obj
,
schema
);
//json_object_put(schema);
free
(
str
);
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(struct json_object *j_object, char *object, char *method, enum schema_call_t type)
{
{
struct json_object *definitions, *schema;
struct json_object *definitions, *schema;
...
@@ -75,6 +192,9 @@ bool schema_validator_validate(struct json_object *j_object, char *object, char
...
@@ -75,6 +192,9 @@ bool schema_validator_validate(struct json_object *j_object, char *object, char
json_object_object_add(schema, "definitions", definitions);
json_object_object_add(schema, "definitions", definitions);
}
}
std::cout << "jobject" << json_object_get_string(j_object) << std::endl;
std::cout << "schema" << json_object_get_string(schema) << std::endl;
rv = json_object_validate_schema(j_object, schema);
rv = json_object_validate_schema(j_object, schema);
json_object_put(schema);
json_object_put(schema);
...
@@ -86,12 +206,12 @@ bool json_object_validate_schema_inject_definitions(struct json_object *j_object
...
@@ -86,12 +206,12 @@ bool json_object_validate_schema_inject_definitions(struct json_object *j_object
{
{
json_object_object_add(j_object, "definitions", definitions);
json_object_object_add(j_object, "definitions", definitions);
return json_object_validate_schema(j_object, j_schema);
return json_object_validate_schema(j_object, j_schema);
}
}
*/
bool
json_object_validate_schema
(
struct
json_object
*
j_object
,
struct
json_object
*
j_schema
)
bool
json_object_validate_schema
(
json
obj
,
json
sch
)
{
{
std
::
cout
<<
"hello"
<<
std
::
endl
;
std
::
cout
<<
"hello"
<<
std
::
endl
;
const
char
*
sch_str
,
*
obj_str
;
/*
const char *sch_str, *obj_str;
obj_str = json_object_get_string(j_object);
obj_str = json_object_get_string(j_object);
sch_str = json_object_get_string(j_schema);
sch_str = json_object_get_string(j_schema);
...
@@ -100,7 +220,7 @@ bool json_object_validate_schema(struct json_object *j_object, struct json_objec
...
@@ -100,7 +220,7 @@ bool json_object_validate_schema(struct json_object *j_object, struct json_objec
std::cout << "sch_str " << sch_str << std::endl;
std::cout << "sch_str " << sch_str << std::endl;
json sch = json::parse(sch_str);
json sch = json::parse(sch_str);
json
obj
=
json
::
parse
(
obj_str
);
json obj = json::parse(obj_str);
*/
/* json-parse the schema */
/* json-parse the schema */
...
@@ -129,4 +249,4 @@ bool json_object_validate_schema(struct json_object *j_object, struct json_objec
...
@@ -129,4 +249,4 @@ bool json_object_validate_schema(struct json_object *j_object, struct json_objec
if
(
err
)
if
(
err
)
return
0
;
return
0
;
return
1
;
return
1
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/json-validator.h
+
9
−
3
View file @
0daab4a8
...
@@ -5,6 +5,8 @@ extern "C"
...
@@ -5,6 +5,8 @@ extern "C"
{
{
#endif
#endif
#include
<stdbool.h>
enum
schema_call_t
{
enum
schema_call_t
{
SCHEMA_INPUT_CALL
,
SCHEMA_INPUT_CALL
,
SCHEMA_OUTPUT_CALL
SCHEMA_OUTPUT_CALL
...
@@ -12,9 +14,13 @@ enum schema_call_t {
...
@@ -12,9 +14,13 @@ enum schema_call_t {
int
schema_validator_destroy
(
void
);
int
schema_validator_destroy
(
void
);
int
schema_validator_init
(
void
);
int
schema_validator_init
(
void
);
bool
schema_validator_validate
(
struct
json_object
*
j_object
,
char
*
object
,
char
*
method
,
enum
schema_call_t
type
);
bool
schema_validator_validate
(
struct
json_object
*
j_object
,
const
char
*
object
,
const
char
*
method
,
enum
schema_call_t
type
);
bool
json_object_validate_schema_inject_definitions
(
struct
json_object
*
j_object
,
struct
json_object
*
definitions
,
struct
json_object
*
j_schema
);
bool
json_object_validate_schema
(
struct
json_object
*
j_obj
,
struct
json_object
*
j_schema
);
//bool schema_validator_validate(struct json_object *j_object, char *object, char *method, enum schema_call_t type);
//bool json_object_validate_schema_inject_definitions(struct json_object *j_object, struct json_object *definitions, struct json_object *j_schema);
//bool json_object_validate_schema(struct json_object *j_obj, struct json_object *j_schema);
//bool json_object_validate_schema(nlohmann::json obj, nlohmann::json sch);
bool
schema_validator_validate_blob
(
struct
blob_attr
*
msg
,
const
char
*
object
,
const
char
*
method
,
enum
schema_call_t
type
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
#endif
#endif
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment