You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Trying to use the validation context within one of my Model classes which pydantic offers is not possible.
Current Behavior
Example code to reproduce:
from odmantic import Model
from pydantic import ValidationInfo, field_validator
class MyModel(Model):
text: str
@field_validator('text')
@classmethod
def remove_stopwords(cls, v: str, info: ValidationInfo):
context = info.context
if context:
stopwords = context.get('stopwords', set())
v = ' '.join(w for w in v.split() if w.lower() not in stopwords)
return v
data = {'text': 'This is an example document'}
print(MyModel.model_validate(data)) # no context
#> text='This is an example document'
print(MyModel.model_validate(data, context={'stopwords': ['this', 'is', 'an']}))
#> text='This is an example document'
print(MyModel.model_validate(data, context={'stopwords': ['document']}))
#> text='This is an example document'
Expected behavior
The output should not be
text='This is an example document'
text='This is an example document'
text='This is an example document'
but
text='This is an example document'
text='example document'
text='This is an example'
according to the context that should filter out the words per use case.
Environment
ODMantic version: 1.0.1
Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())):
Bug
Trying to use the validation context within one of my Model classes which pydantic offers is not possible.
Current Behavior
Example code to reproduce:
Expected behavior
The output should not be
but
according to the context that should filter out the words per use case.
Environment
python -c "import pydantic.utils; print(pydantic.utils.version_info())
):The text was updated successfully, but these errors were encountered: