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
139f85e7
Commit
139f85e7
authored
1 year ago
by
Florian Spreckelsen
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Treat units
parent
a0fe997d
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!89
ENH: JsonSchemaExporter accepts do_not_create parameter.
,
!80
F simple schema export
Pipeline
#42346
failed
1 year 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/json_schema_exporter.py
+25
-7
25 additions, 7 deletions
src/caosadvancedtools/json_schema_exporter.py
with
25 additions
and
7 deletions
src/caosadvancedtools/json_schema_exporter.py
+
25
−
7
View file @
139f85e7
...
...
@@ -33,16 +33,20 @@ def _make_required_list(rt: db.RecordType):
if
rt
.
get_importance
(
prop
.
name
)
==
db
.
OBLIGATORY
]
def
_make_prop_from_prop
(
prop
:
db
.
Property
,
additional_options_for_text_props
:
Optional
[
dict
]):
def
_make_prop_from_prop
(
prop
:
db
.
Property
,
additional_options_for_text_props
:
Optional
[
dict
]
,
units_in_description
:
bool
):
"""
Return the JSON Schema segment for the given property
Parameters
----------
prop : db.Property
the property to be transformed
the property to be transformed
additional_options_for_text_props : Optional[dict]
dict of dicts that may contain the keys
'
pattern
'
and
'
format
'
to
further define the rules for the JSON Schema segment
dict of dicts that may contain the keys
'
pattern
'
and
'
format
'
to
further define the rules for the JSON Schema segment
units_in_description : bool
Whether to store the unit of a LinkAhead property in the description of
the corresponding json schema item or to create a separate `unit` key
instead.
"""
if
prop
.
is_reference
():
...
...
@@ -71,6 +75,13 @@ def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: O
json_prop
=
{}
if
prop
.
description
:
json_prop
[
"
description
"
]
=
prop
.
description
if
units_in_description
and
prop
.
unit
:
if
"
description
"
in
json_prop
:
json_prop
[
"
description
"
]
+=
f
"
Unit is
{
prop
.
unit
}
.
"
else
:
json_prop
[
"
description
"
]
=
f
"
Unit is
{
prop
.
unit
}
.
"
elif
prop
.
unit
:
json_prop
[
"
unit
"
]
=
prop
.
unit
if
prop
.
datatype
==
db
.
BOOLEAN
:
json_prop
[
"
type
"
]
=
"
boolean
"
...
...
@@ -83,7 +94,7 @@ def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: O
list_element_prop
=
db
.
Property
(
name
=
prop
.
name
,
datatype
=
get_list_datatype
(
prop
.
datatype
,
strict
=
True
))
json_prop
[
"
items
"
]
=
_make_prop_from_prop
(
list_element_prop
,
additional_options_for_text_props
)
list_element_prop
,
additional_options_for_text_props
,
units_in_description
)
else
:
raise
ValueError
(
f
"
Unknown or no property datatype. Property
{
prop
.
name
}
with type
{
prop
.
datatype
}
"
)
...
...
@@ -111,7 +122,8 @@ def _make_text_property(description="", text_format=None, text_pattern=None):
def
recordtype_to_json_schema
(
rt
:
db
.
RecordType
,
additional_properties
:
bool
=
True
,
name_and_description_in_properties
:
bool
=
False
,
additional_options_for_text_props
:
Optional
[
dict
]
=
None
):
additional_options_for_text_props
:
Optional
[
dict
]
=
None
,
units_in_description
:
bool
=
True
):
"""
Create a jsonschema from a given RecordType that can be used, e.g., to
validate a json specifying a record of the given type.
...
...
@@ -128,6 +140,11 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
additional_options_for_text_props : dict, optional
Dictionary containing additional
"
pattern
"
or
"
format
"
options for
string-typed properties. Optional, default is empty.
units_in_description : bool, optional
Whether to add the unit of a LinkAhead property (if it has any) to the
description of the corresponding schema entry. If set to false, an
additional `unit` key is added to the schema itself which is purely
annotational and ignored, e.g., in validation. Default is True.
Returns
-------
...
...
@@ -163,7 +180,8 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
"
Creating a schema for multi-properties is not specified.
"
f
"
Property
{
prop
.
name
}
occurs more than once.
"
)
props
[
prop
.
name
]
=
_make_prop_from_prop
(
prop
,
additional_options_for_text_props
)
props
[
prop
.
name
]
=
_make_prop_from_prop
(
prop
,
additional_options_for_text_props
,
units_in_description
)
schema
[
"
properties
"
]
=
props
return
schema
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