Pular para conteúdo

User input contract

UserInput

Bases: BaseModel

A model representing the input data from the user, with validation for various fields.

Attributes:

Name Type Description
sales_agent str

The sales agent handling the deal.

product str

The product being dealt with.

account str

The account associated with the deal.

unknow_customer Optional[str]

An optional field for unknown customer details.

deal_stage str

The current stage of the deal.

engage_date Optional[date]

The date when the deal was engaged.

close_date Optional[date]

The date when the deal was closed.

close_value Optional[NonNegativeFloat]

The value of the closed deal, if applicable.

Source code in shared/contracts/user_input_contract.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
class UserInput(BaseModel):
    """
    A model representing the input data from the user, with validation for various fields.

    Attributes:
        sales_agent (str): The sales agent handling the deal.
        product (str): The product being dealt with.
        account (str): The account associated with the deal.
        unknow_customer (Optional[str]): An optional field for unknown customer details.
        deal_stage (str): The current stage of the deal.
        engage_date (Optional[date]): The date when the deal was engaged.
        close_date (Optional[date]): The date when the deal was closed.
        close_value (Optional[NonNegativeFloat]): The value of the closed deal, if applicable.
    """

    sales_agent: str
    product: str
    account: str
    unknow_customer: Optional[str] = None
    deal_stage: str
    engage_date: Optional[date] = None
    close_date: Optional[date] = None
    close_value: Optional[NonNegativeFloat] = None

    @field_validator("unknow_customer", mode="before")
    def validate_unknown_customer(cls, value: Optional[str], info) -> Optional[str]:
        """
        Validates the 'unknow_customer' field to ensure it's filled if the account is 'Other'.

        Args:
            value (Optional[str]): The value of the 'unknow_customer' field.
            info: Additional information about the validation context.

        Returns:
            Optional[str]: The validated value of 'unknow_customer'.

        Raises:
            ValueError: If 'unknow_customer' is required but not provided.
        """
        if info.data.get("account") == "Other" and not value:
            raise ValueError("If 'Other' was selected, the field 'unknow_customer' must be filled.")
        return value

    @field_validator("close_date", mode="before")
    def validate_close_date(cls, value: Optional[date], info) -> Optional[date]:
        """
        Validates the 'close_date' field to ensure it's provided for 'Won' or 'Lost' deal stages.

        Args:
            value (Optional[date]): The value of the 'close_date' field.
            info: Additional information about the validation context.

        Returns:
            Optional[date]: The validated value of 'close_date'.

        Raises:
            ValueError: If 'close_date' is required but not provided for certain deal stages.
        """
        if (info.data.get("deal_stage") not in ["Engaging", "Prospecting"]) and not value:
            raise ValueError("The closing date must be entered for stages 'Won' or 'Lost'.")
        return value

    @model_validator(mode="after")
    def validate_date_difference(cls, data: "UserInput") -> "UserInput":
        """
        Validates that the 'close_date' is later than or equal to the 'engage_date'.

        Args:
            data (UserInput): The instance of the class being validated.

        Returns:
            UserInput: The validated instance of the class.

        Raises:
            ValueError: If 'close_date' is earlier than 'engage_date'.
        """
        engage_date = data.engage_date
        close_date = data.close_date
        if close_date and close_date < engage_date:
            raise ValueError("The close date must be later than or equal to the engage date.")
        return data

    @field_validator("close_value")
    def validate_close_value(cls, value: Optional[NonNegativeFloat], info) -> Optional[NonNegativeFloat]:
        """
        Validates the 'close_value' field to ensure it's greater than 0 if the deal is 'Won'.

        Args:
            value (Optional[NonNegativeFloat]): The value of the 'close_value' field.
            info: Additional information about the validation context.

        Returns:
            Optional[NonNegativeFloat]: The validated value of 'close_value'.

        Raises:
            ValueError: If 'close_value' is less than or equal to 0 for a 'Won' deal stage.
        """
        if info.data.get("deal_stage") == "Won" and value is not None and value <= 0:
            raise ValueError("The closing value must be greater than 0 for 'Won' stages.")
        return value

validate_close_date(value, info)

Validates the 'close_date' field to ensure it's provided for 'Won' or 'Lost' deal stages.

Parameters:

Name Type Description Default
value Optional[date]

The value of the 'close_date' field.

required
info

Additional information about the validation context.

