API Guide¶
Main Validator¶
The IIIFValidator
is the root validator, responsible for storing all settings
and references to IIIF resource validators. If you are only using Tripoli to debug your own
manifest creation algorithms, it should be the only class you need to know about.
-
class
tripoli.
IIIFValidator
(debug=False, collect_warnings=True, collect_errors=True, fail_fast=True, verbose=False, unique_logging=True)¶ -
AnnotationValidator
¶ An instance of an AnnotationValidator
-
CanvasValidator
¶ An instance of a CanvasValidator.
-
ImageContentValidator
¶ An instance of an ImageContentValidator
-
ManifestValidator
¶ An instance of a ManifestValidator.
-
SequenceValidator
¶ An instance of a SequenceValidator.
-
collect_errors
= True¶ Sets whether or not errors are logged.
-
collect_warnings
= True¶ Sets whether or not warnings are logged. Default
True
.
-
corrected_doc
= None¶ If corrections were made during validation, the corrected document will be placed here.
-
debug
= False¶ Sets whether or not to save tracebacks in warnings/errors.
-
errors
¶ A list of ValidatorLogError from the previous call to validate().
-
fail_fast
= True¶ When
True
, validation stops at first error hit (faster). IfFalse
, entire document will always be validated.Note: Turning
fail_fast
off may cause the validator to raise unexpected exceptions if the the document is grossly invalid (for instance, if an integer is supplied where a list is expected).
-
logger
= None¶ logging.getLogger()
used to print output.
-
print_errors
()¶ Print accumulated errors in a nice format.
-
print_warnings
()¶ Print accumulated warnings in a nice format.
-
unique_logging
= True¶ If
True
, only one instance of duplicate logged messages will be saved. IfFalse
, all logged messages will be saved.Example: If set to true, then if every canvas has error A, instead of having the errors (Error(A, canvas[0]), Error(A, canvas[1]), …), you will only get Error(A, canvas[0]) (the first error of type A on a canvas).
-
validate
(json_dict, **kwargs)¶ Determine the correct validator and validate a resource.
Parameters: json_dict – A dict or str of a json resource.
-
verbose
= False¶ If
True
, prints all errors and warnings as they occur. IfFalse
, errors and warnings only printed after de-duplication.
-
warnings
¶ A list of ValidatorLogWarnings from the previous call to validate().
-
Error and Warning Logging¶
IIIF Resource Validators¶
All validators inherit from the BaseValidator
class. This, and all validators,
requires a reference to a IIIFValidator
to be initialized, as all settings and
references to other validators are held therein.
-
class
tripoli.resource_validators.
BaseValidator
(iiif_validator=None)¶ Defines basic validation behaviour and expected attributes of any IIIF validators that inherit from it.
-
FORBIDDEN_FIELDS
= set()¶ The fields which are forbidden on this resource.
-
KNOWN_FIELDS
= set()¶ The fields which may appear on this resource.
-
RECOMMENDED_FIELDS
= set()¶ The fields which are recommended on this resource.
-
REQUIRED_FIELDS
= set()¶ The fields which are required on this resource.
-
attribution_field
(value)¶ Validate the
attribution
field of the resource.
-
description_field
(value)¶ Validate the
description
field of the resource.
-
errors
¶ A list of ValidatorLogError from the previous call to validate().
-
static
errors_to_warnings
(fn)¶ Cast any errors to warnings on any
*_field
or*_type
function.Works by patching the BaseValidator.log_error to refer to BaseValidator.log_warning. These methods should not be overridden in children.
-
height_field
(value)¶ Validate
height
field.
-
id_field
(value)¶ Validate the
@id
field of the resource.
-
label_field
(value)¶ Validate the
label
field of the resource.
-
license_field
(value)¶ Validate the
license
field of the resource.
-
log_error
(field, msg)¶ Add an error to the validator.
This method should not be overridden in subclasses, as doing so is likely to break the error and warning coercion decorators.
Parameters: - field – The field the error was raised on.
- msg – The message to associate with the error.
-
log_warning
(field, msg)¶ Add a warning to the validator if warnings are being caught.
This method should not be overridden in subclasses, as doing so is likely to break the error and warning coercion decorators.
Parameters: - field – The field the warning was raised on.
- msg – The message to associate with the warning.
-
logo_field
(value)¶ Validate the
logo
field of the resource.
-
metadata_field
(value)¶ Validate the metadata field of the resource.
Recurse into keys/values and checks that they are properly formatted.
-
modify_final_return
(validation_results)¶ Do any final corrections or checks on a block before it is returned.
This method is passed whatever value the validator is about to return to it’s caller. Here you can check for missing keys, compare neighbours, make modifications or additions: anything you’d like to check or correct before return.
Parameters: validation_results – A dict representing a json object. Return (dict): The sole argument, with some modification applied to it.
-
mute_errors
(fn, *args, **kwargs)¶ Run given function and catch any of the errors it logged.
The self._errors key will not be changed by using this function.
Works by patching the BaseValidator.log_error function. BaseValidator.log_error should not be overridden in children.
-
print_errors
()¶ Print accumulated errors in a nice format.
-
print_warnings
()¶ Print accumulated warnings in a nice format.
Validate the
related
field of the resource.
-
rendering_field
(value)¶ Validate the
rendering
field of the resource.
-
seeAlso_field
(value)¶ Validate the
seeAlso
field of the resource.
-
service_field
(value)¶ Validate the
service
field of the resource.
-
thumbnail_field
(value)¶ Validate the
thumbnail
field of the resource.
-
type_field
(value)¶ Validate the
@type
field of the resource.
-
viewing_dir_field
(value)¶ Validate
viewingDir
field againstVIEW_DIRS
set.
-
viewing_hint_field
(value)¶ Validate
viewingHint
field againstVIEW_HINTS
set.
-
warnings
¶ A list of ValidatorLogWarnings from the previous call to validate().
-
static
warnings_to_errors
(fn)¶ Cast any warnings to errors on any
*_field
or*_type
function.Works by patching the BaseValidator.log_warning to refer to BaseValidator.log_error. These methods should not be overridden in children.
-
width_field
(value)¶ Validate
width
field.
-
within_field
(value)¶ Validate the
within
field of the resource.
-
-
class
tripoli.resource_validators.
ManifestValidator
(iiif_validator)¶ Bases:
tripoli.resource_validators.base_validator.BaseValidator
-
context_field
(value)¶ Assert that
@context
is the IIIF 2.0 presentation API.
-
sequences_field
(value)¶ Validate
sequences
list for Manifest.Checks that at least 1 sequence is embedded.
-
structures_field
(value)¶ Validate the
structures
field.
-
type_field
(value)¶ Assert that
@type
==sc:Manifest
.
-
-
class
tripoli.resource_validators.
SequenceValidator
(iiif_validator)¶ Bases:
tripoli.resource_validators.base_validator.BaseValidator
-
canvases_field
(value)¶ Validate
canvases
list for Sequence.
-
context_field
(value)¶ Assert that
@context
is the IIIF 2.0 presentation API if it is allowed.
-
startCanvas_field
(value)¶ Validate
startCanvas
field.
-
type_field
(value)¶ Assert that
@type
==sc:Sequence
-
-
class
tripoli.resource_validators.
CanvasValidator
(iiif_validator)¶ Bases:
tripoli.resource_validators.base_validator.BaseValidator
-
images_field
(value)¶ Validate
images
list.Calls a sub-validation procedure handled by the
AnnotationValidator
.
-
other_content_field
(value)¶ Validate
otherContent
field.
-
type_field
(value)¶ Assert that
@type == 'sc:Canvas
-
-
class
tripoli.resource_validators.
AnnotationValidator
(iiif_validator)¶ Bases:
tripoli.resource_validators.base_validator.BaseValidator
-
motivation_field
(value)¶ Assert that
motivation == 'sc:painting'
.
-
on_field
(value)¶ Validate the
on
field.
-
resource_field
(value)¶ Validate
resources
list.Calls a sub-validation procedure handled by the
ImageContentValidator
.
-
type_field
(value)¶ Assert that
@type == 'oa:Annotation'
.
-