Class: BetterTranslate::Validator
- Inherits:
-
Object
- Object
- BetterTranslate::Validator
- Defined in:
- lib/better_translate/validator.rb
Overview
Input validation utilities
Validates language codes, text, paths, and other inputs.
Class Method Summary collapse
-
.validate_api_key!(key, provider:) ⇒ true
Validate an API key.
-
.validate_file_exists!(path) ⇒ true
Validate a file path exists.
-
.validate_language_code!(code) ⇒ true
Validate a language code.
-
.validate_text!(text) ⇒ true
Validate text for translation.
Class Method Details
.validate_api_key!(key, provider:) ⇒ true
Validate an API key
API keys must be non-empty strings.
97 98 99 100 101 102 103 |
# File 'lib/better_translate/validator.rb', line 97 def self.validate_api_key!(key, provider:) raise ConfigurationError, "API key for #{provider} cannot be nil" if key.nil? raise ConfigurationError, "API key for #{provider} must be a String" unless key.is_a?(String) raise ConfigurationError, "API key for #{provider} cannot be empty" if key.strip.empty? true end |
.validate_file_exists!(path) ⇒ true
Validate a file path exists
73 74 75 76 77 78 79 |
# File 'lib/better_translate/validator.rb', line 73 def self.validate_file_exists!(path) raise FileError, "File path cannot be nil" if path.nil? raise FileError, "File path must be a String" unless path.is_a?(String) raise FileError, "File does not exist: #{path}" unless File.exist?(path) true end |
.validate_language_code!(code) ⇒ true
Validate a language code
Language codes must be 2-letter strings (e.g., "en", "it", "fr").
29 30 31 32 33 34 35 36 |
# File 'lib/better_translate/validator.rb', line 29 def self.validate_language_code!(code) raise ValidationError, "Language code cannot be nil" if code.nil? raise ValidationError, "Language code must be a String" unless code.is_a?(String) raise ValidationError, "Language code cannot be empty" if code.empty? raise ValidationError, "Language code must be 2 letters" unless code.match?(/^[a-z]{2}$/i) true end |
.validate_text!(text) ⇒ true
Validate text for translation
Text must be a non-empty string.
53 54 55 56 57 58 59 |
# File 'lib/better_translate/validator.rb', line 53 def self.validate_text!(text) raise ValidationError, "Text cannot be nil" if text.nil? raise ValidationError, "Text must be a String" unless text.is_a?(String) raise ValidationError, "Text cannot be empty" if text.strip.empty? true end |