Class: HashCast::Casters::StringCaster

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_cast/casters/string_caster.rb

Constant Summary collapse

NULL_BYTE_CHARACTER =
"\u0000".freeze

Class Method Summary collapse

Class Method Details

.cast(value, attr_name, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hash_cast/casters/string_caster.rb', line 4

def self.cast(value, attr_name, options = {})
  casted_value = cast_string(value, attr_name, options)

  if HashCast.config.validate_string_null_byte && casted_value.match?(NULL_BYTE_CHARACTER)
    raise HashCast::Errors::CastingError, 'contains invalid characters'
  end

  if HashCast.config.string_size_validator_enabled
    if value.size > HashCast.config.string_size_validator_limit
      HashCast.config.string_size_validator_callback.call(value, attr_name, options)
    end
  end

  casted_value
end