Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LOFAR
Manage
Activity
Members
Labels
Plan
Issues
Wiki
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RadioObservatory
LOFAR
Commits
69125073
Commit
69125073
authored
2 years ago
by
Jorrit Schaap
Browse files
Options
Downloads
Patches
Plain Diff
TMSS-860
: check if json_doc and schema have matching and
parent
c278f434
No related branches found
No related tags found
1 merge request
!828
TMSS-860
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
LCS/PyCommon/json_utils.py
+15
-14
15 additions, 14 deletions
LCS/PyCommon/json_utils.py
with
15 additions
and
14 deletions
LCS/PyCommon/json_utils.py
+
15
−
14
View file @
69125073
...
...
@@ -17,6 +17,7 @@
import
json
import
time
import
typing
import
jsonschema
from
copy
import
deepcopy
...
...
@@ -429,35 +430,35 @@ def validate_json_against_its_schema(json_object: dict, cache: dict=None, max_ca
return
validate_json_against_schema
(
json_object
,
referenced_schema
,
cache
=
cache
,
max_cache_age
=
max_cache_age
,
add_defaults
=
add_defaults
)
def
validate_json_against_schema
(
json_
string
:
str
,
schema
:
str
,
cache
:
dict
=
None
,
max_cache_age
:
timedelta
=
DEFAULT_MAX_SCHEMA_CACHE_AGE
,
add_defaults
:
bool
=
False
)
->
dict
:
def
validate_json_against_schema
(
json_
doc
:
typing
.
Union
[
str
,
dict
],
schema
:
typing
.
Union
[
str
,
dict
]
,
cache
:
dict
=
None
,
max_cache_age
:
timedelta
=
DEFAULT_MAX_SCHEMA_CACHE_AGE
,
add_defaults
:
bool
=
False
)
->
dict
:
'''
validate the given json_string against the given schema.
If no exception if thrown, then the given json_string validates against the given schema.
:raises SchemaValidationException if the json_string does not validate against the schema
'''
# ensure the given arguments are strings
if
type
(
json_string
)
!=
str
:
json_string
=
json
.
dumps
(
json_string
)
if
type
(
schema
)
!=
str
:
schema
=
json
.
dumps
(
schema
)
# ensure the specification and schema are both valid json in the first place
try
:
json_object
=
json
.
loads
(
json_string
)
if
isinstance
(
json_doc
,
str
):
json_doc
=
json
.
loads
(
json_doc
)
except
json
.
decoder
.
JSONDecodeError
as
e
:
raise
jsonschema
.
exceptions
.
ValidationError
(
"
Invalid JSON: %s
\n
%s
"
%
(
str
(
e
),
json_
string
))
raise
jsonschema
.
exceptions
.
ValidationError
(
"
Invalid JSON: %s
\n
%s
"
%
(
str
(
e
),
json_
doc
))
try
:
schema_object
=
json
.
loads
(
schema
)
if
isinstance
(
schema
,
str
):
schema
=
json
.
loads
(
schema
)
except
json
.
decoder
.
JSONDecodeError
as
e
:
raise
jsonschema
.
exceptions
.
ValidationError
(
"
Invalid JSON: %s
\n
%s
"
%
(
str
(
e
),
schema
))
if
'
$schema
'
not
in
json_doc
:
raise
jsonschema
.
exceptions
.
ValidationError
(
"
The json document does not contain a $schema property
\n
%s
"
%
(
schema
,))
if
json_doc
[
'
$schema
'
].
rstrip
(
'
/ref_resolved
'
).
rstrip
(
'
#
'
).
rstrip
(
'
/
'
)
!=
schema
.
get
(
'
$id
'
,
''
).
rstrip
(
'
#
'
).
rstrip
(
'
/
'
):
raise
jsonschema
.
exceptions
.
ValidationError
(
"
The json document with $schema=
'
%s
'
cannot be validated by schema with $id=
'
%s
'
\n
%s
"
%
(
json_doc
[
'
$schema
'
],
schema
.
get
(
'
$id
'
)))
# resolve $refs to fill in defaults for those, too
schema
_object
=
resolved_remote_refs
(
schema
_object
,
cache
=
cache
,
max_cache_age
=
max_cache_age
)
schema
=
resolved_remote_refs
(
schema
,
cache
=
cache
,
max_cache_age
=
max_cache_age
)
# now do the actual validation
try
:
return
validate_json_object_with_schema
(
json_
object
,
schema
_object
,
add_defaults
=
add_defaults
)
return
validate_json_object_with_schema
(
json_
doc
,
schema
,
add_defaults
=
add_defaults
)
except
jsonschema
.
ValidationError
as
e
:
raise
jsonschema
.
exceptions
.
ValidationError
(
str
(
e
))
...
...
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