1+ from datetime import datetime
2+ from re import compile , search
3+ from typing import Optional
4+
5+ from pydantic import BaseModel , EmailStr , validator
6+
7+
8+ def confirm_field (value : str ) -> str :
9+ """
10+ Validation to prevent empty title field.
11+ Called by the helperfunction below;
12+ """
13+ if not value :
14+ raise ValueError ("Please fill in missing field." )
15+ return value
16+
17+ def check_name_length (value : str ) -> str :
18+ """
19+ Check the length of the name fields
20+ """
21+ if len (value ) > 100 :
22+ raise ValueError ("The provided name is too large." )
23+ return value
24+
25+ def check_special_character (value : str ) -> str :
26+ """
27+ Check the presence of special characters.
28+ """
29+ special_characters = compile ("[\" \" @_!#$%^&*()<>'\" ?/\|}{~:]" )
30+ if special_characters .search (value ) is not None :
31+ raise ValueError ("The provided name contains space or special character(s)." )
32+ return value
33+
34+ def check_short_field_length (value : str ) -> str :
35+ """
36+ Check the length of the email
37+ """
38+ if len (value ) > 255 :
39+ raise ValueError ("The provided email is too large." )
40+ return value
41+
42+
43+ def check_long_field_length (value : str ) -> str :
44+ """
45+ Check the length of the message
46+ """
47+ if len (value ) > 2000 :
48+ raise ValueError ("The message field is too large." )
49+ return value
50+
51+
52+ class ContactBase (BaseModel ):
53+ """
54+ Base fields for contact.
55+ """
56+ firstname : str
57+ lastname : str
58+ email : EmailStr
59+ subject : str
60+ message : str
61+ # Custom validation for first name field
62+ _check_firstname = validator ("firstname" , allow_reuse = True )(confirm_field )
63+ _check_fn_length = validator ("firstname" , allow_reuse = True )(check_name_length )
64+ _check_fn_spec_chr = validator ("firstname" , allow_reuse = True )(check_special_character )
65+
66+ # Custom validation for last name field
67+ _check_lastname = validator ("lastname" , allow_reuse = True )(confirm_field )
68+ _check_ln_length = validator ("lastname" , allow_reuse = True )(check_name_length )
69+ _check_ln_spec_chr = validator ("lastname" , allow_reuse = True )(check_special_character )
70+
71+ # Custom validation for email field
72+ _check_email = validator ("email" , allow_reuse = True )(confirm_field )
73+ _check_email_length = validator ("email" , allow_reuse = True )(check_short_field_length )
74+
75+ # Custom validation for message field
76+ _check_message = validator ("message" , allow_reuse = True )(confirm_field )
77+ _check_message_length = validator ("message" , allow_reuse = True )(check_long_field_length )
78+
79+ # Custom validation for subject field.
80+ _check_subject = validator ("subject" , allow_reuse = True )(confirm_field )
81+ _check_subject_length = validator ("subject" , allow_reuse = True )(check_short_field_length )
82+
83+
84+ class ContactCreate (ContactBase ):
85+ """
86+ Fields for creating contacts.
87+ """
88+ ...
89+
90+ class ContactOut (ContactBase ):
91+ """
92+ For Contact response.
93+ """
94+ id : Optional [int ] = None
95+ active : bool = True
96+ created : Optional [datetime ] = None
97+
0 commit comments