Skip to content

Commit 9d0e07f

Browse files
committed
[tutorial] Add validation introduction/example
1 parent 8e292c8 commit 9d0e07f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

doc/tutorial.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,49 @@ A cardinality is set via its convenience method:
983983
>>> # or
984984
>>> prop.val_cardinality = None
985985

986+
Working with Validations
987+
------------------------
988+
989+
odML Validations are a set of pre-defined checks that are run against an odML document automatically when it is saved or loaded. A document cannot be saved, if a Validation fails a check that is classified as an Error. Most validation checks are Warnings that are supposed to raise the overall data quality of the odml Document.
990+
991+
When an odML document is saved or loaded, tha automatic validation will print a short report of encountered Validation Warnings and it is up to the user whether they want to resolve the Warnings. The odML document provides the ``validate`` method to gain easy access to the default validations. A Validation in turn provides not only a specific description of all encountered warnings or errors within an odML document, but it also provides direct access to each and every odML entity i.e. an odml.Section or an odml.Property where am issue has been found. This enables the user to quickly access and fix an encountered issue.
992+
993+
A minimal example shows how a workflow using default validations might look like:
994+
995+
>>> # Create a minimal document with Section issues: name and type are not assigned
996+
>>> doc = odml.Document()
997+
>>> sec = odml.Section(parent=doc)
998+
>>> odml.save(doc, "validation_example.odml.xml")
999+
1000+
This minimal example document will be saved, but will also print the following Validation report:
1001+
1002+
>>> UserWarning: The saved Document contains unresolved issues. Run the Documents 'validate' method to access them.
1003+
>>> Validation found 0 errors and 2 warnings in 1 Sections and 0 Properties.
1004+
1005+
To fix the encountered warnings, users can access the validation via the documents' ``validate`` method:
1006+
1007+
>>> validation = doc.validate()
1008+
>>> for issue in validation.errors:
1009+
>>> print(issue)
1010+
1011+
This will show that the validation has encountered two Warnings and also displays the offending odml entity.
1012+
1013+
>>> ValidationWarning: Section[73f29acd-16ae-47af-afc7-371d57898e28] 'Section type not specified'
1014+
>>> ValidationWarning: Section[73f29acd-16ae-47af-afc7-371d57898e28] 'Name not assigned'
1015+
1016+
To fix the "Name not assigned" warning the Section can be accessed via the validation entry and used to directly assign a human readable name to Section in the original document. Re-running the validation will show, that the warning has been removed.
1017+
1018+
>>> validation.errors[1].obj.name = "validation_example_section"
1019+
>>> # Check that the section name has been changed in the document
1020+
>>> print(doc.sections)
1021+
>>> # Re-running validation
1022+
>>> validation = doc.validate()
1023+
>>> for issue in validation.errors:
1024+
>>> print(issue)
1025+
1026+
Similarly the second validation warning can be resolved before saving the document again.
1027+
1028+
Please note that the automatic validation is run whenever a document is saved or loaded using the ``odml.save`` and ``odml.load`` functions as well as the ``ODMLWriter`` or the ``ODMLReader``. The validation is not run when using any of the lower level ``xmlparser``, ``dict_parser`` or ``rdf_converter`` classes.
9861029

9871030
Advanced knowledge on Values
9881031
----------------------------

0 commit comments

Comments
 (0)