pydantic validation does not add default value

46 views Asked by At

I have an excel spreadsheet with multiple sheets. I am reading the entire sheet and making into a dictionary where the the key is the sheet_name and the value is a dataframe of all the data. I am using the pydantic model below and cant seem get it update the dataframe value to the default. If I input enable/disable it works and validation passes, but I would like to add a default value and have that reflected on the dataframe. I have played with different ways of doing this and cant seem to figure it out. I am trying to have a set_default_value function do a precheck and have it set the default values if they are not present, but this does not seem to work.

from pydantic import BaseModel, Field, validator, field_validator, model_validator
from pydantic.networks import IPv4Address
from typing_extensions import Annotated, Literal

ENABLED_DISABLED = Literal["disabled", "enabled"]


class GlobalSchema(BaseModel):
    mgmt_vip: Annotated[IPv4Address, Field(description="Global vip")]
    mgmt_gw: Annotated[IPv4Address, Field(description="Global gw")]
    radius1: Annotated[IPv4Address, Field(description="Primary Radius IP")]
    radius2: Annotated[IPv4Address, Field(description="Backup Radius IP")]
    location: Annotated[ENABLED_DISABLED, Field(description="Location", default="disabled")]


    @model_validator(mode='before')
    def set_default_values(cls, values):
        print(f'type is {type(values)}')
        # Iterate over all fields and set default value if field is empty
        for field_name, field_value in values.items():
            if field_value == "" or field_value is None:
                default_value = cls.__fields__[field_name].default
                values[field_name] = default_value
        # print(f'type is {type(values)}')
        return values


def update_schema_instance(model_instance):
    # Convert model instance to a dictionary
    values = model_instance.dict()
    return GlobalSchema(**values)

def get_dataframe_data(excel_file: str) -> dict:
    """
    Reads all sheets from an Excel file into a dictionary of DataFrames.

    Parameters:
        excel_file (str): Path to the Excel file.

    Returns:
        dict: A dictionary where keys are sheet names and values are DataFrames.
    """
    # Read all sheet names from the Excel file
    sheet_names = pd.ExcelFile(excel_file).sheet_names

    # Create an empty dictionary to store DataFrames
    sheet_name_df = {}

    # Loop through each sheet name
    for sheet_name in sheet_names:
        # Read the sheet into a DataFrame and assign it to a variable with the sheet name
        sheet_name_df[sheet_name] = pd.read_excel(excel_file, sheet_name=sheet_name, na_filter=False)

    return sheet_name_df



if __name__ == '__main__':
    sheets_data = get_dataframe_data(EXCEL_FILE)

    print(validate_schema(sheets_data))
0

There are 0 answers