search.py 529 B

12345678910111213141516171819
  1. from __future__ import annotations
  2. from shlex import join, split
  3. from marshmallow import fields, ValidationError
  4. class SearchFilterField(fields.Str):
  5. """Field that represents a search filter."""
  6. def _deserialize(self, value, attr, data, **kwargs) -> list[str]:
  7. try:
  8. search_terms = split(value)
  9. except ValueError as e:
  10. raise ValidationError(str(e))
  11. return search_terms
  12. def _serialize(self, value: list[str], attr, obj, **kwargs) -> str:
  13. return join(value)