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

Class Method Details

.normalize_type(type) ⇒ Hash

Normalize type definition to hash format

Parameters:

  • type (Symbol, String, Hash)

    the type definition

Returns:

  • (Hash)

    normalized type definition



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

Parameters:

  • type (Symbol, Hash)

    the type to check

Returns:

  • (Boolean)


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

Parameters:

  • value (Object)

    the value to validate

  • type (Symbol, Hash)

    the type definition

  • options (Hash) (defaults to: {})

    additional validation options

Returns:

  • (Object)

    the validated and converted value

Raises:

  • (ValidationError)

    if validation fails



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, options = {})
  type_definition = normalize_type(type)

  case type_definition[:name]
  when :file
    validate_file(value, options)
  when :string
    validate_string(value, options)
  when :integer
    validate_integer(value, options)
  when :float
    validate_float(value, options)
  when :symbol
    validate_symbol(value, options)
  when :boolean
    validate_boolean(value)
  when :uri
    validate_uri(value, options)
  when :datetime
    validate_datetime(value, options)
  when :hash
    validate_hash(value, options)
  when :array
    validate_array(value, options)
  else
    raise Ukiryu::Errors::ValidationError, "Unknown type: #{type_definition[:name]}"
  end
end