/** * AddressValidator class that provides methods for validating and cleaning address inputs. * It includes methods to clean the input string, validate address formats, and ensure proper * formatting for street names, house numbers, and additional text. */ class AddressValidator { /** * Cleans the input string by removing unwanted characters. * This allows only letters, numbers, spaces, and hyphens. * * @param {string} value - The address input string to be cleaned. * @returns {string} - The cleaned input string. */ static cleanInput(value: string) { // Remove any character that is not a letter, number, space, or hyphen return value.replace(/[^a-zA-Z0-9\s\-]/g, ""); // Allow spaces, letters, numbers, and hyphens } /** * Validates the format of an address. * Ensures the address has a valid street name followed by a house number. * * @param {string} address - The address string to validate. * @returns {boolean} - Returns true if the address format is valid, false otherwise. */ static validateAddressFormat(address: string) { // Match the house number at the end of the address const houseNumberMatch = address.match(/(\d+)\s*$/); // Match house number at the end // Get the street name by removing the house number part const streetName = address.replace(/\d+\s*$/, "").trim(); // Get the part before the house number // Ensure the street name contains only letters, numbers, or spaces and the house number is present return /^[a-zA-Z0-9\s]+$/.test(streetName) && houseNumberMatch !== null; } /** * Validates that the house number does not come before the street name. * This ensures that the street name is always placed first, followed by the house number. * * @param {string} address - The address string to validate. * @returns {boolean} - Returns true if the house number is correctly placed after the street name, false otherwise. */ static validateHouseNumberBeforeStreet(address: string) { // Ensure that the address does not start with a number followed by a space (house number before street) return !/^\d+\s/.test(address); // Ensure the house number doesn't come before the street name } /** * Validates that no extra text exists after the house number. * This ensures that the house number is followed only by the street name, and not additional text. * * @param {string} address - The address string to validate. * @returns {boolean} - Returns true if no extra text follows the house number, false otherwise. */ static validateExtraTextAfterHouseNumber(address: string) { // Ensure there is no extra text (e.g., letters) after the house number return !/\d+\s*[a-zA-Z]+$/.test(address); // Ensure no extra text comes after the house number } } export default AddressValidator;