linkml-validator


Namelinkml-validator JSON
Version 0.4.5 PyPI version JSON
download
home_pagehttps://github.com/linkml/linkml-validator
SummaryLinkML Validator
upload_time2022-12-06 16:08:46
maintainer
docs_urlNone
authorDeepak Unni
requires_python>=3.8
licenseBSD3
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # LinkML Validator

[![Run tests](https://github.com/linkml/linkml-validator/actions/workflows/run-tests.yml/badge.svg)](https://github.com/linkml/linkml-validator/actions/workflows/run-tests.yml)
[![PyPI](https://img.shields.io/pypi/v/linkml-validator)](https://img.shields.io/pypi/v/linkml-validator)

The LinkML Validator is a library for performing validation on data objects that
conform to a given LinkML schema.

The Validator is initialized using a LinkML schema YAML, and is designed to allow
for flexible validation where each type of validation is done by a plugin.

For example, JSONSchema validation is performed by
[JsonSchemaValidationPlugin](linkml_validator/plugins/jsonschema_validation.py).

## Motivation

The LinkML Validator is built with the following goals in mind:
- the Validator should respond with parseable validation messages
- the Validator should not break the validation process even if one
object from a list of objects fail validation
- the Validator should provide the ability to perform more than one
type of validation on an object



## Installation

```sh
python setup.py install
```

To install development dependencies (like `pytest`, `mkdocs`, etc.):

```sh
pip install -e ".[dev]"
```

## Running the LinkML Validator via CLI

To run the LinkML Validator,

```sh
linkml-validator --inputs <INPUT JSON> \
    --schema <SCHEMA YAML> \
    --output <OUTPUT>
```

You can pass filepath or a URL that points to the LinkML schema YAML.


### Input data as a dictionary of objects

The input JSON can be a dictionary of objects keyed by the object type.

```json
{
    "<OBJECT_TYPE>": [
        {

        }
    ]
}
```

Where the `<OBJECT_TYPE>` is the pythonic representation of a class defined in the schema YAML.

For example, consider [examples/example_data1.json](examples/example_data1.json):

```json
{
    "NamedThing": [
        {
            "id": "obj1",
            "name": "Object 1",
            "type": "X"
        },
        {
            "id": "obj2",
            "name": "Object 2",
            "type": "Y"
        }
    ]
}
```

In the above example, the `NamedThing` is the `target_class`, which is the pythonic
representation of the class `named thing` as defined in the
[examples/example_schema.yaml](examples/example_schema.yaml).

You can run the validator on the above data as follows:

```sh
linkml-validator --inputs examples/example_data1.json \
    --schema examples/example_schema.yaml \
    --output examples/example_data1_validation_report.json
```


### Input data as an array of objects

The input JSON can also be an array of objects:

```json
[
    {},
    {}
]
```

In this case, one must also specify the object type via `--target-class` argument in the CLI.

For example, consider [examples/example_data2.json](examples/example_data2.json):

```json
[
    {
        "id": "obj1",
        "name": "Object 1",
        "type": "X"
    },
    {
        "id": "obj2",
        "name": "Object 2",
        "type": "Y"
    }
]
```

You can run the validator on the above data as follows:

```sh
linkml-validator --inputs examples/example_data2.json \
    --schema examples/example_schema.yaml \
    --output examples/example_data2_validation_report.json \
    --target-class NamedThing
```


## Running selected plugins

To run only certain plugins as part of the validation,

```sh
linkml-validator --inputs data.json \
    --schema schema.yaml \
    --output validation_results.json \
    --plugins JsonSchemaValidationPlugin
```

To perform strict validation,

```sh
linkml-validator --inputs data.json \
    --schema schema.yaml \
    --output validation_results.json \
    --plugins JsonSchemaValidationPlugin \
    --strict
```

Under normal (default) mode, the validator will run all the checks defined in all
referenced plugins on a given object.

When in strict mode, the validator will stop the validation for an object if even one
of the plugins report a failed validation.

## Running your own plugins with the Validator (via CLI)

To run your custom plugin as part of the validation,

```sh
linkml-validator --inputs data.json \
    --schema schema.yaml \
    --output validation_results.json \
    --plugins JsonSchemaValidationPlugin \
    --plugins <CUSTOM_PLUGIN_CLASS>
```
where `<CUSTOM_PLUGIN_CLASS>` the reference to a custom plugin class.

**Note:** The custom plugin class must be a subclass of `linkml_validator.plugins.base.BasePlugin` and must implement all the methods defined in `BasePlugin` class.


## Using LinkML Validator as a module

You can use the `linkml_validator.validator.Validator` class directly in your codebase
to perform validation on objects that you are working with.

The following code snippet provides a quick way of instantiating the Validator class
and performing validation on an object:

```py
from linkml_validator.validator import Validator

data_obj = {
    "id": "obj1",
    "name": "Object 1",
    "type": "X"
}
validator = Validator(schema="examples/example_schema.yaml")
validator.validate(obj=data_obj, target_class="NamedThing")
```

**Note:** The above code makes the assumption that there is a class `named thing` defined
in the [examples/example_schema.yaml](examples/example_schema.yaml) and that `NamedThing`
is its Pythonic representation.


You can also provide your own custom plugin class to run with the Validator,

```py
from linkml_validator.validator import Validator
from linkml_validator.plugins.base import BasePlugin
from linkml_validator.models import ValidationResult

class MyCustomPlugin(BasePlugin):
    NAME = "MyCustomPlugin"

    def __init__(self, schema: str, **kwargs) -> None:
        super().__init__(schema)

    def process(self, obj: dict, **kwargs) -> ValidationResult:
        # Add your custom logic for processing and validating the incoming object
        valid = False
        print("In MyCustomPlugin.process method")
        result = ValidationResult(
            plugin_name=self.NAME,
            valid=valid,
            validation_messages=[]
        )
        return result

data_obj = {
    "id": "obj1",
    "name": "Object 1",
    "type": "X"
}
validator = Validator(schema="examples/example_schema.yaml", plugins=[{"plugin_class": "MyCustomPlugin", "args": {}])
validator.validate(obj=data_obj, target_class="NamedThing")

```




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/linkml/linkml-validator",
    "name": "linkml-validator",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "Deepak Unni",
    "author_email": "deepak.unni3@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d6/d7/2b67879fdd0b77e2451e073ac62d1284b6e88c85ceb919dbad2bf487d6b9/linkml_validator-0.4.5.tar.gz",
    "platform": null,
    "description": "# LinkML Validator\n\n[![Run tests](https://github.com/linkml/linkml-validator/actions/workflows/run-tests.yml/badge.svg)](https://github.com/linkml/linkml-validator/actions/workflows/run-tests.yml)\n[![PyPI](https://img.shields.io/pypi/v/linkml-validator)](https://img.shields.io/pypi/v/linkml-validator)\n\nThe LinkML Validator is a library for performing validation on data objects that\nconform to a given LinkML schema.\n\nThe Validator is initialized using a LinkML schema YAML, and is designed to allow\nfor flexible validation where each type of validation is done by a plugin.\n\nFor example, JSONSchema validation is performed by\n[JsonSchemaValidationPlugin](linkml_validator/plugins/jsonschema_validation.py).\n\n## Motivation\n\nThe LinkML Validator is built with the following goals in mind:\n- the Validator should respond with parseable validation messages\n- the Validator should not break the validation process even if one\nobject from a list of objects fail validation\n- the Validator should provide the ability to perform more than one\ntype of validation on an object\n\n\n\n## Installation\n\n```sh\npython setup.py install\n```\n\nTo install development dependencies (like `pytest`, `mkdocs`, etc.):\n\n```sh\npip install -e \".[dev]\"\n```\n\n## Running the LinkML Validator via CLI\n\nTo run the LinkML Validator,\n\n```sh\nlinkml-validator --inputs <INPUT JSON> \\\n    --schema <SCHEMA YAML> \\\n    --output <OUTPUT>\n```\n\nYou can pass filepath or a URL that points to the LinkML schema YAML.\n\n\n### Input data as a dictionary of objects\n\nThe input JSON can be a dictionary of objects keyed by the object type.\n\n```json\n{\n    \"<OBJECT_TYPE>\": [\n        {\n\n        }\n    ]\n}\n```\n\nWhere the `<OBJECT_TYPE>` is the pythonic representation of a class defined in the schema YAML.\n\nFor example, consider [examples/example_data1.json](examples/example_data1.json):\n\n```json\n{\n    \"NamedThing\": [\n        {\n            \"id\": \"obj1\",\n            \"name\": \"Object 1\",\n            \"type\": \"X\"\n        },\n        {\n            \"id\": \"obj2\",\n            \"name\": \"Object 2\",\n            \"type\": \"Y\"\n        }\n    ]\n}\n```\n\nIn the above example, the `NamedThing` is the `target_class`, which is the pythonic\nrepresentation of the class `named thing` as defined in the\n[examples/example_schema.yaml](examples/example_schema.yaml).\n\nYou can run the validator on the above data as follows:\n\n```sh\nlinkml-validator --inputs examples/example_data1.json \\\n    --schema examples/example_schema.yaml \\\n    --output examples/example_data1_validation_report.json\n```\n\n\n### Input data as an array of objects\n\nThe input JSON can also be an array of objects:\n\n```json\n[\n    {},\n    {}\n]\n```\n\nIn this case, one must also specify the object type via `--target-class` argument in the CLI.\n\nFor example, consider [examples/example_data2.json](examples/example_data2.json):\n\n```json\n[\n    {\n        \"id\": \"obj1\",\n        \"name\": \"Object 1\",\n        \"type\": \"X\"\n    },\n    {\n        \"id\": \"obj2\",\n        \"name\": \"Object 2\",\n        \"type\": \"Y\"\n    }\n]\n```\n\nYou can run the validator on the above data as follows:\n\n```sh\nlinkml-validator --inputs examples/example_data2.json \\\n    --schema examples/example_schema.yaml \\\n    --output examples/example_data2_validation_report.json \\\n    --target-class NamedThing\n```\n\n\n## Running selected plugins\n\nTo run only certain plugins as part of the validation,\n\n```sh\nlinkml-validator --inputs data.json \\\n    --schema schema.yaml \\\n    --output validation_results.json \\\n    --plugins JsonSchemaValidationPlugin\n```\n\nTo perform strict validation,\n\n```sh\nlinkml-validator --inputs data.json \\\n    --schema schema.yaml \\\n    --output validation_results.json \\\n    --plugins JsonSchemaValidationPlugin \\\n    --strict\n```\n\nUnder normal (default) mode, the validator will run all the checks defined in all\nreferenced plugins on a given object.\n\nWhen in strict mode, the validator will stop the validation for an object if even one\nof the plugins report a failed validation.\n\n## Running your own plugins with the Validator (via CLI)\n\nTo run your custom plugin as part of the validation,\n\n```sh\nlinkml-validator --inputs data.json \\\n    --schema schema.yaml \\\n    --output validation_results.json \\\n    --plugins JsonSchemaValidationPlugin \\\n    --plugins <CUSTOM_PLUGIN_CLASS>\n```\nwhere `<CUSTOM_PLUGIN_CLASS>` the reference to a custom plugin class.\n\n**Note:** The custom plugin class must be a subclass of `linkml_validator.plugins.base.BasePlugin` and must implement all the methods defined in `BasePlugin` class.\n\n\n## Using LinkML Validator as a module\n\nYou can use the `linkml_validator.validator.Validator` class directly in your codebase\nto perform validation on objects that you are working with.\n\nThe following code snippet provides a quick way of instantiating the Validator class\nand performing validation on an object:\n\n```py\nfrom linkml_validator.validator import Validator\n\ndata_obj = {\n    \"id\": \"obj1\",\n    \"name\": \"Object 1\",\n    \"type\": \"X\"\n}\nvalidator = Validator(schema=\"examples/example_schema.yaml\")\nvalidator.validate(obj=data_obj, target_class=\"NamedThing\")\n```\n\n**Note:** The above code makes the assumption that there is a class `named thing` defined\nin the [examples/example_schema.yaml](examples/example_schema.yaml) and that `NamedThing`\nis its Pythonic representation.\n\n\nYou can also provide your own custom plugin class to run with the Validator,\n\n```py\nfrom linkml_validator.validator import Validator\nfrom linkml_validator.plugins.base import BasePlugin\nfrom linkml_validator.models import ValidationResult\n\nclass MyCustomPlugin(BasePlugin):\n    NAME = \"MyCustomPlugin\"\n\n    def __init__(self, schema: str, **kwargs) -> None:\n        super().__init__(schema)\n\n    def process(self, obj: dict, **kwargs) -> ValidationResult:\n        # Add your custom logic for processing and validating the incoming object\n        valid = False\n        print(\"In MyCustomPlugin.process method\")\n        result = ValidationResult(\n            plugin_name=self.NAME,\n            valid=valid,\n            validation_messages=[]\n        )\n        return result\n\ndata_obj = {\n    \"id\": \"obj1\",\n    \"name\": \"Object 1\",\n    \"type\": \"X\"\n}\nvalidator = Validator(schema=\"examples/example_schema.yaml\", plugins=[{\"plugin_class\": \"MyCustomPlugin\", \"args\": {}])\nvalidator.validate(obj=data_obj, target_class=\"NamedThing\")\n\n```\n\n\n\n",
    "bugtrack_url": null,
    "license": "BSD3",
    "summary": "LinkML Validator",
    "version": "0.4.5",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "7c14e8848bf595b56a4bd4b3bec4ae73",
                "sha256": "7a9799830cba3e1df203cde86263d8ea85f9ed9f6a45a218e3f752483abac030"
            },
            "downloads": -1,
            "filename": "linkml_validator-0.4.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7c14e8848bf595b56a4bd4b3bec4ae73",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 14320,
            "upload_time": "2022-12-06T16:08:44",
            "upload_time_iso_8601": "2022-12-06T16:08:44.803740Z",
            "url": "https://files.pythonhosted.org/packages/87/e1/4a9473997ae94d899008b9ae0c5da0702c7dfdc9ee1dd53c0a00c86873cf/linkml_validator-0.4.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "5a07b109dd32dc34e1dd8f048e49fbe2",
                "sha256": "bce72053c1593a678ccefd28ed5cf4b39a878e78da884b5b41fef65610a23867"
            },
            "downloads": -1,
            "filename": "linkml_validator-0.4.5.tar.gz",
            "has_sig": false,
            "md5_digest": "5a07b109dd32dc34e1dd8f048e49fbe2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12126,
            "upload_time": "2022-12-06T16:08:46",
            "upload_time_iso_8601": "2022-12-06T16:08:46.038304Z",
            "url": "https://files.pythonhosted.org/packages/d6/d7/2b67879fdd0b77e2451e073ac62d1284b6e88c85ceb919dbad2bf487d6b9/linkml_validator-0.4.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-06 16:08:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "linkml",
    "github_project": "linkml-validator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "linkml-validator"
}
        
Elapsed time: 0.04421s