Class: Castkit::Types::String

Inherits:
Generic
  • 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 Generic

cast!, deserialize, serialize, validate!

Instance Method Details

#deserialize(value) ⇒ String

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



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.



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.

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