Spaces:
Build error
Build error
File size: 12,588 Bytes
52c1998 |
1 2 3 4 5 6 7 8 9 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
import datetime
from dateutil.relativedelta import relativedelta
from pydantic import (
BaseModel,
computed_field,
Field,
ValidationInfo,
model_validator,
ConfigDict
)
import pandas as pd
class UKPassportSchema(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
full_name: str | None = Field(
default=None,
description="Applicant's full name. Must consist of at least 2 words, have length gte 2 & lte 61",
examples=["Jodie Pippa"],
) # , min_length=2, max_length=61)
expiry_date: datetime.date | None = Field(
default=None,
description="The passport's expiry date in YYYY-MM-DD format",
examples=["2028-06-01"],
)
full_name_err_msgs: str | None = None
expiry_date_err_msgs: str | None = None
validation_policy_status_df: pd.DataFrame = pd.DataFrame(
columns=["Policy", "Value", "Status", "Message"])
@model_validator(mode="after")
def validate_expiry_date(cls, values):
try:
err_msgs = list()
expiry_date_val = values.expiry_date
if not expiry_date_val:
err_msgs.append("Expiry date must be present")
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = ["Expiry date must be present", values.expiry_date, False, "Expiry date is not present"]
else:
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = ["Expiry date must be present", values.expiry_date, True, "Expiry date is present"]
if expiry_date_val < datetime.date.today() + relativedelta(years=1):
# raise ValueError("Provided passport expires within 1 year")
err_msgs.append("Provided passport expires within 1 year")
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Provided passport expiry should be more than 1 year",
values.expiry_date,
False,
"Provided passport expires within 1 year &/or is expired",
]
else:
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Provided passport expiry should be more than 1 year",
values.expiry_date,
True,
"Provided passport does not expire within 1 year",
]
values.expiry_date_err_msgs = ", ".join(
err_msgs) if err_msgs else None
return values
except Exception as e:
raise
# if not values.expiry_date_err_msgs:
# values.expiry_date_err_msgs = "Provided passport expires within 1 year"
# else:
# values.expiry_date_err_msgs = f"{values.expiry_date_err_msgs}, Provided passport expires within 1 year"
# if not values.expiry_date_err_msgs:
# values.expiry_date_err_msgs = None
# return values
@model_validator(mode="after")
def validate_full_name(cls, values, info: ValidationInfo):
"""Match applicant's full name against provided name (case-insensitive)"""
try:
err_msgs = []
expected = (
info.context.get("application_summary_full_name")
if info.context
else None
)
full_name_val = values.full_name
if not full_name_val:
err_msgs.append("Applicant's full name not present")
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = ["Applicant's full name should be present", full_name_val, False, "Applicant's full name not present"]
else:
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = ["Applicant's full name should be present", full_name_val, True, "Applicant's full name is present"]
full_name_val_len = 0
if full_name_val:
full_name_val_len = len(full_name_val)
if not full_name_val and not (
full_name_val_len >= 2 and full_name_val_len <= 61
):
err_msgs.append(
"Full name must have a length of at least 2 & at most 61"
)
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = [ "Full name must have a length of at least 2 & at most 61", full_name_val_len, False, "Full name does not have a length of at least 2 & at most 61"]
else:
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = [ "Full name must have a length of at least 2 & at most 61", full_name_val_len, True, "Full name has a length of at least 2 & at most 61"]
if (
not expected
or not full_name_val
or full_name_val.lower() != expected.lower()
):
err_msgs.append("Name mismatch with provided value")
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = ["Name should match with provided value", full_name_val, False, "Name does not match with provided value"]
else:
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = ["Name should match with provided value", full_name_val, True, "Name matches with provided value"]
if not full_name_val or len(full_name_val.strip().split(" ")) < 2:
err_msgs.append(
"Full name must consist of at least 2 words (first name + last name)"
)
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = ["Full name must consist of at least 2 words (first name + last name)", len(full_name_val.strip().split(" ")), False, "Full name does not consist of at least 2 words (first name + last name)"]
else:
values.validation_policy_status_df.loc[len(
values.validation_policy_status_df)] = ["Full name must consist of at least 2 words (first name + last name)", len(full_name_val.strip().split(" ")), True, "Full name does consist of at least 2 words (first name + last name)"]
if err_msgs:
values.full_name_err_msgs = ", ".join(err_msgs)
else:
values.full_name_err_msgs = None
return values
except Exception as e:
# logger.exception(e, exc_info=True)
# return None
raise
@computed_field
@property
def is_red_flagged(self) -> bool:
if self.full_name_err_msgs or self.expiry_date_err_msgs:
return True
return False
class UKDrivingLicense(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
full_name: str | None = Field(
default=None,
description="Applicant's full name. Must consist of at least 2 words, have length gte 2 & lte 61",
examples=["Jodie Pippa"],
) # , min_length=2, max_length=61)
full_name_err_msgs: str | None = None
expiry_date_err_msgs: str | None = None
validation_policy_status_df: pd.DataFrame = pd.DataFrame(
columns=["Policy", "Value", "Status", "Message"])
@model_validator(mode="after")
def validate_full_name(cls, values, info: ValidationInfo):
"""Match applicant's full name against provided name (case-insensitive)"""
try:
err_msgs = []
expected = (
info.context.get("application_summary_full_name")
if info.context
else None
)
full_name_val = values.full_name
if not full_name_val:
err_msgs.append("Applicant's full name not present")
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Applicant's full name should be present",
full_name_val,
False,
"Applicant's full name not present",
]
else:
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Applicant's full name should be present",
full_name_val,
True,
"Applicant's full name is present",
]
full_name_val_len = 0
if full_name_val:
full_name_val_len = len(full_name_val)
if not full_name_val and not (
full_name_val_len >= 2 and full_name_val_len <= 61
):
err_msgs.append(
"Full name must have a length of at least 2 & at most 61"
)
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Full name must have a length of at least 2 & at most 61",
full_name_val_len,
False,
"Full name does not have a length of at least 2 & at most 61",
]
else:
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Full name must have a length of at least 2 & at most 61",
full_name_val_len,
True,
"Full name has a length of at least 2 & at most 61",
]
if (
not expected
or not full_name_val
or full_name_val.lower() != expected.lower()
):
err_msgs.append("Name mismatch with provided value")
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Name should match with provided value",
full_name_val,
False,
"Name does not match with provided value",
]
else:
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Name should match with provided value",
full_name_val,
True,
"Name matches with provided value",
]
if not full_name_val or len(full_name_val.strip().split(" ")) < 2:
err_msgs.append(
"Full name must consist of at least 2 words (first name + last name)"
)
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Full name must consist of at least 2 words (first name + last name)",
len(full_name_val.strip().split(" ")),
False,
"Full name does not consist of at least 2 words (first name + last name)",
]
else:
values.validation_policy_status_df.loc[
len(values.validation_policy_status_df)
] = [
"Full name must consist of at least 2 words (first name + last name)",
len(full_name_val.strip().split(" ")),
True,
"Full name does consist of at least 2 words (first name + last name)",
]
if err_msgs:
values.full_name_err_msgs = ", ".join(err_msgs)
else:
values.full_name_err_msgs = None
return values
except Exception as e:
# logger.exception(e, exc_info=True)
# return None
raise
@computed_field
@property
def is_red_flagged(self) -> bool:
if self.full_name_err_msgs or self.expiry_date_err_msgs:
return True
return False
|