Skip to content

Commit bfdbefa

Browse files
dalpassosbrunato
authored andcommitted
feat(core): search validation (#1877)
1 parent c7d4dcc commit bfdbefa

File tree

13 files changed

+351
-586
lines changed

13 files changed

+351
-586
lines changed

docs/notebooks/api_user_guide/3_search.ipynb

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@
7474
"}"
7575
]
7676
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"A set of default values can be configured for each product type of a provider. This is configured in `providers.yml` as described in the section [\"Add a product type\"](../../add_product_type.rst). No default value is available for all ECMWF-like providers: `ecmwf`, `cop_ads`, `cop_cds`, `meteoblue`, `wekeo_ecmwf`, `dedt_lumi`, and `cop_ewds`. The [`list_queryables`](5_queryables.ipynb) method can be used to know which query parameters can be used to filter the search result."
82+
]
83+
},
7784
{
7885
"cell_type": "markdown",
7986
"metadata": {},
@@ -4148,6 +4155,7 @@
41484155
"* `geom`\n",
41494156
"* `locations`\n",
41504157
"* `count`\n",
4158+
"* `validate`\n",
41514159
"\n",
41524160
"More parameters can be passed through *kwargs*, they are also described below.\n",
41534161
"\n",
@@ -5431,6 +5439,104 @@
54315439
"print(f\"number_matched: {results_with_count.number_matched}\")"
54325440
]
54335441
},
5442+
{
5443+
"cell_type": "markdown",
5444+
"metadata": {},
5445+
"source": [
5446+
"### Validation\n",
5447+
"\n",
5448+
"The parameter `validate` can be used to automatically validate a search request before sending it to the provider. The validation is **enabled by default**."
5449+
]
5450+
},
5451+
{
5452+
"cell_type": "markdown",
5453+
"metadata": {},
5454+
"source": [
5455+
"Supported or required search parameters can be checked using [list_queryables()](./4_queryables.ipynb) method."
5456+
]
5457+
},
5458+
{
5459+
"cell_type": "markdown",
5460+
"metadata": {},
5461+
"source": [
5462+
"The following example illustrate a search request using invalid parameters.\n",
5463+
"Using validation, the search request returns 0 products and an exception is raised with information about the invalid or missing parameters."
5464+
]
5465+
},
5466+
{
5467+
"cell_type": "code",
5468+
"execution_count": 12,
5469+
"metadata": {},
5470+
"outputs": [
5471+
{
5472+
"name": "stderr",
5473+
"output_type": "stream",
5474+
"text": [
5475+
"2025-10-06 18:50:37,464 eodag.core [INFO ] Searching on provider cop_cds\n",
5476+
"2025-10-06 18:50:37,481 eodag.core [ERROR ] Error while searching on provider cop_cds (ignored):\n",
5477+
"Traceback (most recent call last):\n",
5478+
" File \"/home/ndalpasso/repos/CS-SI/eodag/eodag/plugins/search/base.py\", line 425, in validate\n",
5479+
" ).get_model().model_validate(filter)\n",
5480+
" ~~~~~~~~~~~~~~^^^^^^^^\n",
5481+
" File \"/home/ndalpasso/.pyenv/versions/venv-eodag-3.13/lib/python3.13/site-packages/pydantic/main.py\", line 705, in model_validate\n",
5482+
" return cls.__pydantic_validator__.validate_python(\n",
5483+
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^\n",
5484+
" obj, strict=strict, from_attributes=from_attributes, context=context, by_alias=by_alias, by_name=by_name\n",
5485+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
5486+
" )\n",
5487+
" ^\n",
5488+
"pydantic_core._pydantic_core.ValidationError: 2 validation errors for Queryables\n",
5489+
"ecmwf:variable\n",
5490+
" Field required [type=missing, input_value={'productType': 'AG_ERA5'...6_00', 'geometry': None}, input_type=dict]\n",
5491+
" For further information visit https://errors.pydantic.dev/2.11/v/missing\n",
5492+
"ecmwf:version\n",
5493+
" Field required [type=missing, input_value={'productType': 'AG_ERA5'...6_00', 'geometry': None}, input_type=dict]\n",
5494+
" For further information visit https://errors.pydantic.dev/2.11/v/missing\n",
5495+
"\n",
5496+
"The above exception was the direct cause of the following exception:\n",
5497+
"\n",
5498+
"Traceback (most recent call last):\n",
5499+
" File \"/home/ndalpasso/repos/CS-SI/eodag/eodag/api/core.py\", line 1889, in _do_search\n",
5500+
" search_plugin.validate(kwargs, prep.auth)\n",
5501+
" ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^\n",
5502+
" File \"/home/ndalpasso/repos/CS-SI/eodag/eodag/plugins/search/base.py\", line 427, in validate\n",
5503+
" raise ValidationError(format_pydantic_error(e)) from e\n",
5504+
"eodag.utils.exceptions.ValidationError: 2 error(s). ecmwf:variable: Field required; ecmwf:version: Field required\n"
5505+
]
5506+
},
5507+
{
5508+
"name": "stdout",
5509+
"output_type": "stream",
5510+
"text": [
5511+
"Invalid search criteria: got a hand on 0 products.\n"
5512+
]
5513+
}
5514+
],
5515+
"source": [
5516+
"invalid_search_criteria = {\n",
5517+
" \"productType\": \"AG_ERA5\",\n",
5518+
" \"provider\": \"cop_cds\",\n",
5519+
" \"day\": \"01\",\n",
5520+
" \"month\": \"01\",\n",
5521+
" \"time\": \"06_00\",\n",
5522+
"}\n",
5523+
"products = dag.search(validate=True, **invalid_search_criteria)\n",
5524+
"print(f\"Invalid search criteria: got a hand on {len(products)} products.\")"
5525+
]
5526+
},
5527+
{
5528+
"cell_type": "markdown",
5529+
"metadata": {},
5530+
"source": [
5531+
"<div class=\"alert alert-info\">\n",
5532+
"\n",
5533+
"Note\n",
5534+
"\n",
5535+
"By default errors are not raised, only logged. See [Errors handling](./3_search.ipynb#Error-handling) and use `raise_errors=True` to raise an exception on incorrect search parameters.\n",
5536+
"\n",
5537+
"</div>"
5538+
]
5539+
},
54345540
{
54355541
"cell_type": "markdown",
54365542
"metadata": {},

0 commit comments

Comments
 (0)