Skip to content
Snippets Groups Projects

ENH: JsonSchemaExporter accepts do_not_create parameter.

Merged Florian Spreckelsen requested to merge release-v0.9.0 into main
3 files
+ 126
31
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -36,10 +36,13 @@ class JsonSchemaExporter:
"""
def __init__(self, additional_properties: bool = True,
name_and_description_in_properties: bool = False,
name_property_for_new_records: bool = False,
description_property_for_new_records: bool = False,
additional_options_for_text_props: dict = None,
units_in_description: bool = True,
do_not_create: List[str] = None,
do_not_retrieve: List[str] = None,
no_remote: bool = False,
):
"""Set up a JsonSchemaExporter, which can then be applied on RecordTypes.
@@ -48,9 +51,12 @@ class JsonSchemaExporter:
additional_properties : bool, optional
Whether additional properties will be admitted in the resulting
schema. Optional, default is True.
name_and_description_in_properties : bool, optional
Whether objects that are generated from reference properties shall have a `name` and
`description` property in the generated schema. Optional, default is False.
name_property_for_new_records : bool, optional
Whether objects shall generally have a `name` property in the generated schema.
Optional, default is False.
description_property_for_new_records : bool, optional
Whether objects shall generally have a `description` property in the generated schema.
Optional, default is False.
additional_options_for_text_props : dict, optional
Dictionary containing additional "pattern" or "format" options for
string-typed properties. Optional, default is empty.
@@ -63,17 +69,28 @@ class JsonSchemaExporter:
A list of RedcordType names, for which there should be no option
to create them. Instead, only the choice of existing elements should
be given.
do_not_retrieve : list[str]
A list of RedcordType names, for which no Records shall be retrieved. Instead, only an
object description should be given. If this list overlaps with the `do_not_create`
parameter, the behavior is undefined.
no_remote : bool
If True, do not attempt to connect to a LinkAhead server at all. Default is False.
"""
if not additional_options_for_text_props:
additional_options_for_text_props = {}
if not do_not_create:
do_not_create = []
if not do_not_retrieve:
do_not_retrieve = []
self._additional_properties = additional_properties
self._name_and_description_in_properties = name_and_description_in_properties
self._name_property_for_new_records = name_property_for_new_records
self._description_property_for_new_records = description_property_for_new_records
self._additional_options_for_text_props = additional_options_for_text_props
self._units_in_description = units_in_description
self._do_not_create = do_not_create
self._do_not_retrieve = do_not_retrieve
self._no_remote = no_remote
@staticmethod
def _make_required_list(rt: db.RecordType):
@@ -151,22 +168,31 @@ class JsonSchemaExporter:
prop_name = prop.datatype
if isinstance(prop.datatype, db.Entity):
prop_name = prop.datatype.name
values = self._retrieve_enum_values(f"RECORD '{prop_name}'")
if prop_name in self._do_not_retrieve:
values = []
else:
values = self._retrieve_enum_values(f"RECORD '{prop_name}'")
if prop_name in self._do_not_create:
# Only a simple list of values
json_prop["enum"] = values
else:
rt = db.execute_query(f"FIND RECORDTYPE WITH name='{prop_name}'",
unique=True)
if self._no_remote:
rt = prop.datatype
else:
rt = db.execute_query(f"FIND RECORDTYPE WITH name='{prop_name}'",
unique=True)
subschema = self._make_segment_from_recordtype(rt)
subschema["title"] = "Create new"
json_prop["oneOf"] = [
{
"title": "Existing entries",
"enum": values,
},
subschema
]
if values:
subschema["title"] = "Create new"
json_prop["oneOf"] = [
{
"title": "Existing entries",
"enum": values,
},
subschema
]
else:
json_prop = subschema
else:
raise ValueError(
@@ -203,8 +229,10 @@ class JsonSchemaExporter:
return prop
@staticmethod
def _retrieve_enum_values(role: str):
def _retrieve_enum_values(self, role: str):
if self._no_remote:
return []
possible_values = db.execute_query(f"SELECT name, id FROM {role}")
@@ -228,8 +256,9 @@ class JsonSchemaExporter:
schema["additionalProperties"] = self._additional_properties
props = OrderedDict()
if self._name_and_description_in_properties:
if self._name_property_for_new_records:
props["name"] = self._make_text_property("The name of the Record to be created")
if self._description_property_for_new_records:
props["description"] = self._make_text_property(
"The description of the Record to be created")
@@ -274,10 +303,14 @@ class JsonSchemaExporter:
def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = True,
name_and_description_in_properties: bool = False,
name_property_for_new_records: bool = False,
description_property_for_new_records: bool = False,
additional_options_for_text_props: Optional[dict] = None,
units_in_description: bool = True,
do_not_create: List[str] = None):
do_not_create: List[str] = None,
do_not_retrieve: List[str] = None,
no_remote: bool = False,
):
"""Create a jsonschema from a given RecordType that can be used, e.g., to
validate a json specifying a record of the given type.
@@ -291,9 +324,12 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
additional_properties : bool, optional
Whether additional properties will be admitted in the resulting
schema. Optional, default is True.
name_and_description_in_properties : bool, optional
Whether objects that are generated from reference properties shall have a `name` and
`description` property in the generated schema. Optional, default is False.
name_property_for_new_records : bool, optional
Whether objects shall generally have a `name` property in the generated schema. Optional,
default is False.
description_property_for_new_records : bool, optional
Whether objects shall generally have a `description` property in the generated schema.
Optional, default is False.
additional_options_for_text_props : dict, optional
Dictionary containing additional "pattern" or "format" options for
string-typed properties. Optional, default is empty.
@@ -306,6 +342,12 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
A list of RedcordType names, for which there should be no option
to create them. Instead, only the choice of existing elements should
be given.
do_not_retrieve : list[str]
A list of RedcordType names, for which no Records shall be retrieved. Instead, only an
object description should be given. If this list overlaps with the `do_not_create`
parameter, the behavior is undefined.
no_remote : bool
If True, do not attempt to connect to a LinkAhead server at all. Default is False.
Returns
-------
@@ -316,10 +358,13 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
exporter = JsonSchemaExporter(
additional_properties=additional_properties,
name_and_description_in_properties=name_and_description_in_properties,
name_property_for_new_records=name_property_for_new_records,
description_property_for_new_records=description_property_for_new_records,
additional_options_for_text_props=additional_options_for_text_props,
units_in_description=units_in_description,
do_not_create=do_not_create,
do_not_retrieve=do_not_retrieve,
no_remote=no_remote,
)
return exporter.recordtype_to_json_schema(rt)
@@ -408,7 +453,7 @@ out : dict
"properties": sub_schemas,
"required": required,
"additionalProperties": False,
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
}
return result
Loading