Skip to content

Commit a0b2d0d

Browse files
committed
[tutorial] Add custom validation section
1 parent b6d6ce0 commit a0b2d0d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

doc/tutorial.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,40 @@ The following contains a list of the default odml validations, their message and
11071107
| Applies to: ``Section``
11081108
| Course of action: Optional validation. Will report any section that does not specify a repository. Add a repository to the reported Section to resolve.
11091109
1110+
Custom validations
1111+
******************
1112+
1113+
Users can write their own validation and register them either with the default validation or add it to their own validation class instance.
1114+
1115+
A custom validation handler needs to ``yield`` a ``ValidationError``. See the ``validation.ValidationError`` class for details.
1116+
1117+
Custom validation handlers can be registered to be applied on ``odML`` (the odml Document), ``section`` or ``property``.
1118+
1119+
>>> import odml
1120+
>>> import odml.validation as oval
1121+
>>>
1122+
>>> # Create an example document
1123+
>>> doc = odml.Document()
1124+
>>> sec_valid = odml.Section(name="Recording-20200505", parent=doc)
1125+
>>> sec_invalid = odml.Section(name="Movie-20200505", parent=doc)
1126+
>>> subsec = odml.Section(name="Sub-Movie-20200505", parent=sec_valid)
1127+
>>>
1128+
>>> # Define a validation handler that yields a ValidationError if a section name does not start with 'Recording-'
1129+
>>> def custom_validation_handler(obj):
1130+
>>> validation_id = oval.IssueID.custom_validation
1131+
>>> msg = "Section name does not start with 'Recording-'"
1132+
>>> if not obj.name.startswith("Recording-"):
1133+
>>> yield oval.ValidationError(obj, msg, oval.LABEL_ERROR, validation_id)
1134+
>>>
1135+
>>> # Create a custom, empty validation with an odML document 'doc'
1136+
>>> custom_validation = oval.Validation(doc, reset=True)
1137+
>>> # Register a custom validation handler that should be applied on all Sections of a Document
1138+
>>> custom_validation.register_custom_handler("section", custom_validation_handler)
1139+
>>> # Run the custom validation and return a report
1140+
>>> custom_validation.report()
1141+
>>> # Display the errors reported by the validation
1142+
>>> print(custom_validation.errors)
1143+
11101144
Advanced knowledge on Values
11111145
----------------------------
11121146

0 commit comments

Comments
 (0)