Class: Castkit::Types::String

Inherits:
Base
  • Object
show all
Defined in:
lib/castkit/types/string.rb

Overview

Type definition for :string attributes.

Coerces any input to a string using to_s, and validates that the resulting value is a String. Supports optional format validation via a :format option (Regexp or Proc).

This class is used internally by Castkit when an attribute is defined with:

`string :id`

Instance Method Summary collapse

Methods inherited from Base

cast!, deserialize, serialize, validate!

Instance Method Details

#deserialize(value) ⇒ String

Deserializes the value by coercing it to a string using to_s.

Parameters:

  • value (Object)

Returns:



20
21
22
# File 'lib/castkit/types/string.rb', line 20

def deserialize(value)
  value.to_s
end

#serialize(value) ⇒ String

Serializes the value as-is.

Parameters:

Returns:



28
29
30
# File 'lib/castkit/types/string.rb', line 28

def serialize(value)
  value
end

#validate!(value, options: {}, context: {}) ⇒ void

This method returns an undefined value.

Validates the value is a String and optionally matches a format.

Parameters:

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

    validation options (e.g., ‘format: /regex/`)

  • context (Symbol, String) (defaults to: {})

Raises:



39
40
41
# File 'lib/castkit/types/string.rb', line 39

def validate!(value, options: {}, context: {})
  Castkit::Validators::StringValidator.call(value, options: options, context: context)
end