Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 19x 19x 19x 19x 19x 19x 19x 19x 12x 9x 9x 9x 9x 9x 9x 19x 44x 18x 9x 9x 9x 19x 19x 19x | import * as Yup from "yup";
import { matchIsValidTel } from "mui-tel-input";
import { nonResidentInputsSchema, ssnSchema } from "@utils/validation.helpers";
import { COUNTRY_CODE_REGEX } from "@validation/regex";
import { VALIDATION_MESSAGES } from "@validation/messages";
import { buildZipSchema } from "@validation/address/zipValidator";
import { createStateValidator } from "@validation/address/stateValidator";
import { createStreetAddressValidator } from "@validation/address/streetValidator";
import { createCityValidation } from "@validation/address/cityValidator";
const currentYear = new Date().getFullYear();
const minYear = currentYear - 100;
const maxYear = currentYear + 100;
const baseAddressSchema = Yup.object({
country: Yup.string()
.matches(COUNTRY_CODE_REGEX, VALIDATION_MESSAGES.COUNTRY_REQUIRED)
.required(VALIDATION_MESSAGES.REQUIRED_SHORT),
line1: createStreetAddressValidator({
required: true,
requiredMessage: VALIDATION_MESSAGES.ADDRESS_REQUIRED,
}),
city: createCityValidation({
required: true,
customRequiredMessage: VALIDATION_MESSAGES.CITY_REQUIRED,
}),
state: createStateValidator({
required: true,
requiredMessage: VALIDATION_MESSAGES.STATE_REQUIRED,
}),
zip: buildZipSchema({
required: true,
usFormatOnly: true,
allowNull: false,
message: VALIDATION_MESSAGES.INVALID_ZIP_SHORT,
}),
});
export const nestedSchema = Yup.object({
address: baseAddressSchema,
tinType: Yup.mixed<"ssn" | "ein">().oneOf(["ssn", "ein"]),
ssn: Yup.string().when("tinType", {
is: "ssn",
then: ssnSchema("ssn"),
}),
ein: Yup.string().when("tinType", {
is: "ein",
then: ssnSchema("ein"),
}),
ownership: Yup.string().required(VALIDATION_MESSAGES.OWNERSHIP_REQUIRED),
});
const firstNameSchema = Yup.string().required(
VALIDATION_MESSAGES.FIRST_NAME_REQUIRED,
);
const lastNameSchema = Yup.string().required(
VALIDATION_MESSAGES.LAST_NAME_REQUIRED,
);
const dobSchema = Yup.date()
.nullable()
.typeError(VALIDATION_MESSAGES.INVALID_DATE_POLITE)
.test("is-valid", VALIDATION_MESSAGES.INVALID_DATE_POLITE, function (value) {
if (!value) return true;
const year = value.getFullYear();
const month = value.getMonth();
const day = value.getDate();
const isValidYear = year >= minYear && year <= maxYear;
const isNotZeroStart = year.toString().charAt(0) !== "0"; // Check if year doesn't start with 0
return (
isValidYear &&
isNotZeroStart &&
month === value.getMonth() &&
day === value.getDate()
);
});
const phoneNumberSchema = Yup.string()
.nullable()
.when({
is: (exists: string) => !!exists,
then: (schema) =>
schema.test(
"is-valid-number",
VALIDATION_MESSAGES.INVALID_PHONE,
function (value) {
const phoneNumber = value as string;
Iif (phoneNumber === "+1") return true; // valid if empty number
return matchIsValidTel(phoneNumber);
},
),
});
export { firstNameSchema, lastNameSchema, dobSchema, phoneNumberSchema };
export const schema = Yup.object().shape({
base: Yup.object({
firstName: firstNameSchema,
lastName: lastNameSchema,
dob: dobSchema,
phoneNumber: phoneNumberSchema,
isManager: Yup.boolean(),
...nonResidentInputsSchema,
}),
isOwner: Yup.boolean(),
whenIsOwner: Yup.object().when("isOwner", {
is: true,
then: nestedSchema,
}),
});
export const newSchema = Yup.object().shape({
base: Yup.object({
firstName: firstNameSchema,
lastName: lastNameSchema,
dob: dobSchema,
phoneNumber: phoneNumberSchema,
}),
});
export const calculateCompletionPercentage = (
values: any,
schema: Yup.ObjectSchema<any>,
): number => {
const schemaFields: any = schema.describe().fields;
const requiredFields = Object.keys(schemaFields).filter((key) =>
schemaFields[key].tests.some((test: any) => test.name === "required"),
);
const filledFields = requiredFields.filter((field) => {
const value = values[field];
return value !== undefined && value !== null && value !== "";
});
return Math.round((filledFields.length / requiredFields.length) * 100);
};
|