mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
e7227db41a
* starting insert prints * working exec_script * remove prints, fix exec_examples.py * more cleanup of examples, better model printing * upgrade netlify runtime * extra docs deps * few more small tweaks
30 lines
724 B
Python
30 lines
724 B
Python
from pydantic import BaseModel, HttpUrl, PostgresDsn, ValidationError, validator
|
|
|
|
class MyModel(BaseModel):
|
|
url: HttpUrl
|
|
|
|
m = MyModel(url='http://www.example.com')
|
|
|
|
# the repr() method for a url will display all properties of the url
|
|
print(repr(m.url))
|
|
print(m.url.scheme)
|
|
print(m.url.host)
|
|
print(m.url.host_type)
|
|
print(m.url.port)
|
|
class MyDatabaseModel(BaseModel):
|
|
db: PostgresDsn
|
|
|
|
@validator('db')
|
|
def check_db_name(cls, v):
|
|
assert v.path and len(v.path) > 1, 'database must be provided'
|
|
return v
|
|
|
|
m = MyDatabaseModel(db='postgres://user:pass@localhost:5432/foobar')
|
|
print(m.db)
|
|
|
|
try:
|
|
MyDatabaseModel(db='postgres://user:pass@localhost:5432')
|
|
except ValidationError as e:
|
|
print(e)
|
|
|