UNPKG

ajt-validator

Version:

Validation library for JavaScript and TypeScript

58 lines (57 loc) 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AddressValidator = void 0; const base_1 = require("../base"); class AddressValidator extends base_1.BaseValidator { constructor(options = {}) { super(); this.options = Object.assign({ streetRequired: true, cityRequired: true, stateRequired: true, postalCodeRequired: true, countryRequired: true, maxStreetLength: 100, maxCityLength: 50, maxStateLength: 50, postalCodePattern: /^[a-zA-Z0-9\s-]{3,10}$/ }, options); } validate(address) { var _a, _b, _c, _d, _e; if (!address) { return this.createError('ADDRESS_REQUIRED', 'Address data is required'); } // Street validation if (this.options.streetRequired && !address.street) { return this.createError('STREET_REQUIRED', 'Street address is required'); } if (address.street && address.street.length > this.options.maxStreetLength) { return this.createError('STREET_TOO_LONG', `Street address must not exceed ${this.options.maxStreetLength} characters`); } // City validation if (this.options.cityRequired && !address.city) { return this.createError('CITY_REQUIRED', 'City is required'); } if (address.city && address.city.length > this.options.maxCityLength) { return this.createError('CITY_TOO_LONG', `City must not exceed ${this.options.maxCityLength} characters`); } // State validation if (this.options.stateRequired && !address.state) { return this.createError('STATE_REQUIRED', 'State/Province is required'); } if (address.state && address.state.length > this.options.maxStateLength) { return this.createError('STATE_TOO_LONG', `State/Province must not exceed ${this.options.maxStateLength} characters`); } // Postal code validation if (this.options.postalCodeRequired && !address.postalCode) { return this.createError('POSTAL_CODE_REQUIRED', 'Postal code is required'); } if (address.postalCode && !this.options.postalCodePattern.test(address.postalCode)) { return this.createError('INVALID_POSTAL_CODE', 'Postal code format is invalid'); } // Country validation if (this.options.countryRequired && !address.country) { return this.createError('COUNTRY_REQUIRED', 'Country is required'); } return this.createSuccess({ street: (_a = address.street) === null || _a === void 0 ? void 0 : _a.trim(), city: (_b = address.city) === null || _b === void 0 ? void 0 : _b.trim(), state: (_c = address.state) === null || _c === void 0 ? void 0 : _c.trim(), postalCode: (_d = address.postalCode) === null || _d === void 0 ? void 0 : _d.trim(), country: (_e = address.country) === null || _e === void 0 ? void 0 : _e.trim() }); } } exports.AddressValidator = AddressValidator;