Skip to content

Commit

Permalink
Merge pull request #14 from Nikronic/better-pydantic-eval
Browse files Browse the repository at this point in the history
use predefined types (e.g. `Field`) instead of genereal `validator` from `pydantic`
  • Loading branch information
Nikronic authored Sep 9, 2023
2 parents 38dfb60 + 4354995 commit 1e4203f
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions niklib/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

# core
import pydantic
from pydantic import validator
from pydantic import (
validator,
Field,
)
import json
# ours
from niklib.data.constant import (
Expand Down Expand Up @@ -83,19 +86,21 @@ class PayloadExample(BaseModel):
ValueError: _description_
"""

sex: str
@validator('sex')
def _sex(cls, value):
if value not in ExampleSex.get_member_names():
raise ValueError(f'"{value}" is not valid')
return value

funds: float = 8000.
@validator('funds')
def _funds(cls, value):
if value <= 0.:
raise ValueError('funds cannot be negative number.')
return value
sex: str = ExampleSex
# or you can define custom validation as below

# @validator('sex')
# def _sex(cls, value):
# if value not in ExampleSex.get_member_names():
# raise ValueError(f'"{value}" is not valid')
# return value

funds: float = Field(
title='Amount of fund',
description='Amount of money you are going to bring and probably spend.',
gt=1000.,
default=8000.
)

class Config:
orm_mode = True

0 comments on commit 1e4203f

Please sign in to comment.