From c0885699767cffee8308665bfe45a79f3b8e683d Mon Sep 17 00:00:00 2001 From: HosniBelfeki Date: Wed, 22 Jul 2026 02:02:30 +0100 Subject: [PATCH] fix(python): validate query field names --- python/tests/test_params.py | 7 ++++--- python/zvec/model/param/query.py | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/python/tests/test_params.py b/python/tests/test_params.py index 8f613939f..f83c7cdb5 100644 --- a/python/tests/test_params.py +++ b/python/tests/test_params.py @@ -408,9 +408,10 @@ def test_init_both_id_and_vector_raises_error(self): with pytest.raises(ValueError): Query(field_name="embedding", id="doc123", vector=[0.1])._validate() - def test_init_without_field_name_raises_error(self): - with pytest.raises(ValueError): - Query(field_name=None)._validate() + @pytest.mark.parametrize("field_name", [None, "", " ", 123]) + def test_init_without_valid_field_name_raises_error(self, field_name): + with pytest.raises(ValueError, match="Field name must be a non-empty string"): + Query(field_name=field_name)._validate() def test_has_id_returns_true_when_id_set(self): vq = Query(field_name="embedding", id="doc123") diff --git a/python/zvec/model/param/query.py b/python/zvec/model/param/query.py index d0916b3e3..fd46d2caa 100644 --- a/python/zvec/model/param/query.py +++ b/python/zvec/model/param/query.py @@ -115,8 +115,8 @@ def has_fts(self) -> bool: return False def _validate(self) -> None: - if self.field_name is None: - raise ValueError("Field name cannot be empty") + if not isinstance(self.field_name, str) or not self.field_name.strip(): + raise ValueError("Field name must be a non-empty string") if self.has_id() and self.has_vector(): raise ValueError("Cannot provide both id and vector") if self.has_fts() and (self.has_vector() or self.has_id()):