Const
// Valid country codes
const validCodes = ['US', 'CA', 'MX', 'BR', 'AR', 'ES', 'FR', 'DE', 'JP', 'AU'];
validCodes.forEach(code => {
const result = CountryCodeSchema.parse(code);
console.log(`${code} is valid`);
});
// User profile with country validation
const UserProfileSchema = z.object({
name: z.string(),
email: EmailSchema,
country: CountryCodeSchema.optional(),
timezone: z.string().optional()
});
Country code validation schema following ISO 3166-1 alpha-2 standard
Strict country code validation that enforces the ISO 3166-1 alpha-2 standard for country identification. This schema ensures consistent geographic data representation throughout the application, supporting features like user location tracking, content regionalization, and compliance with geographic restrictions or regulations.
The validation enforces the exact two-character uppercase format required by the ISO standard, preventing common errors like lowercase codes, full country names, or numeric codes. This strict validation ensures compatibility with international systems and maintains data consistency across distributed services and third-party integrations.
Security considerations include prevention of injection attacks through strict pattern matching, elimination of malformed geographic data that could cause localization errors, and consistent data format that supports geographic analytics and compliance reporting. The schema maintains compatibility with standard geographic databases and mapping services.