Code#

MetaModel#

See Datamodel docs for full documentation

class linkml_runtime.linkml_model.meta.SchemaDefinition(*args, _if_missing: Optional[Callable[[JsonObj, str], Tuple[bool, Any]]] = None, **kwargs)[source]#

A collection of definitions that make up a schema or a data model.

class linkml_runtime.linkml_model.meta.ClassDefinition(*args, _if_missing: Optional[Callable[[JsonObj, str], Tuple[bool, Any]]] = None, **kwargs)[source]#

an element whose instances are complex objects that may have slot-value assignments

class linkml_runtime.linkml_model.meta.SlotDefinition(*args, _if_missing: Optional[Callable[[JsonObj, str], Tuple[bool, Any]]] = None, **kwargs)[source]#

an element that describes how instances are related to other instances

Generators#

Code for various generators for schema definitions

class linkml.generators.jsonschemagen.JsonSchemaGenerator(schema: ~typing.Union[str, ~typing.TextIO, ~linkml_runtime.linkml_model.meta.SchemaDefinition, ~linkml.utils.generator.Generator], schemaview: ~typing.Optional[~linkml_runtime.utils.schemaview.SchemaView] = None, format: ~typing.Optional[str] = None, metadata: bool = <factory>, useuris: ~typing.Optional[bool] = None, log_level: int = 30, mergeimports: ~typing.Optional[bool] = <factory>, source_file_date: ~typing.Optional[str] = None, source_file_size: ~typing.Optional[int] = None, logger: ~typing.Optional[~logging.Logger] = None, verbose: ~typing.Optional[bool] = None, output: ~typing.Optional[str] = None, namespaces: ~typing.Optional[~linkml_runtime.utils.namespaces.Namespaces] = None, directory_output: bool = False, base_dir: ~typing.Optional[str] = None, metamodel_name_map: ~typing.Optional[~typing.Dict[str, str]] = None, importmap: ~typing.Optional[~typing.Union[str, ~typing.Mapping[str, str]]] = None, emit_prefixes: ~typing.Set[str] = <factory>, metamodel: ~typing.Optional[~linkml.utils.schemaloader.SchemaLoader] = None, stacktrace: bool = False, topClass: ~typing.Optional[str] = None, not_closed: ~typing.Optional[bool] = <factory>, indent: int = 4, inline: bool = False, top_class: ~typing.Optional[~typing.Union[~linkml_runtime.linkml_model.meta.ClassDefinitionName, str]] = None, include_range_class_descendants: bool = <factory>, top_level_schema: ~typing.Optional[~linkml.generators.jsonschemagen.JsonSchema] = None, **_kwargs)[source]#

Generates JSONSchema documents from a LinkML SchemaDefinition

  • Each linkml class generates a schema

  • inheritance hierarchies are rolled-down from ancestors

  • Composition not yet implemented

  • Enumerations treated as strings

  • Foreign key references are treated as semantics-free strings

serialize(**kwargs) str[source]#

Generate output in the required format

Parameters:

kwargs – Generator specific parameters

Returns:

Generated output

class linkml.generators.yamlgen.YAMLGenerator(schema: ~typing.Union[str, ~typing.TextIO, ~linkml_runtime.linkml_model.meta.SchemaDefinition, ~linkml.utils.generator.Generator], schemaview: ~typing.Optional[~linkml_runtime.utils.schemaview.SchemaView] = None, format: ~typing.Optional[str] = None, metadata: bool = <factory>, useuris: ~typing.Optional[bool] = None, log_level: int = 30, mergeimports: ~typing.Optional[bool] = <factory>, source_file_date: ~typing.Optional[str] = None, source_file_size: ~typing.Optional[int] = None, logger: ~typing.Optional[~logging.Logger] = None, verbose: ~typing.Optional[bool] = None, output: ~typing.Optional[str] = None, namespaces: ~typing.Optional[~linkml_runtime.utils.namespaces.Namespaces] = None, directory_output: bool = False, base_dir: ~typing.Optional[str] = None, metamodel_name_map: ~typing.Optional[~typing.Dict[str, str]] = None, importmap: ~typing.Optional[~typing.Union[str, ~typing.Mapping[str, str]]] = None, emit_prefixes: ~typing.Set[str] = <factory>, metamodel: ~typing.Optional[~linkml.utils.schemaloader.SchemaLoader] = None, stacktrace: bool = False, validateonly: bool = <factory>, **_kwargs)[source]#

A generator that produces a schema as a YAML Document

serialize(validateonly: bool = False, **kwargs) str[source]#

Generate output in the required format

Parameters:

kwargs – Generator specific parameters

Returns:

Generated output

Loaders and Dumpers#

class linkml_runtime.dumpers.json_dumper.JSONDumper[source]#
dump(element: Union[BaseModel, YAMLRoot], to_file: str, contexts: Optional[Union[str, dict, JsonObj, List[Union[str, dict, JsonObj]]]] = None, **kwargs) None[source]#

