Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-advanced-user-tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
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
caosdb
Software
caosdb-advanced-user-tools
Commits
fef66b62
Commit
fef66b62
authored
4 months ago
by
I. Nüske
Browse files
Options
Downloads
Patches
Plain Diff
BUG: Add a function to filter dict before using jsonschema.validate in convert.py
parent
841bae3b
No related branches found
No related tags found
2 merge requests
!138
Release 0.14.0
,
!129
Enable validation in table_json_conversion.convert.to_dict for use in XLSX-converter
Pipeline
#60709
passed
4 months ago
Stage: setup
Stage: cert
Stage: style
Stage: unittest
Stage: integrationtest
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/caosadvancedtools/table_json_conversion/convert.py
+46
-2
46 additions, 2 deletions
src/caosadvancedtools/table_json_conversion/convert.py
with
46 additions
and
2 deletions
src/caosadvancedtools/table_json_conversion/convert.py
+
46
−
2
View file @
fef66b62
...
...
@@ -31,6 +31,7 @@ from operator import getitem
from
types
import
SimpleNamespace
from
typing
import
Any
,
BinaryIO
,
Callable
,
TextIO
,
Union
,
Optional
from
warnings
import
warn
from
copy
import
deepcopy
import
jsonschema
from
openpyxl
import
load_workbook
...
...
@@ -152,6 +153,49 @@ class ForeignError(KeyError):
self
.
definitions
=
definitions
def
_validate_jsonschema
(
instance
,
schema
):
# Checks whether a key: value pair is in the given schema or a direct
# subschema (anyOf) ToDo: How to treat allOf and oneOf?
def
in_schema
(
key
,
val
,
schema
):
if
schema
.
get
(
key
,
None
)
==
val
:
return
True
if
'
anyOf
'
in
schema
:
return
any
([
in_schema
(
key
,
val
,
sub
)
for
sub
in
schema
[
'
anyOf
'
]])
return
False
# Removes Key: None and datetime instances from nested dicts and lists of
# any depth. Key: None is currently valid as there is no 'obligatory with
# value', and datetime cannot be checked by jsonschema.
# ToDo: Is ID: None also valid?
def
remove_incompatible_values
(
it
,
schema
):
if
isinstance
(
it
,
list
):
schema
=
schema
.
get
(
'
items
'
,
schema
)
for
elem
in
it
:
remove_incompatible_values
(
elem
,
schema
)
elif
isinstance
(
it
,
dict
):
schema
=
schema
.
get
(
'
properties
'
,
schema
)
for
key
,
elem
in
list
(
it
.
items
()):
if
elem
is
None
:
it
.
pop
(
key
)
elif
isinstance
(
elem
,
datetime
.
date
)
or
isinstance
(
elem
,
datetime
.
datetime
):
if
in_schema
(
'
format
'
,
'
date
'
,
schema
[
key
])
or
in_schema
(
'
format
'
,
'
date-time
'
,
schema
[
key
]):
it
.
pop
(
key
)
elif
isinstance
(
it
,
(
dict
,
list
)):
remove_incompatible_values
(
elem
,
schema
[
key
])
return
it
# If instance is not a dict, remove_incompatible_values would not remove
# the value if it is valid, so we need to check manually by wrapping
instance
=
deepcopy
(
instance
)
if
not
isinstance
(
instance
,
dict
):
if
remove_incompatible_values
({
'
key
'
:
instance
},
{
'
key
'
:
schema
})
==
{}:
return
# Clean dict and validate
instance
=
remove_incompatible_values
(
deepcopy
(
instance
),
schema
)
jsonschema
.
validate
(
instance
,
schema
=
schema
)
class
XLSXConverter
:
"""
Class for conversion from XLSX to JSON.
...
...
@@ -328,7 +372,7 @@ class XLSXConverter:
for
e
in
exceptions
])
raise
jsonschema
.
ValidationError
(
mess
)
if
validate
:
jsonschema
.
validate
(
self
.
_result
,
self
.
_schema
)
_validate_
jsonschema
(
self
.
_result
,
self
.
_schema
)
if
self
.
_errors
:
raise
RuntimeError
(
"
There were error while handling the XLSX file.
"
)
return
self
.
_result
...
...
@@ -563,7 +607,7 @@ class XLSXConverter:
value
=
False
if
value
==
1
or
isinstance
(
value
,
str
)
and
'
=true()
'
==
value
.
lower
():
value
=
True
jsonschema
.
validate
(
value
,
subschema
)
_validate_
jsonschema
(
value
,
subschema
)
# Finally: convert to target type
return
self
.
PARSER
[
subschema
.
get
(
"
type
"
,
"
string
"
)](
value
)
...
...
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