Package: linkml-validator
LinkML ValidatorThe 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 byJsonSchemaValidationPlugin.MotivationThe LinkML Validator is built with the following goals in mind:the Validator should respond with parseable validation messagesthe Validator should not break the validation process even if one
object from a list of objects fail validationthe Validator should provide the ability to perform more than one
type of validation on an objectInstallationpythonsetup.pyinstallTo install development dependencies (likepytest,mkdocs, etc.):pipinstall-e".[dev]"Running the LinkML Validator via CLITo run the LinkML Validator,linkml-validator--inputs<INPUTJSON>\--schema<SCHEMAYAML>\--output<OUTPUT>You can pass filepath or a URL that points to the LinkML schema YAML.Input data as a dictionary of objectsThe input JSON can be a dictionary of objects keyed by the object type.{"<OBJECT_TYPE>":[{}]}Where the<OBJECT_TYPE>is the pythonic representation of a class defined in the schema YAML.For example, considerexamples/example_data1.json:{"NamedThing":[{"id":"obj1","name":"Object 1","type":"X"},{"id":"obj2","name":"Object 2","type":"Y"}]}In the above example, theNamedThingis thetarget_class, which is the pythonic
representation of the classnamed thingas defined in theexamples/example_schema.yaml.You can run the validator on the above data as follows:linkml-validator--inputsexamples/example_data1.json\--schemaexamples/example_schema.yaml\--outputexamples/example_data1_validation_report.jsonInput data as an array of objectsThe input JSON can also be an array of objects:[{},{}]In this case, one must also specify the object type via--target-classargument in the CLI.For example, considerexamples/example_data2.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:linkml-validator--inputsexamples/example_data2.json\--schemaexamples/example_schema.yaml\--outputexamples/example_data2_validation_report.json\--target-classNamedThingRunning selected pluginsTo run only certain plugins as part of the validation,linkml-validator--inputsdata.json\--schemaschema.yaml\--outputvalidation_results.json\--pluginsJsonSchemaValidationPluginTo perform strict validation,linkml-validator--inputsdata.json\--schemaschema.yaml\--outputvalidation_results.json\--pluginsJsonSchemaValidationPlugin\--strictUnder 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,linkml-validator--inputsdata.json\--schemaschema.yaml\--outputvalidation_results.json\--pluginsJsonSchemaValidationPlugin\--plugins<CUSTOM_PLUGIN_CLASS>where<CUSTOM_PLUGIN_CLASS>the reference to a custom plugin class.Note:The custom plugin class must be a subclass oflinkml_validator.plugins.base.BasePluginand must implement all the methods defined inBasePluginclass.Using LinkML Validator as a moduleYou can use thelinkml_validator.validator.Validatorclass 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:fromlinkml_validator.validatorimportValidatordata_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 classnamed thingdefined
in theexamples/example_schema.yamland thatNamedThingis its Pythonic representation.You can also provide your own custom plugin class to run with the Validator,fromlinkml_validator.validatorimportValidatorfromlinkml_validator.plugins.baseimportBasePluginfromlinkml_validator.modelsimportValidationResultclassMyCustomPlugin(BasePlugin):NAME="MyCustomPlugin"def__init__(self,schema:str,**kwargs)->None:super().__init__(schema)defprocess(self,obj:dict,**kwargs)->ValidationResult:# Add your custom logic for processing and validating the incoming objectvalid=Falseprint("In MyCustomPlugin.process method")result=ValidationResult(plugin_name=self.NAME,valid=valid,validation_messages=[])returnresultdata_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")