Add Pydantic validation [0.5h]
epogrebnyak opened this issue · 7 comments
Lines 44 to 45 in fadae34
Add validation for:
- github handle (must have slash)
- programming language is in hardcoded list
@validator("lang")
def lang_field_must_be_one_of_allowed(cls, value: str) -> str:
if value.lower() not in allowed_languages:
raise ValueError('Incorrect language name')
return value
@validator("github_handle")
def github_handle_field_must_contain_slash(cls, value: str) -> str:
if '/' not in value:
raise ValueError("github_handle must contain '/'")
return value
Allowed only this languages or more?
Maybe more versobe error message on allowed languages? Got , expected any of .
Let's stick for just js for Javascript, no options
Can also use own error type (if you prefer):
class ValidationError(ValueError):
pass
Yes, in pydantic exist ValidationError exception, but it use more than 1 positional args. I will make own exception, ok. And more verbose message
It looks from docs that we raise ValueError, but can catch pydantic.ValuationError
- so maybe start without own error class, just leave ValueError in validator code for simpolicity (otherwise we can run into conflict of names, etc)
Maybe make subclass OwnValidationError, because ValueError can raise from other places of project and can confuse us?