Write element as json to to_file :param element: LinkML object to be serialized as YAML :param to_file: file to write to :param contexts: JSON-LD context(s) in the form of:

  • file name

  • URL

  • JSON String

  • dict

  • JSON Object

  • A list containing elements of any type named above

dumps(element: Union[BaseModel, YAMLRoot], contexts: Optional[Union[str, dict, JsonObj, List[Union[str, dict, JsonObj]]]] = None, inject_type=True) str[source]#

Return element as a JSON or a JSON-LD string :param element: LinkML object to be emitted :param contexts: JSON-LD context(s) in the form of:

  • file name

  • URL

  • JSON String

  • dict

  • JSON Object

  • A list containing elements of any type named above

Parameters:

inject_type – if True (default), add a @type at the top level

Returns:

JSON Object representing the element

class linkml_runtime.loaders.JSONLoader[source]#
loads(source: str, target_class: Type[Union[BaseModel, YAMLRoot]], *, metadata: Optional[FileInfo] = None, **_) Union[BaseModel, YAMLRoot]#

Load source as a string :param source: source :param target_class: destination class :param metadata: metadata about the source :param _: extensions :return: instance of taarget_class

Schema Utils#

class linkml_runtime.utils.schemaview.SchemaView(schema: Union[str, Path, SchemaDefinition], importmap: Optional[Dict[str, str]] = None, merge_imports: bool = False, base_dir: Optional[str] = None)[source]#

A SchemaView provides a virtual schema layered on top of a schema plus its import closure

Most operations are parameterized by imports. If this is set to True (default), then the full import closure is considered when answering

This class utilizes caching for efficient lookup operations.

TODO: decide how to use this in conjunction with the existing schemaloader, which injects into the schema rather than providing dynamic methods.

See:

Utils#

Utilities

Validator#

The linkml.validator package contains a new LinkML validation framework that is more flexible than the linkml.validators package. While that package still exists, it may become deprecated in the future.

class linkml.validator.Validator(schema: Union[str, dict, TextIO, Path, SchemaDefinition], validation_plugins: Optional[List[ValidationPlugin]] = None, *, strict: bool = False)[source]#

A class for coordinating instance validation using configurable plugins

Parameters:
  • schema – The schema to validate against. If a string or Path, the schema will be loaded from that location. Otherwise, a SchemaDefinition is required.

  • validation_plugins – A list of plugins that be used to validate instances using the given schema. Each element should be an instance of a subclass of linkml.validator.plugins.ValidationPlugin. Defaults to None.

  • strict – If True, stop validating after the first validation problem is found. Defaults to False.

iter_results(instance: Any, target_class: Optional[str] = None) Iterator[ValidationResult][source]#

Lazily yield validation results for the given instance

Parameters:
  • instance – The instance to validate

  • target_class – Name of the class within the schema to validate against. If None, the class will be inferred from the schema by looked for a class with tree_root: true. Defaults to None.

Returns:

Iterator over validation results

Return type:

Iterator[ValidationResult]

iter_results_from_source(loader: Loader, target_class: Optional[str] = None) Iterator[ValidationResult][source]#

Lazily yield validation results for the instances provided by a loader

Parameters:
  • loader – An instance of a subclass of linkml.validator.loaders.Loader which provides the instances to validate

  • target_class – Name of the class within the schema to validate against. If None, the class will be inferred from the schema by looked for a class with tree_root: true. Defaults to None.

Returns:

Iterator over validation results

Return type:

Iterator[ValidationResult]

validate(instance: Any, target_class: Optional[str] = None) ValidationReport[source]#

Validate the given instance

Parameters:
  • instance – The instance to validate

  • target_class – Name of the class within the schema to validate against. If None, the class will be inferred from the schema by looked for a class with tree_root: true. Defaults to None.

Returns:

A validation report

Return type:

ValidationReport

validate_source(loader: Loader, target_class: Optional[str] = None) ValidationReport[source]#

Validate instances from a data source

Parameters:
  • loader – An instance of a subclass of linkml.validator.loaders.Loader which provides the instances to validate

  • target_class – Name of the class within the schema to validate against. If None, the class will be inferred from the schema by looked for a class with tree_root: true. Defaults to None.

Returns:

A validation report

Return type:

ValidationReport

linkml.validator.validate(instance: Any, schema: Union[str, dict, SchemaDefinition], target_class: Optional[str] = None, *, strict: bool = False) ValidationReport[source]#

Validate a data instance against a schema

This function provides a simple interface to do basic validation performed by a JSON Schema validator on a single instance. To have more control over the type of validation performed, see the Validator class.

Parameters:
  • instance – The instance to validate

  • schema – The schema used to validate the instance. If a string is it will be interpreted as a path, URL, or other loadable location. If it is a dict it should be compatible with SchemaDefinition, otherwise it should be a SchemaDefinition instance.

  • target_class – Name of the class within the schema to validate against. If None, the class will be inferred from the schema by looked for a class with tree_root: true. Defaults to None.

  • strict – If True, validation will stop after the first validation error is found, Otherwise all validation problems will be reported. Defaults to False.

