Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
CaosDB Crawler
Commits
2cceed88
Commit
2cceed88
authored
8 months ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH(scanner): validation function for json schemas
parent
f291fbf4
No related branches found
No related tags found
2 merge requests
!217
TST: Make NamedTemporaryFiles Windows-compatible
,
!201
Validator that checks created records using a json schema
Pipeline
#58149
passed
8 months ago
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caoscrawler/validator.py
+20
-10
20 additions, 10 deletions
src/caoscrawler/validator.py
unittests/test_validation.py
+25
-1
25 additions, 1 deletion
unittests/test_validation.py
with
45 additions
and
11 deletions
src/caoscrawler/validator.py
+
20
−
10
View file @
2cceed88
...
@@ -28,14 +28,12 @@ This module contains functions to validate the output of a scanner run with a
...
@@ -28,14 +28,12 @@ This module contains functions to validate the output of a scanner run with a
json schema.
json schema.
"""
"""
import
json
import
jsonschema
import
jsonschema
import
linkahead
as
db
import
linkahead
as
db
# from caosadvancedtools.models.parser import parse_model_from_string
# from caosadvancedtools.models.parser import parse_model_from_string
from
caosadvancedtools.json_schema_exporter
import
recordtype_to_json_schema
from
caosadvancedtools.json_schema_exporter
import
recordtype_to_json_schema
from
caosadvancedtools.models.parser
import
parse_model_from_yaml
from
caosadvancedtools.models.parser
import
parse_model_from_yaml
from
jsonschema
import
ValidationError
from
linkahead.high_level_api
import
convert_to_python_object
from
linkahead.high_level_api
import
convert_to_python_object
from
caoscrawler
import
scanner
from
caoscrawler
import
scanner
...
@@ -101,9 +99,12 @@ def convert_record(record: db.Record):
...
@@ -101,9 +99,12 @@ def convert_record(record: db.Record):
return
pobj
return
pobj
def
validate
(
records
:
list
[
db
.
Record
],
schema
:
dict
)
->
tuple
[
list
,
list
]:
def
validate
(
records
:
list
[
db
.
Record
],
schema
s
:
list
[
dict
]
)
->
tuple
[
list
,
list
]:
"""
"""
Validate a list of records against a JSON schema.
Validate a list of records against a list of possible JSON schemas.
It is tried to validate each schema from the list of schemas. If none of them validates
without error, it is assumed that it does not match at all.
Arguments:
Arguments:
----------
----------
...
@@ -111,8 +112,8 @@ def validate(records: list[db.Record], schema: dict) -> tuple[list, list]:
...
@@ -111,8 +112,8 @@ def validate(records: list[db.Record], schema: dict) -> tuple[list, list]:
records: list[db.Record]
records: list[db.Record]
List of records that will be validated.
List of records that will be validated.
schema: dict
schema
s
:
list[
dict
]
A JSON schema generated using `load_json_schema_from_datamodel_yaml`.
A
list of
JSON schema
s
generated using `load_json_schema_from_datamodel_yaml`.
Returns:
Returns:
--------
--------
...
@@ -120,7 +121,16 @@ def validate(records: list[db.Record], schema: dict) -> tuple[list, list]:
...
@@ -120,7 +121,16 @@ def validate(records: list[db.Record], schema: dict) -> tuple[list, list]:
- Index 0: A list of boolean values, one for each record in `records` determining whether
- Index 0: A list of boolean values, one for each record in `records` determining whether
the validation was successful.
the validation was successful.
- Index 1: A list of ValidationErrors (in case of insuccesful validation) or None if
- Index 1: A list of schemas matching the record at this position of the list `records`.
the validation was successful.
"""
"""
retval
=
[]
for
r
in
records
:
matching_schemas
=
[]
for
schema
in
schemas
:
try
:
jsonschema
.
validate
(
convert_record
(
r
),
schema
)
matching_schemas
.
append
(
schema
)
except
ValidationError
:
pass
pass
retval
.
append
((
len
(
matching_schemas
)
>
0
,
matching_schemas
))
return
retval
This diff is collapsed.
Click to expand it.
unittests/test_validation.py
+
25
−
1
View file @
2cceed88
...
@@ -34,7 +34,8 @@ import linkahead as db
...
@@ -34,7 +34,8 @@ import linkahead as db
import
pytest
import
pytest
import
yaml
import
yaml
from
caoscrawler.validator
import
(
convert_record
,
from
caoscrawler.validator
import
(
convert_record
,
load_json_schema_from_datamodel_yaml
)
load_json_schema_from_datamodel_yaml
,
validate
)
from
jsonschema
import
ValidationError
from
jsonschema
import
ValidationError
UNITTESTDIR
=
Path
(
__file__
).
parent
UNITTESTDIR
=
Path
(
__file__
).
parent
...
@@ -62,3 +63,26 @@ def test_create_json_schema():
...
@@ -62,3 +63,26 @@ def test_create_json_schema():
with
pytest
.
raises
(
ValidationError
,
match
=
"
.*
'
keywords
'
is a required property.*
"
):
with
pytest
.
raises
(
ValidationError
,
match
=
"
.*
'
keywords
'
is a required property.*
"
):
jsonschema
.
validate
(
pobj
,
json
[
0
])
jsonschema
.
validate
(
pobj
,
json
[
0
])
def
test_validation
():
"""
Test for the main validation API function `validate`
"""
json
=
load_json_schema_from_datamodel_yaml
(
join
(
UNITTESTDIR
,
"
datamodels
"
,
"
datamodel.yaml
"
))
r1
=
db
.
Record
()
r1
.
add_parent
(
name
=
"
Dataset
"
)
r1
.
add_property
(
name
=
"
keywords
"
,
value
=
"
jakdlfjakdf
"
)
r1
.
add_property
(
name
=
"
dateModified
"
,
value
=
"
2024-11-16
"
)
r2
=
db
.
Record
()
r2
.
add_parent
(
name
=
"
Dataset
"
)
r2
.
add_property
(
name
=
"
keywordss
"
,
value
=
"
jakdlfjakdf
"
)
r2
.
add_property
(
name
=
"
dateModified
"
,
value
=
"
2024-11-16
"
)
valres
=
validate
([
r1
,
r2
],
json
)
assert
valres
[
0
][
0
]
assert
len
(
valres
[
0
][
1
])
==
1
assert
valres
[
0
][
1
][
0
]
==
json
[
0
]
assert
len
(
valres
[
1
][
1
])
==
0
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