caosadvancedtools.json_schema_exporter module#
Convert a data model into a json schema.
Sometimes you may want to have a json schema which describes a LinkAhead data model, for example for the automatic generation of user interfaces with third-party tools like rjsf. Then this is the right module for you!
The json_schema_exporter module has one main class,
JsonSchemaExporter, and a few utility and wrapper functions.
For easy usage, you may simply import recordtype_to_json_schema and use it on a fully referenced RecordType like this:
import caosadvancedtools.models.parser as parser
import caosadvancedtools.json_schema_exporter as jsex
model = parser.parse_model_from_yaml("my_model.yml")
# get the data model schema for the "Journey" recordtype
schema, ui_schema = recordtype_to_json_schema(
rt=model.get_deep("Journey"),
do_not_create=["Continent"], # only choose from existing Records
multiple_choice=["visited_cities"],
rjsf=True # also create a UI schema
)
For more details on how to use this wrapper, read the function documentation <recordtype_to_json_schema>.
Other useful functions are make_array, which creates an array out of a single schema, and merge_schemas, which as the name suggests allows to combine multiple schema definitions into a single schema.
- class caosadvancedtools.json_schema_exporter.JsonSchemaExporter(additional_properties: bool = True, name_property_for_new_records: bool = False, use_id_for_identification: bool = False, description_property_for_new_records: bool = False, additional_options_for_text_props: dict | None = None, additional_json_schema: dict[str, dict] | None = None, additional_ui_schema: dict[str, dict] | None = None, units_in_description: bool = True, do_not_create: list[str] | None = None, do_not_retrieve: str | list[str] | None = None, no_remote: bool = False, use_rt_pool: DataModel | None = None, enums: list[str] | None = None, multiple_choice: list[str] | None = None, multiple_choice_guess: bool = False, wrap_files_in_objects: bool = False, add_readonly: dict | None = None)#
Bases:
objectA class which collects everything needed for the conversion.
- get_unused_additional_ui_schema_keys() set[str]#
Return configured
additional_ui_schemakeys that were not used.- Returns:
The configured keys in
additional_ui_schemawhich have not been applied yet.- Return type:
set[str]
- recordtype_to_json_schema(rt: RecordType, rjsf: bool = False) dict | tuple[dict, dict]#
Create a jsonschema from a given RecordType that can be used, e.g., to validate a json specifying a record of the given type.
add_readonlyeffect#If this object’s
add_readonlymatches the record type,__is_readonlyproperties will be added at the locations indicated byadd_readonly: At the “leaf level”, there is a special dict, with a boolean__hidden__which decides whether the__is_readonlyproperty is hidden or not. There may also be a__msg__string value which will be added as the description of the record type.Example
add_readonly = { "TopRT": { "Ref1": { __msg__: "Ref1 is read-only", __hidden__: True }, "Ref2": { "DeepRef": {__hidden__: False} } }
In this example,
TopRT.Ref1andTopRT.Ref2.DeepRefwill have the additional__is_readonlyproperty. Ifrjsfis True, in the first case this property will have the uiSchema element"ui:widget": "hidden".- param rt:
The RecordType from which a json schema will be created.
- type rt:
RecordType
- param rjsf:
If True, uiSchema definitions for react-jsonschema-forms will be output as the second return value.
- type rjsf:
bool, default=False
- returns:
schema (dict) – A dict containing the json schema created from the given RecordType’s properties.
ui_schema (dict, optional) – A ui schema. Only if a parameter asks for it (e.g.
rjsf).
- caosadvancedtools.json_schema_exporter.make_array(schema: dict, rjsf_uischema: dict | None = None) dict | tuple[dict, dict]#
Create an array of the given schema.
The result will look like this:
{ "type": "array", "items": { // the schema } }
- Parameters:
schema (dict) – The JSON schema which shall be packed into an array.
rjsf_uischema (dict, optional) – A react-jsonschema-forms ui schema that shall be wrapped as well.
- Returns:
schema (dict) – A JSON schema dict with a top-level array which contains instances of the given schema.
ui_schema (dict, optional) – The wrapped ui schema. Only returned if
rjsf_uischemais given as parameter.
- caosadvancedtools.json_schema_exporter.merge_schemas(schemas: dict[str, dict] | Iterable[dict], rjsf_uischemas: dict[str, dict] | Sequence[dict] | None = None, return_data_schema=False) dict | tuple[dict, dict]#
Merge the given schemata into a single schema.
The result will look like this:
{ "type": "object", "properties": { // A, B, C }, "required": [ // "A", "B", "C" ], "additionalProperties": false }
- Parameters:
schemas (dict[str, dict] | Iterable[dict]) – A dict or iterable of schemata which shall be merged together. If this is a dict, the keys will be used as property names, otherwise the titles of the submitted schemata. If they have no title, numbers will be used as a fallback. Note that even with a dict, the original schema’s “title” is not changed.
rjsf_uischemas (dict[str, dict] | Iterable[dict], optional) – If given, also merge the react-jsonschema-forms from this argument and return as the second return value. If
schemasis a dict, this parameter must also be a dict, ifschemasis only an iterable, this paramater must support numerical indexing.return_data_schema (bool, default False) – If set to True, a second schema with all top-level entries wrapped in an array will be returned. This is necessary if the schema describes the data layout of an XLSX file. Cannot be used together with rjsf_uischemas.
- Returns:
schema (dict) – A JSON schema dict with a top-level object which contains the given schemata as properties.
uischema (dict) – If
rjsf_uischemaswas given, this contains the merged UI schemata.data_schema (dict) – If
return_data_schemawas given, this contains the XLSX file schema.
- caosadvancedtools.json_schema_exporter.recordtype_to_json_schema(rt: RecordType, additional_properties: bool = True, name_property_for_new_records: bool = False, use_id_for_identification: bool = False, description_property_for_new_records: bool = False, additional_options_for_text_props: dict | None = None, additional_json_schema: dict[str, dict] | None = None, additional_ui_schema: dict[str, dict] | None = None, units_in_description: bool = True, do_not_create: list[str] | None = None, do_not_retrieve: str | list[str] | None = None, no_remote: bool = False, use_rt_pool: DataModel | None = None, multiple_choice: list[str] | None = None, multiple_choice_guess: bool = False, rjsf: bool = False, wrap_files_in_objects: bool = False, add_readonly: dict | None = None) dict | tuple[dict, dict]#
Create a jsonschema from a given RecordType that can be used, e.g., to validate a json specifying a record of the given type.
This is a standalone function which works without manually creating a JsonSchemaExporter object.
- Parameters:
rt (RecordType) – The RecordType from which a json schema will be created.
JsonSchemaExporter (The other parameters are identical to the ones use by)
- Returns:
schema (dict) – A dict containing the json schema created from the given RecordType’s properties.
ui_schema (dict, optional) – A ui schema. Only if a parameter asks for it (e.g.
rjsf).