Module: Shamu::Attributes::Validation

Extended by:
ActiveSupport::Concern
Included in:
Entities::ListScope, JsonApi::Rails::Pagination, Services::Request
Defined in:
lib/shamu/attributes/validation.rb

Overview

Defines an interface for entities that report validation failures with respect to their attributes.

Defined Under Namespace

Modules: Overrides

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, build, **options) ⇒ Object

Adds validation options to Attributes::DSL#attribute. Any option not recognized by one of the Attributes mixins will be used as validation arguments for the given attribute.

Examples:

attribute :email, presence: true

# Results in
attribute :email
validates :email, presence: true


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shamu/attributes/validation.rb', line 45

def attribute( name, *args, **options, &block )
  super

  validation_options = options.each_with_object({}) do |(key, value), opts|
    next if attribute_option_keys.include?( key )

    validator = "#{ key.to_s.camelize }Validator"
    key = "shamu/attributes/validators/#{ key }" if Shamu::Attributes::Validators.const_defined?( validator.to_sym ) # rubocop:disable Metrics/LineLength
    opts[ key ] = value
  end
  validates name, validation_options if validation_options.any?
end

Instance Method Details

#valid?Boolean

Returns if the object is free from validation errors. Must call #validate before checking.

Returns:

  • (Boolean)

    if the object is free from validation errors. Must call #validate before checking.



20
21
22
# File 'lib/shamu/attributes/validation.rb', line 20

def valid?
  errors.blank?
end

#validated?Boolean

Returns true if the object has been validated at least once.

Returns:

  • (Boolean)

    true if the object has been validated at least once.



25
26
27
# File 'lib/shamu/attributes/validation.rb', line 25

def validated?
  @validated
end