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
016c33e7
Commit
016c33e7
authored
5 years ago
by
Jakob Olsson
Browse files
Options
Downloads
Patches
Plain Diff
refactor to store blobs in memory
parent
759510c2
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/json-validator.cpp
+15
-61
15 additions, 61 deletions
src/json-validator.cpp
src/schema.cpp
+17
-8
17 additions, 8 deletions
src/schema.cpp
src/schema.h
+2
-5
2 additions, 5 deletions
src/schema.h
with
34 additions
and
74 deletions
src/json-validator.cpp
+
15
−
61
View file @
016c33e7
...
@@ -158,12 +158,13 @@ out:
...
@@ -158,12 +158,13 @@ out:
return
rv
;
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_object
*
s_object
;
struct
schema_method
*
s_method
;
struct
schema_method
*
s_method
;
json
obj
,
sch
;
char
*
s
;
int
rv
=
1
;
int
rv
=
1
;
s_object
=
schema_get_object_schema
(
object
);
s_object
=
schema_get_object_schema
(
object
);
...
@@ -177,76 +178,29 @@ bool schema_validator_validate(struct json_object *j_object, char *object, char
...
@@ -177,76 +178,29 @@ bool schema_validator_validate(struct json_object *j_object, char *object, char
goto
out
;
goto
out
;
if
(
type
==
SCHEMA_INPUT_CALL
)
if
(
type
==
SCHEMA_INPUT_CALL
)
s
chema = json_tokener_parse
(s_method->input);
s
=
blobmsg_format_json
(
s_method
->
b_
input
,
true
);
else
if
(
type
==
SCHEMA_OUTPUT_CALL
)
else
if
(
type
==
SCHEMA_OUTPUT_CALL
)
s
chema = json_tokener_parse
(s_method->output);
s
=
blobmsg_format_json
(
s_method
->
b_
output
,
true
);
else
else
goto
out
;
goto
out
;
if (!schema)
if
(
!
s
)
goto
out
;
sch
=
json
::
parse
(
s
);
if
(
!
sch
)
goto
out
;
goto
out
;
if
(
s_object
->
definitions
)
{
if
(
s_object
->
definitions
)
{
definitions = json_tokener_parse(s_object->definitions);
json
definitions
=
json
::
parse
(
s_object
->
definitions
);
if (definitions)
sch
+=
json
::
object_t
::
value_type
(
"definitions"
,
definitions
);
json_object_object_add(schema, "definitions", definitions);
}
}
std::cout << "jobject" << json_object_get_string(j_object) << std::endl;
obj
=
json
::
parse
(
json_object_get_string
(
j_object
));
std::cout << "schema" << json_object_get_string(schema) << std::endl;
rv = json_object_validate_schema(
j_
obj
ect
, sch
ema
);
rv
=
json_object_validate_schema
(
obj
,
sch
);
json_object_put(schema);
out
:
out
:
return
rv
;
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
;
}
This diff is collapsed.
Click to expand it.
src/schema.cpp
+
17
−
8
View file @
016c33e7
...
@@ -21,10 +21,11 @@ static void schema_flush_method(struct schema_method *s_method)
...
@@ -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
);
fprintf
(
stderr
,
"cleaning method %s
\n
"
,
(
char
*
)
s_method
->
avl
.
key
);
if
(
s_method
->
input
)
if
(
s_method
->
b_input
)
free
(
s_method
->
input
);
free
(
s_method
->
b_input
);
if
(
s_method
->
output
)
if
(
s_method
->
b_output
)
free
(
s_method
->
output
);
free
(
s_method
->
b_output
);
if
(
s_method
->
avl
.
key
)
if
(
s_method
->
avl
.
key
)
free
((
void
*
)
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
...
@@ -97,12 +98,20 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
blobmsg_for_each_attr
(
method
,
properties
,
rem2
)
{
blobmsg_for_each_attr
(
method
,
properties
,
rem2
)
{
//fprintf(stderr, "%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method));
//fprintf(stderr, "%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method));
if
(
!
strncmp
(
blobmsg_name
(
method
),
"input"
,
6
))
{
if
(
!
strncmp
(
blobmsg_name
(
method
),
"input"
,
6
))
{
s_method
->
input
=
blobmsg_format_json
(
method
,
true
);
int
len
=
blob_raw_len
(
method
);
//fprintf(stderr, "input = %s\n", s_method->input);
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
))
{
else
if
(
!
strncmp
(
blobmsg_name
(
method
),
"output"
,
7
))
{
s_method
->
output
=
blobmsg_format_json
(
method
,
true
);
int
len
=
blob_raw_len
(
method
);
//fprintf(stderr, "output = %s\n", s_method->output);
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
));
}
}
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/schema.h
+
2
−
5
View file @
016c33e7
...
@@ -16,11 +16,8 @@ struct schema_context {
...
@@ -16,11 +16,8 @@ struct schema_context {
struct
schema_method
{
struct
schema_method
{
char
method_name
[
METHOD_NAME_MAX_LEN
];
char
method_name
[
METHOD_NAME_MAX_LEN
];
//struct blob_attr input;
struct
blob_attr
*
b_input
;
//struct blob_attr output;
struct
blob_attr
*
b_output
;
char
*
input
;
char
*
output
;
struct
avl_node
avl
;
struct
avl_node
avl
;
};
};
...
...
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