diff --git a/docs/source/item-validation.rst b/docs/source/item-validation.rst index 8f3a0606..5d3d7c02 100644 --- a/docs/source/item-validation.rst +++ b/docs/source/item-validation.rst @@ -141,6 +141,17 @@ as a `dict`: OtherItem: '/path/to/otheritem_schema.json', } +Keys of ``dict`` can also be strings matching item class names: + +.. code-block:: python + + SPIDERMON_VALIDATION_SCHEMAS = { + 'DummyItem': '/path/to/dummyitem_schema.json', + 'OtherItem': '/path/to/otheritem_schema.json', + } + + + Validation in Monitors ---------------------- diff --git a/spidermon/contrib/scrapy/pipelines.py b/spidermon/contrib/scrapy/pipelines.py index d86fcdc6..96ad572a 100644 --- a/spidermon/contrib/scrapy/pipelines.py +++ b/spidermon/contrib/scrapy/pipelines.py @@ -56,7 +56,7 @@ def set_validators(loader, schema): if type(schema) in (list, tuple): schema = {Item: schema} for obj, paths in schema.items(): - key = obj.__name__ + key = obj.__name__ if hasattr(obj, "__name__") else str(obj) paths = paths if type(paths) in (list, tuple) else [paths] objects = [loader(v) for v in paths] validators[key].extend(objects) diff --git a/tests/contrib/scrapy/test_pipelines.py b/tests/contrib/scrapy/test_pipelines.py index d2a7ff99..896eaa99 100644 --- a/tests/contrib/scrapy/test_pipelines.py +++ b/tests/contrib/scrapy/test_pipelines.py @@ -203,6 +203,18 @@ class PipelineJSONSchemaValidator(PipelineTest): assert_type_in_stats(TreeItem), ], ), + DataTest( + name="validators specified by str rather than class", + item=TreeItem(), + settings={ + SETTING_SCHEMAS: {"TestItem": test_schema, "TreeItem": tree_schema} + }, + cases=[ + f"{{stats}}['{STATS_MISSINGS}'] is 1", + assert_type_in_stats(TestItem), + assert_type_in_stats(TreeItem), + ], + ), ]