Const
// Valid language codes
const validCodes = ['en', 'es', 'qu', 'es-ES', 'en-US', 'es-419', 'zh-CN'];
validCodes.forEach(code => {
const result = LanguageCodeSchema.parse(code);
console.log(`${code} is valid`);
});
// Usage in course content validation
const CourseLanguageSchema = z.object({
sourceLanguage: LanguageCodeSchema,
targetLanguage: LanguageCodeSchema,
supportedLocales: z.array(LanguageCodeSchema).optional()
});
// Localization endpoint validation
router.get('/content/:lang', validate({
params: z.object({ lang: LanguageCodeSchema })
}), (req, res) => {
const { lang } = req.params; // Validated language code
const content = await contentService.getLocalizedContent(lang);
res.json({ content, language: lang });
});
Language code validation schema following BCP 47 standard
Comprehensive language code validation that supports the BCP 47 standard for language identification, including ISO 639-1 (two-letter codes), ISO 639-2/3 (three-letter codes), and regional variants. This schema is essential for internationalization support in the language learning platform, ensuring proper language identification and content localization.
The validation supports various language code formats including simple language codes (e.g., "en", "es", "qu"), language codes with country variants (e.g., "es-ES", "en-US"), and language codes with numeric region codes (e.g., "es-419" for Latin American Spanish). This flexibility accommodates diverse linguistic requirements and regional content variations.
Security and data integrity features include strict pattern matching to prevent malformed language codes that could cause localization errors, length limits to prevent buffer overflow attacks, and case sensitivity enforcement to maintain consistency with international standards. The schema ensures that only valid language identifiers are accepted throughout the application.