required

Returns:

Type Description
Optional[date]

Optional[date]: The validated value of 'close_date'.

Raises:

Type Description
ValueError

If 'close_date' is required but not provided for certain deal stages.

Source code in shared/contracts/user_input_contract.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@field_validator("close_date", mode="before")
def validate_close_date(cls, value: Optional[date], info) -> Optional[date]:
    """
    Validates the 'close_date' field to ensure it's provided for 'Won' or 'Lost' deal stages.

    Args:
        value (Optional[date]): The value of the 'close_date' field.
        info: Additional information about the validation context.

    Returns:
        Optional[date]: The validated value of 'close_date'.

    Raises:
        ValueError: If 'close_date' is required but not provided for certain deal stages.
    """
    if (info.data.get("deal_stage") not in ["Engaging", "Prospecting"]) and not value:
        raise ValueError("The closing date must be entered for stages 'Won' or 'Lost'.")
    return value

validate_close_value(value, info)

Validates the 'close_value' field to ensure it's greater than 0 if the deal is 'Won'.

Parameters:

Name Type Description Default
value Optional[NonNegativeFloat]

The value of the 'close_value' field.

required
info

Additional information about the validation context.

required

Returns:

Type Description
Optional[NonNegativeFloat]

Optional[NonNegativeFloat]: The validated value of 'close_value'.

Raises:

Type Description
ValueError

If 'close_value' is less than or equal to 0 for a 'Won' deal stage.

Source code in shared/contracts/user_input_contract.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
@field_validator("close_value")
def validate_close_value(cls, value: Optional[NonNegativeFloat], info) -> Optional[NonNegativeFloat]:
    """
    Validates the 'close_value' field to ensure it's greater than 0 if the deal is 'Won'.

    Args:
        value (Optional[NonNegativeFloat]): The value of the 'close_value' field.
        info: Additional information about the validation context.

    Returns:
        Optional[NonNegativeFloat]: The validated value of 'close_value'.

    Raises:
        ValueError: If 'close_value' is less than or equal to 0 for a 'Won' deal stage.
    """
    if info.data.get("deal_stage") == "Won" and value is not None and value <= 0:
        raise ValueError("The closing value must be greater than 0 for 'Won' stages.")
    return value

validate_date_difference(data)

Validates that the 'close_date' is later than or equal to the 'engage_date'.

Parameters:

Name Type Description Default
data UserInput

The instance of the class being validated.

required

Returns:

Name Type Description
UserInput UserInput

The validated instance of the class.

Raises:

Type Description
ValueError

If 'close_date' is earlier than 'engage_date'.

Source code in shared/contracts/user_input_contract.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@model_validator(mode="after")
def validate_date_difference(cls, data: "UserInput") -> "UserInput":
    """
    Validates that the 'close_date' is later than or equal to the 'engage_date'.

    Args:
        data (UserInput): The instance of the class being validated.

    Returns:
        UserInput: The validated instance of the class.

    Raises:
        ValueError: If 'close_date' is earlier than 'engage_date'.
    """
    engage_date = data.engage_date
    close_date = data.close_date
    if close_date and close_date < engage_date:
        raise ValueError("The close date must be later than or equal to the engage date.")
    return data

validate_unknown_customer(value, info)

Validates the 'unknow_customer' field to ensure it's filled if the account is 'Other'.

Parameters:

Name Type Description Default
value Optional[str]

The value of the 'unknow_customer' field.

required
info

Additional information about the validation context.

required

Returns:

Type Description
Optional[str]

Optional[str]: The validated value of 'unknow_customer'.

Raises:

Type Description
ValueError

If 'unknow_customer' is required but not provided.

Source code in shared/contracts/user_input_contract.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@field_validator("unknow_customer", mode="before")
def validate_unknown_customer(cls, value: Optional[str], info) -> Optional[str]:
    """
    Validates the 'unknow_customer' field to ensure it's filled if the account is 'Other'.

    Args:
        value (Optional[str]): The value of the 'unknow_customer' field.
        info: Additional information about the validation context.

    Returns:
        Optional[str]: The validated value of 'unknow_customer'.

    Raises:
        ValueError: If 'unknow_customer' is required but not provided.
    """
    if info.data.get("account") == "Other" and not value:
        raise ValueError("If 'Other' was selected, the field 'unknow_customer' must be filled.")
    return value