9240a863d1
Part 1: Shared Utilities - Create internal/fileutil package with FindDataFile() and LoadJSON() - Create internal/lang package with language constants and validation - Eliminate 46 lines of code duplication between cv/loader.go and ui/loader.go - Simplify cv/loader.go from 69 to 36 lines (-48%) - Simplify ui/loader.go from 56 to 24 lines (-57%) Part 2: Validation Layer - Add comprehensive validation in internal/models/cv/validation.go - Validate Personal (name, email format, URLs) - Validate Experience (required fields, dates) - Validate Education (required fields) - Validate Skills (proficiency ranges 1-5, categories) - Validate Languages (proficiency levels 1-5) - Validate Projects (title, URLs) - Validate Meta (version, language) - Integrate validation into LoadCV() - automatic on load - Create ValidationError and ValidationErrors types for clear error reporting - Report all validation errors at once (better UX) Testing: - Add comprehensive tests for fileutil package (FindDataFile, LoadJSON) - Add tests for lang package (IsValid, Validate, All) - Add 280+ validation test cases covering edge cases - All tests pass with real CV data (cv-en.json, cv-es.json) - Fixed validation to allow both URLs and local paths for gitRepoUrl Documentation: - Create _go-learning/refactorings/002-shared-utilities-validation.md - Document architecture, benefits, testing, and interview talking points - Explain WHY decisions were made (DRY, type safety, data integrity) Benefits: - DRY: Single source of truth for utilities - Type safety: Language constants instead of magic strings - Data integrity: Validation catches errors at load time - Better errors: Clear messages showing all issues at once - Maintainability: Centralized utilities easier to update
35 lines
750 B
Go
35 lines
750 B
Go
package lang
|
|
|
|
import "fmt"
|
|
|
|
// Supported language codes
|
|
const (
|
|
English = "en"
|
|
Spanish = "es"
|
|
)
|
|
|
|
// All returns all supported language codes
|
|
func All() []string {
|
|
return []string{English, Spanish}
|
|
}
|
|
|
|
// IsValid checks if a language code is supported
|
|
func IsValid(lang string) bool {
|
|
return lang == English || lang == Spanish
|
|
}
|
|
|
|
// Validate returns an error if the language code is unsupported.
|
|
// It provides helpful error messages showing all supported languages.
|
|
//
|
|
// Example:
|
|
//
|
|
// if err := lang.Validate("fr"); err != nil {
|
|
// // err: unsupported language: fr (supported: [en es])
|
|
// }
|
|
func Validate(lang string) error {
|
|
if !IsValid(lang) {
|
|
return fmt.Errorf("unsupported language: %s (supported: %v)", lang, All())
|
|
}
|
|
return nil
|
|
}
|