Repository navigation
Raise ValidationError for invalid values in Django vector fields - #172
Open
mayuriphad wants to merge 2 commits into
Open
mayuriphad wants to merge 2 commits into
mayuriphad wants to merge 2 commits into
Conversation
VectorField, HalfVectorField and SparseVectorField passed malformed input straight to the parsers, so a ModelForm or serializer raised ValueError instead of reporting a field error. A value with the wrong number of dimensions passed validation and failed later with DataError from Postgres. Convert parse errors to ValidationError in to_python and add a validator that checks the dimensions when the field has them. Fixes pgvector#171 Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #171
VectorField,HalfVectorFieldandSparseVectorFieldpass form and serializer input straight to the text parsers. A malformed value madeform.is_valid()raiseValueError, and a value with the wrong number of dimensions passed validation and only failed onsave()with aDataErrorfrom Postgres. In both cases the request ended in a 500.Changes:
to_pythonturns parse errors intoValidationError(Enter a valid vector.and the half/sparse variants).max_lengthaddsMaxLengthValidator. It checks that the value converts to the vector type and, whendimensionsis set, that the length matches (Expected 3 dimensions, not 2.). Since it is invalidators, DRF'sModelFieldalso runs it, andArrayField(VectorField(dimensions=3))reports the bad item.dimensionsskip the length check.Tests: new cases in
tests/test_django.pyforfull_cleanand the three model forms, covering malformed input and wrong dimensions. The full suite, mypy andty checkpass on my fork's CI run (Postgres with pgvector 0.8.7).