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 | 135x 135x 1517x 1517x 1517x 755x 232x 232x 2080x 1517x | import { VALIDATION_MESSAGES } from "@validation/messages";
import * as Yup from "yup";
export const FORBIDDEN_ADDRESS_WORDS = [
"po",
"p.o",
"box",
"boxes",
"pmb",
"pmbs",
"pobox",
"pob",
"p.o.b",
];
/**
* Configuration for street address validation
*/
export interface StreetAddressValidatorConfig {
/** Whether address is required */
required?: boolean;
/** Whether to check for PO Boxes */
disallowPoBox?: boolean;
/** Custom required error message */
requiredMessage?: string;
/** Custom PO Box error message */
poBoxMessage?: string;
nullable?: boolean;
}
/**
* Creates a Yup street address validation schema
* Can optionally disallow PO Boxes and PMBs
*
* @param config - Configuration options
* @returns Yup string schema for street address validation
*
* @example
* ```typescript
* const schema = Yup.object({
* street: createStreetAddressValidator({
* required: true,
* disallowPoBox: true
* })
* });
* ```
*/
export const createStreetAddressValidator = ({
required = true,
disallowPoBox = true,
requiredMessage = VALIDATION_MESSAGES.ADDRESS_REQUIRED,
poBoxMessage = VALIDATION_MESSAGES.PO_BOX_NOT_ALLOWED,
nullable = false,
}: StreetAddressValidatorConfig = {}) => {
let schema = nullable ? Yup.string().nullable() : Yup.string();
Eif (disallowPoBox) {
schema = schema.test("no-po-box", poBoxMessage, (value) => {
if (!value || value.trim() === "") return true;
const lowerValue = value.toLowerCase();
return !FORBIDDEN_ADDRESS_WORDS.some((word) =>
new RegExp(`\\b${word}(\\b|\\d|[^a-zA-Z])`, "i").test(lowerValue),
);
});
}
return required ? schema.required(requiredMessage) : schema;
};
|