@ivandt/json-rules
Version:
Rule parsing engine for JSON rules
165 lines (164 loc) • 3.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateCountry = validateCountry;
exports.getSupportedCountryNames = getSupportedCountryNames;
const isISO31661Alpha2_1 = __importDefault(require("validator/lib/isISO31661Alpha2"));
const isISO31661Alpha3_1 = __importDefault(require("validator/lib/isISO31661Alpha3"));
/**
* Common country names mapping for validation
* This is a subset of common countries - in production, you might want a more comprehensive list
*/
const COUNTRY_NAMES = [
// Major countries
"united states",
"usa",
"america",
"united kingdom",
"uk",
"britain",
"great britain",
"england",
"scotland",
"wales",
"northern ireland",
"canada",
"australia",
"new zealand",
"germany",
"deutschland",
"france",
"spain",
"españa",
"italy",
"italia",
"netherlands",
"holland",
"belgium",
"switzerland",
"austria",
"österreich",
"sweden",
"sverige",
"norway",
"norge",
"denmark",
"danmark",
"finland",
"suomi",
"poland",
"polska",
"czech republic",
"czechia",
"slovakia",
"hungary",
"magyarország",
"romania",
"bulgaria",
"greece",
"hellas",
"turkey",
"türkiye",
"russia",
"russian federation",
"ukraine",
"belarus",
"lithuania",
"latvia",
"estonia",
"japan",
"nippon",
"nihon",
"china",
"people's republic of china",
"prc",
"south korea",
"republic of korea",
"north korea",
"democratic people's republic of korea",
"india",
"pakistan",
"bangladesh",
"sri lanka",
"indonesia",
"malaysia",
"thailand",
"vietnam",
"philippines",
"singapore",
"taiwan",
"republic of china",
"hong kong",
"macau",
"brazil",
"brasil",
"argentina",
"chile",
"colombia",
"peru",
"venezuela",
"ecuador",
"bolivia",
"uruguay",
"paraguay",
"mexico",
"méxico",
"south africa",
"egypt",
"nigeria",
"kenya",
"ethiopia",
"morocco",
"algeria",
"tunisia",
"israel",
"saudi arabia",
"united arab emirates",
"uae",
"iran",
"iraq",
"jordan",
"lebanon",
"syria",
"kuwait",
"qatar",
"bahrain",
"oman",
"yemen",
];
/**
* Validates country identifiers in various formats
*/
function validateCountry(value, config) {
// Must be a string
if (typeof value !== "string") {
return false;
}
// Config is required for country validation
if (!config || !config.format) {
return false;
}
const cleanValue = value.trim();
switch (config.format) {
case "iso2":
// Validate ISO 3166-1 alpha-2 codes (e.g., "US", "GB", "DE")
return (0, isISO31661Alpha2_1.default)(cleanValue);
case "iso3":
// Validate ISO 3166-1 alpha-3 codes (e.g., "USA", "GBR", "DEU")
return (0, isISO31661Alpha3_1.default)(cleanValue);
case "name":
// Validate against common country names (case-insensitive)
const cleanName = cleanValue.toLowerCase();
return COUNTRY_NAMES.includes(cleanName);
default:
return false;
}
}
/**
* Get all supported country names
*/
function getSupportedCountryNames() {
return [...COUNTRY_NAMES];
}