Skip to content

Commit

Permalink
Allow per item validators to be specified by string
Browse files Browse the repository at this point in the history
  • Loading branch information
rvandam authored Sep 13, 2023
1 parent 6edb037 commit 6c1a7d8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/source/item-validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------

Expand Down
2 changes: 1 addition & 1 deletion spidermon/contrib/scrapy/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 if type(obj) in (str) else obj.__name__
paths = paths if type(paths) in (list, tuple) else [paths]
objects = [loader(v) for v in paths]
validators[key].extend(objects)
Expand Down
10 changes: 10 additions & 0 deletions tests/contrib/scrapy/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ 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),
],
),
]


Expand Down

0 comments on commit 6c1a7d8

Please sign in to comment.