Module: Ukiryu::Type
- Defined in:
- lib/ukiryu/type.rb
Overview
Type validation and conversion
Validates and converts parameters according to their type definitions.
Class Method Summary collapse
-
.normalize_type(type) ⇒ Hash
Normalize type definition to hash format.
-
.valid_type?(type) ⇒ Boolean
Check if a type is valid.
-
.validate(value, type, options = {}) ⇒ Object
Validate a value against a type definition.
Class Method Details
.normalize_type(type) ⇒ Hash
Normalize type definition to hash format
52 53 54 55 56 57 58 59 60 |
# File 'lib/ukiryu/type.rb', line 52 def normalize_type(type) if type.is_a?(Hash) type else # Convert string types to symbols for consistency type_sym = type.is_a?(String) ? type.to_sym : type { name: type_sym } end end |
.valid_type?(type) ⇒ Boolean
Check if a type is valid
66 67 68 69 |
# File 'lib/ukiryu/type.rb', line 66 def valid_type?(type) type_definition = normalize_type(type) VALID_TYPES.include?(type_definition[:name]) end |
.validate(value, type, options = {}) ⇒ Object
Validate a value against a type definition
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ukiryu/type.rb', line 19 def validate(value, type, = {}) type_definition = normalize_type(type) case type_definition[:name] when :file validate_file(value, ) when :string validate_string(value, ) when :integer validate_integer(value, ) when :float validate_float(value, ) when :symbol validate_symbol(value, ) when :boolean validate_boolean(value) when :uri validate_uri(value, ) when :datetime validate_datetime(value, ) when :hash validate_hash(value, ) when :array validate_array(value, ) else raise Ukiryu::Errors::ValidationError, "Unknown type: #{type_definition[:name]}" end end |