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
f343ca98
Commit
f343ca98
authored
5 years ago
by
Jakob Olsson
Browse files
Options
Downloads
Patches
Plain Diff
add debug prints and debug flag JSON_VALIDATOR_DEBUG
parent
f120a612
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CMakeLists.txt
+5
-0
5 additions, 0 deletions
CMakeLists.txt
src/json-validator.cpp
+5
-2
5 additions, 2 deletions
src/json-validator.cpp
src/schema.cpp
+19
-21
19 additions, 21 deletions
src/schema.cpp
with
29 additions
and
23 deletions
CMakeLists.txt
+
5
−
0
View file @
f343ca98
...
@@ -64,6 +64,7 @@ INTERFACE
...
@@ -64,6 +64,7 @@ INTERFACE
# and one for the validator
# and one for the validator
add_library
(
json-validator
add_library
(
json-validator
SHARED
SHARED
src/debug.cpp
src/json-validator.cpp
src/json-validator.cpp
src/schema.cpp
src/schema.cpp
)
)
...
@@ -108,6 +109,10 @@ target_link_libraries(json-validator
...
@@ -108,6 +109,10 @@ target_link_libraries(json-validator
PUBLIC
PUBLIC
libubox
)
libubox
)
IF
(
JSON_VALIDATOR_DEBUG
)
set
(
CMAKE_CXX_FLAGS_DEBUG
"-DJSON_VALIDATOR_DEBUG"
)
ENDIF
()
#testing
#testing
IF
(
CMAKE_BUILD_TYPE STREQUAL Debug
)
IF
(
CMAKE_BUILD_TYPE STREQUAL Debug
)
OPTION
(
ENABLE_BUILD_TESTS
"Build tests"
ON
)
OPTION
(
ENABLE_BUILD_TESTS
"Build tests"
ON
)
...
...
This diff is collapsed.
Click to expand it.
src/json-validator.cpp
+
5
−
2
View file @
f343ca98
...
@@ -17,9 +17,11 @@ extern "C"
...
@@ -17,9 +17,11 @@ extern "C"
#include
<stdbool.h>
#include
<stdbool.h>
#include
<json-c/json.h>
#include
<json-c/json.h>
}
#include
"debug.h"
#include
"schema.h"
#include
"schema.h"
#include
"json-validator.h"
#include
"json-validator.h"
}
using
nlohmann
::
json
;
using
nlohmann
::
json
;
using
nlohmann
::
json_uri
;
using
nlohmann
::
json_uri
;
...
@@ -135,7 +137,6 @@ out:
...
@@ -135,7 +137,6 @@ out:
return
rv
;
return
rv
;
}
}
bool
schema_validator_validate_jobj
(
struct
json_object
*
j_object
,
const
char
*
object
,
const
char
*
method
,
enum
schema_call_t
type
)
bool
schema_validator_validate_jobj
(
struct
json_object
*
j_object
,
const
char
*
object
,
const
char
*
method
,
enum
schema_call_t
type
)
{
{
struct
schema_object
*
s_object
;
struct
schema_object
*
s_object
;
...
@@ -180,6 +181,8 @@ bool schema_validator_validate_jobj(struct json_object *j_object, const char *ob
...
@@ -180,6 +181,8 @@ bool schema_validator_validate_jobj(struct json_object *j_object, const char *ob
obj
=
json
::
parse
(
json_object_get_string
(
j_object
));
obj
=
json
::
parse
(
json_object_get_string
(
j_object
));
dbg
(
"validation object -- %s
\n
"
,
obj
.
dump
().
c_str
());
dbg
(
"validation schema -- %s
\n
"
,
sch
.
dump
().
c_str
());
rv
=
json_object_validate_schema
(
obj
,
sch
);
rv
=
json_object_validate_schema
(
obj
,
sch
);
out_definitions
:
out_definitions
:
...
...
This diff is collapsed.
Click to expand it.
src/schema.cpp
+
19
−
21
View file @
f343ca98
...
@@ -11,6 +11,7 @@ extern "C"
...
@@ -11,6 +11,7 @@ extern "C"
#include
<stdlib.h>
#include
<stdlib.h>
#include
<regex.h>
#include
<regex.h>
}
}
#include
"debug.h"
#include
"schema.h"
#include
"schema.h"
#define JSON_SCHEMA_DIR "/usr/share/rpcd/schemas"
#define JSON_SCHEMA_DIR "/usr/share/rpcd/schemas"
...
@@ -19,7 +20,7 @@ struct schema_context s_ctx;
...
@@ -19,7 +20,7 @@ struct schema_context s_ctx;
static
void
schema_flush_method
(
struct
schema_method
*
s_method
)
static
void
schema_flush_method
(
struct
schema_method
*
s_method
)
{
{
//fprintf(stderr,
"cleaning method %s\n", (char *)s_method->avl.key);
dbg
(
"cleaning method %s
\n
"
,
(
char
*
)
s_method
->
avl
.
key
);
if
(
s_method
->
b_input
)
if
(
s_method
->
b_input
)
free
(
s_method
->
b_input
);
free
(
s_method
->
b_input
);
...
@@ -37,7 +38,7 @@ static void schema_flush_methods(struct schema_object *s_object)
...
@@ -37,7 +38,7 @@ static void schema_flush_methods(struct schema_object *s_object)
{
{
struct
schema_method
*
s_method
,
*
tmp
;
struct
schema_method
*
s_method
,
*
tmp
;
//fprintf(stderr,
"flushing all methods of object %s\n", (char *)s_object->avl.key);
dbg
(
"flushing all methods of object %s
\n
"
,
(
char
*
)
s_object
->
avl
.
key
);
avl_for_each_element_safe
(
&
s_object
->
schema_methods
,
s_method
,
avl
,
tmp
)
{
avl_for_each_element_safe
(
&
s_object
->
schema_methods
,
s_method
,
avl
,
tmp
)
{
avl_delete
(
&
s_object
->
schema_methods
,
&
s_method
->
avl
);
avl_delete
(
&
s_object
->
schema_methods
,
&
s_method
->
avl
);
...
@@ -49,7 +50,7 @@ static void schema_flush_methods(struct schema_object *s_object)
...
@@ -49,7 +50,7 @@ static void schema_flush_methods(struct schema_object *s_object)
static
void
schema_flush_object
(
struct
schema_object
*
s_object
)
static
void
schema_flush_object
(
struct
schema_object
*
s_object
)
{
{
//fprintf(stderr,
"cleaning object %s\n", (char *)s_object->avl.key);
dbg
(
"cleaning object %s
\n
"
,
(
char
*
)
s_object
->
avl
.
key
);
schema_flush_methods
(
s_object
);
schema_flush_methods
(
s_object
);
free
((
void
*
)
s_object
->
avl
.
key
);
free
((
void
*
)
s_object
->
avl
.
key
);
...
@@ -68,12 +69,13 @@ void schema_flush_objects(void)
...
@@ -68,12 +69,13 @@ void schema_flush_objects(void)
{
{
struct
schema_object
*
s_object
,
*
tmp
;
struct
schema_object
*
s_object
,
*
tmp
;
//fprintf(stderr,
"cleaning all schema objects\n");
dbg
(
"cleaning all schema objects
\n
"
);
avl_for_each_element_safe
(
&
s_ctx
.
schema_objects
,
s_object
,
avl
,
tmp
)
{
avl_for_each_element_safe
(
&
s_ctx
.
schema_objects
,
s_object
,
avl
,
tmp
)
{
avl_delete
(
&
s_ctx
.
schema_objects
,
&
s_object
->
avl
);
avl_delete
(
&
s_ctx
.
schema_objects
,
&
s_object
->
avl
);
schema_flush_object
(
s_object
);
schema_flush_object
(
s_object
);
}
}
return
;
return
;
}
}
...
@@ -87,7 +89,7 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
...
@@ -87,7 +89,7 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
if
(
strncmp
(
blobmsg_name
(
properties
),
"properties"
,
11
))
if
(
strncmp
(
blobmsg_name
(
properties
),
"properties"
,
11
))
continue
;
continue
;
////fprintf(stderr,
"%s %d method name = %s\n", __func__, __LINE__, blobmsg_name(object));
dbg
(
"%s %d method name = %s
\n
"
,
__func__
,
__LINE__
,
blobmsg_name
(
object
));
s_method
=
(
struct
schema_method
*
)
calloc
(
1
,
sizeof
(
*
s_method
));
s_method
=
(
struct
schema_method
*
)
calloc
(
1
,
sizeof
(
*
s_method
));
if
(
!
s_method
)
if
(
!
s_method
)
continue
;
continue
;
...
@@ -96,22 +98,18 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
...
@@ -96,22 +98,18 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
avl_insert
(
&
schema_object
->
schema_methods
,
&
s_method
->
avl
);
avl_insert
(
&
schema_object
->
schema_methods
,
&
s_method
->
avl
);
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));
dbg
(
"%s %d name = %s
\n
"
,
__func__
,
__LINE__
,
blobmsg_name
(
method
));
if
(
!
strncmp
(
blobmsg_name
(
method
),
"input"
,
6
))
{
if
(
!
strncmp
(
blobmsg_name
(
method
),
"input"
,
6
))
{
int
len
=
blob_raw_len
(
method
);
int
len
=
blob_raw_len
(
method
);
s_method
->
b_input
=
(
struct
blob_attr
*
)
calloc
(
1
,
sizeof
(
struct
blob_attr
)
+
len
);
s_method
->
b_input
=
(
struct
blob_attr
*
)
calloc
(
1
,
sizeof
(
struct
blob_attr
)
+
len
);
memcpy
(
s_method
->
b_input
,
method
,
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
))
{
int
len
=
blob_raw_len
(
method
);
int
len
=
blob_raw_len
(
method
);
s_method
->
b_output
=
(
struct
blob_attr
*
)
calloc
(
1
,
sizeof
(
struct
blob_attr
)
+
len
);
s_method
->
b_output
=
(
struct
blob_attr
*
)
calloc
(
1
,
sizeof
(
struct
blob_attr
)
+
len
);
memcpy
(
s_method
->
b_output
,
method
,
len
);
memcpy
(
s_method
->
b_output
,
method
,
len
);
//fprintf(stderr, "b_output = %s\n", blobmsg_format_json(s_method->b_output, true));
}
}
}
}
}
}
...
@@ -129,7 +127,7 @@ schema_setup_file(const char *path)
...
@@ -129,7 +127,7 @@ schema_setup_file(const char *path)
blob_buf_init
(
&
acl
,
0
);
blob_buf_init
(
&
acl
,
0
);
if
(
!
blobmsg_add_json_from_file
(
&
acl
,
path
))
{
if
(
!
blobmsg_add_json_from_file
(
&
acl
,
path
))
{
//fprintf(stderr,
"Failed to parse %s\n", path);
dbg
(
"Failed to parse %s
\n
"
,
path
);
goto
out
;
goto
out
;
}
}
...
@@ -143,8 +141,7 @@ schema_setup_file(const char *path)
...
@@ -143,8 +141,7 @@ schema_setup_file(const char *path)
blob_for_each_attr
(
root
,
acl
.
head
,
rem
)
{
blob_for_each_attr
(
root
,
acl
.
head
,
rem
)
{
if
(
!
strncmp
(
blobmsg_name
(
root
),
"regex"
,
6
))
{
if
(
!
strncmp
(
blobmsg_name
(
root
),
"regex"
,
6
))
{
schema_object
->
regex
=
blobmsg_get_bool
(
root
);
schema_object
->
regex
=
blobmsg_get_bool
(
root
);
//fprintf(stderr, "%s: regex enabled %d\n", __func__, schema_object->regex);
dbg
(
"%s: regex enabled %d
\n
"
,
__func__
,
schema_object
->
regex
);
}
}
if
(
!
strncmp
(
blobmsg_name
(
root
),
"object"
,
7
))
{
if
(
!
strncmp
(
blobmsg_name
(
root
),
"object"
,
7
))
{
...
@@ -169,16 +166,17 @@ schema_setup_file(const char *path)
...
@@ -169,16 +166,17 @@ schema_setup_file(const char *path)
continue
;
continue
;
schema_parse_object
(
schema_object
,
object
);
schema_parse_object
(
schema_object
,
object
);
//fprintf(stderr,
"added schema %s\n", schema_object->object_name);
dbg
(
"added schema %s
\n
"
,
schema_object
->
object_name
);
}
}
}
}
if
(
schema_object
->
regex
)
{
if
(
schema_object
->
regex
)
{
int
rv
;
int
rv
;
//fprintf(stderr, "parsing regex for %s!\n", (char *)schema_object->avl.key );
dbg
(
"parsing regex for %s!
\n
"
,
(
char
*
)
schema_object
->
avl
.
key
);
rv
=
regcomp
(
&
schema_object
->
regex_exp
,
(
char
*
)
schema_object
->
avl
.
key
,
0
);
rv
=
regcomp
(
&
schema_object
->
regex_exp
,
(
char
*
)
schema_object
->
avl
.
key
,
0
);
if
(
rv
)
{
if
(
rv
)
{
//fprintf(stderr,
"%s: invalid regex: %s, flushing validation schema!\n", __func__, (char *) schema_object->avl.key);
dbg
(
"%s: invalid regex: %s, flushing validation schema!
\n
"
,
__func__
,
(
char
*
)
schema_object
->
avl
.
key
);
goto
out_flush
;
goto
out_flush
;
}
}
}
}
...
@@ -201,14 +199,14 @@ struct schema_method *schema_get_method_schema(const char *object, const char *m
...
@@ -201,14 +199,14 @@ struct schema_method *schema_get_method_schema(const char *object, const char *m
struct
schema_object
*
s_object
;
struct
schema_object
*
s_object
;
struct
schema_method
*
s_method
;
struct
schema_method
*
s_method
;
//fprintf(stderr,
"%s: object = %s method = %s\n", __func__, object, method);
dbg
(
"%s: object = %s method = %s
\n
"
,
__func__
,
object
,
method
);
s_object
=
schema_get_object_schema
(
object
);
s_object
=
schema_get_object_schema
(
object
);
if
(
!
s_object
)
if
(
!
s_object
)
return
NULL
;
return
NULL
;
avl_for_each_element
(
&
s_object
->
schema_methods
,
s_method
,
avl
)
{
avl_for_each_element
(
&
s_object
->
schema_methods
,
s_method
,
avl
)
{
//fprintf(stderr,
"%s: method = %s, avl.key = %s\n", __func__, method, (char *)s_method->avl.key);
dbg
(
"%s: method = %s, avl.key = %s
\n
"
,
__func__
,
method
,
(
char
*
)
s_method
->
avl
.
key
);
if
(
!
strncmp
(
method
,
(
char
*
)
s_method
->
avl
.
key
,
METHOD_NAME_MAX_LEN
))
if
(
!
strncmp
(
method
,
(
char
*
)
s_method
->
avl
.
key
,
METHOD_NAME_MAX_LEN
))
return
s_method
;
return
s_method
;
}
}
...
@@ -230,10 +228,10 @@ struct schema_object *schema_get_object_schema(const char *object)
...
@@ -230,10 +228,10 @@ struct schema_object *schema_get_object_schema(const char *object)
{
{
struct
schema_object
*
s_object
;
struct
schema_object
*
s_object
;
//fprintf(stderr,
"%s: object = %s\n", __func__, object);
dbg
(
"%s: object = %s
\n
"
,
__func__
,
object
);
avl_for_each_element
(
&
s_ctx
.
schema_objects
,
s_object
,
avl
)
{
avl_for_each_element
(
&
s_ctx
.
schema_objects
,
s_object
,
avl
)
{
//fprintf(stderr,
"%s: object = %s, avl.key = %s, regex = %d\n", __func__, object, (char *)s_object->avl.key, s_object->regex);
dbg
(
"%s: object = %s, avl.key = %s, regex = %d
\n
"
,
__func__
,
object
,
(
char
*
)
s_object
->
avl
.
key
,
s_object
->
regex
);
if
(
s_object
->
regex
)
{
if
(
s_object
->
regex
)
{
if
(
!
regexec
(
&
s_object
->
regex_exp
,
object
,
0
,
NULL
,
0
))
if
(
!
regexec
(
&
s_object
->
regex_exp
,
object
,
0
,
NULL
,
0
))
return
s_object
;
return
s_object
;
...
@@ -258,7 +256,7 @@ schema_setup_json(void)
...
@@ -258,7 +256,7 @@ schema_setup_json(void)
return
;
return
;
for
(
i
=
0
;
i
<
gl
.
gl_pathc
;
i
++
)
{
for
(
i
=
0
;
i
<
gl
.
gl_pathc
;
i
++
)
{
//fprintf(stderr,
"path = %s\n", gl.gl_pathv[i]);
dbg
(
"path = %s
\n
"
,
gl
.
gl_pathv
[
i
]);
schema_setup_file
(
gl
.
gl_pathv
[
i
]);
schema_setup_file
(
gl
.
gl_pathv
[
i
]);
}
}
...
...
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