Class: TypedEAV::Field::Email

Inherits:
ValidatedString show all
Defined in:
app/models/typed_eav/field/email.rb

Overview

Email-typed field. Inherits string_value storage shape, the min_length / max_length / pattern store_accessor, the max_gte_min_length guard, and the validate_pattern_syntax guard from Field::ValidatedString. Adds the EMAIL_FORMAT regex and layers the format check onto validate_typed_value via super.

Latent-bug fix (per ADR-0004): max_gte_min_length (previously only on Text) now applies here — an Email field configured with max_length < min_length fails at field-save.

Constant Summary collapse

EMAIL_FORMAT =
/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/

Constants inherited from Base

Base::RESERVED_NAMES

Constants included from TypedStorage

TypedStorage::DEFAULT_OPERATORS_BY_COLUMN, TypedStorage::FALLBACK_OPERATORS

Instance Method Summary collapse

Methods inherited from Base

#allowed_option_values, #array_field?, #backfill_default!, #clear_option_cache!, #default_value, #default_value=, #display_name, export_schema, #field_type_name, import_schema, #insert_at, #move_higher, #move_lower, #move_to_bottom, #move_to_top, #optionable?

Methods included from TypedStorage

#after_snapshot, #apply_default, #before_snapshot, #read_value, #value_changed?, #write_value

Instance Method Details

#cast(raw) ⇒ Object



21
22
23
24
25
# File 'app/models/typed_eav/field/email.rb', line 21

def cast(raw)
  return [nil, false] if raw.nil?

  [raw.to_s.strip.downcase, false]
end

#email_format_valid?(val) ⇒ Boolean

Returns:



27
28
29
# File 'app/models/typed_eav/field/email.rb', line 27

def email_format_valid?(val)
  EMAIL_FORMAT.match?(val)
end

#validate_typed_value(record, val) ⇒ Object



31
32
33
34
# File 'app/models/typed_eav/field/email.rb', line 31

def validate_typed_value(record, val)
  super
  record.errors.add(:value, "is not a valid email address") unless email_format_valid?(val)
end