From 1fb82b2c46bbc53380e110e662c3ab8ea295d511 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Mon, 18 Sep 2023 11:44:51 +0200 Subject: [PATCH] Improve doc --- docs/index.md | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/docs/index.md b/docs/index.md index 52decf18..20db0b4a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -111,31 +111,29 @@ You can disable this behaviour by setting `validate_default=False` either in `mo or on field-by-field basis: ```py -import datetime as dt - from pydantic import Field -from pydantic.functional_validators import BeforeValidator -from typing_extensions import Annotated from pydantic_settings import BaseSettings, SettingsConfigDict -def str_to_date(v: str) -> dt.date: - return dt.date.fromisoformat(v) - - -class GlobalSettings(BaseSettings): +class Settings(BaseSettings): model_config = SettingsConfigDict(validate_default=False) # default won't be validated - date: Annotated[dt.date, BeforeValidator(str_to_date)] = dt.date(2000, 1, 1) + foo: int = 'test' + + +print(Settings()) +#> foo='test' -class LocalSettings(BaseSettings): +class Settings1(BaseSettings): # default won't be validated - date: Annotated[dt.date, BeforeValidator(str_to_date)] = Field( - default=dt.date(2000, 1, 1), validate_default=False - ) + foo: int = Field('test', validate_default=False) + + +print(Settings1()) +#> foo='test' ``` Check the [Validation of default values](validators.md#validation-of-default-values) for more information.