Raises:

ValueError – If a valid SchemaDefinition cannot be constructed from the schema parameter.

Returns:

A validation report

Return type:

ValidationReport

linkml.validator.validate_file(file: Union[str, bytes, PathLike], schema: Union[str, dict, SchemaDefinition], target_class: Optional[str] = None, *, strict: bool = False) ValidationReport[source]#

Validate instances loaded from a file against a schema

This function provides a simple interface to do basic validation performed by a JSON Schema validator on instances loaded from a file. Loading is done according to the file’s extension. Accepted file extensions are: .csv, .tsv, .yaml, .yml, and .json. Individual rows of CSV and TSV files are treated as instances to validate. Each document within a YAML file is treated as an individual instance to validate. If the top-level of a JSON file is an array, each element of the array is treated as an instance to validate. Otherwise, if the top-level is an object it is treated as a single instance to validate.

To have more control over the type of validation performed, see the Validator class.

Parameters:
  • file – Path-like object of the file to be read

  • schema – The schema used to validate the instance. If a string is it will be interpreted as a path, URL, or other loadable location. If it is a dict it should be compatible with SchemaDefinition, otherwise it should be a SchemaDefinition instance.

  • target_class – Name of the class within the schema to validate against. If None, the class will be inferred from the schema by looked for a class with tree_root: true. Defaults to None.

  • strict – If True, validation will stop after the first validation error is found, Otherwise all validation problems will be reported. Defaults to False.

Returns:

A validation report

Return type:

ValidationReport

Loaders#

The linkml.validator.loaders package contain classes which are responsible for yielding data instance from a source. Instances of these classes are passed to linkml.validator.Validator.validate_source() and linkml.validator.Validator.iter_results_from_source()

class linkml.validator.loaders.CsvLoader(source, *, skip_empty_rows: bool = False, index_slot_name: Optional[str] = None)[source]#

A loader for instances serialized as CSV

Parameters:
  • skip_empty_rows – If True, skip empty rows instead of yielding empty dicts. Defaults to False.

  • index_slot_name – If provided, iter_instances will yield one dict where all rows of the CSV file are collected into a list with index_slot_name as the key. If None, iter_instances will yield each row as a dict individually. Defaults to None.

class linkml.validator.loaders.JsonLoader(source)[source]#

A loader for instances serialized as JSON

Parameters:

source – Path or URL to JSON source

class linkml.validator.loaders.Loader(source)[source]#

Abstract base class for instance data loaders.

Subclasses must implement the iter_instances method.

Parameters:

source – Path or URL to load instances from

class linkml.validator.loaders.TsvLoader(source, *, skip_empty_rows: bool = False, index_slot_name: Optional[str] = None)[source]#

A loader for instances serialized as TSV

Parameters:
  • skip_empty_rows – If True, skip empty rows instead of yielding empty dicts. Defaults to False.

  • index_slot_name – If provided, iter_instances will yield one dict where all rows of the TSV file are collected into a list with index_slot_name as the key. If None, iter_instances will yield each row as a dict individually. Defaults to None.

class linkml.validator.loaders.YamlLoader(source)[source]#

A loader for instances serialized as YAML

Parameters:

source – Path to YAML source

Plugins#

The linkml.validator.plugins package contains classes that perform the actual validation work on data instances. Instances of these classes should be provided when constructing a linkml.validator.Validator instance.

class linkml.validator.plugins.JsonschemaValidationPlugin(*, closed: bool = False, include_range_class_descendants: bool = True, json_schema_path: Optional[PathLike] = None)[source]#

A validation plugin which validates instances using a JSON Schema validator.

Parameters:
  • closed – If True, additional properties are not allowed on instances. Defaults to False.

  • include_range_class_descendants – If True, use an open world assumption and allow the range of a slot to be any descendant of the declared range. Note that if the range of a slot has a type designator, descendants will always be included.

  • json_schema_path – If provided, JSON Schema will not be generated from the schema, instead it will be read from this path. In this case the value of the closed argument is disregarded and the open- or closed-ness of the existing JSON Schema is taken as-is.

class linkml.validator.plugins.PydanticValidationPlugin(closed: bool = False)[source]#

A validation plugin which validates instances using a Pydantic validator.

Note that this plugin provides less complete validation than JsonschemaValidationPlugin. Also, due to the nature of Pydantic, it will fail fast on errors and only report the first error found.

For general use cases, JsonschemaValidationPlugin is recommended. However, this plugin may be useful in some scenarios:

  • You are using in a pipeline to ensure objects will be valid for loading into Pydantic.

  • You are exploring relative capabilities of Pydantic and JSON Schema validation.

  • Pydantic is faster for your use case (to be tested).

Parameters:

closed – If True, additional properties are not allowed on instances. Defaults to False.

class linkml.validator.plugins.RecommendedSlotsPlugin[source]#

A validation plugin which validates that recommended slots are populated

class linkml.validator.plugins.ValidationPlugin[source]#

Abstract base class for validation plugins.

Subclasses must implement a process method.