UNPKG

@wallfar/ocd-studio-core-sdk

Version:

Helper SDK for our OneClick Studio modules

53 lines (52 loc) 1.84 kB
export function validateAddress(addr) { const errors = []; // Name checks if (!addr.firstName?.trim() || !/^[A-Za-zÀ-ÖØ-öø-ÿ '-]+$/.test(addr.firstName)) { errors.push("Invalid first name"); } if (!addr.lastName?.trim() || !/^[A-Za-zÀ-ÖØ-öø-ÿ '-]+$/.test(addr.lastName)) { errors.push("Invalid last name"); } // Street & city if (!addr.streetLine1?.trim()) errors.push("Street address is required"); if (!addr.city?.trim() || !/^[A-Za-z '-]+$/.test(addr.city)) { errors.push("Invalid city"); } // Country & region // TODO improve checks (use a lib) if (!addr.country) { errors.push("Invalid country"); } if (!addr.stateOrProvince.trim()) { errors.push("State/Province is required for this country"); } // Postal code if (!addr.postalCode) { errors.push("Invalid postal code"); } // Phone if (!addr.phoneNumber) { errors.push("Phone number is required"); // } else if ( // !isValidPhoneNumber(addr.phoneNumber, addr.country) // ) { // errors.push("Invalid phone number"); // } else { // // Optionally parse and re-format: // const phone = parsePhoneNumberFromString(addr.phoneNumber, addr.country); // if (!phone) { // errors.push("Unable to parse phone number"); // } else { // // e.g. normalize to E.164: // addr.phoneNumber = phone.number; // } } // Coordinates (optional) if (addr.latitude != null && addr.longitude != null) { if (addr.latitude < -90 || addr.latitude > 90 || addr.longitude < -180 || addr.longitude > 180) { errors.push("Coordinates out of range"); } } return errors; }