module Mongoid::Fields::Validators::Macro

Validates the params passed to the field macro.

Constants

FIELD_TYPE_IS_SYMBOL

The warning message to give when a field is of type Symbol.

@since 7.0.0

OPTIONS

Public Instance Methods

validate(klass, name, options) click to toggle source

Validate the field definition.

@example Validate the field definition.

Macro.validate(Model, :name, { localized: true })

@param [ Class ] klass The model class. @param [ Symbol ] name The field name. @param [ Hash ] options The provided options.

@since 3.0.0

# File lib/mongoid/fields/validators/macro.rb, line 41
def validate(klass, name, options)
  validate_field_name(klass, name)
  validate_name_uniqueness(klass, name, options)
  validate_options(klass, name, options)
end
validate_field_name(klass, name) click to toggle source

Determine if the field name is valid, if not raise an error.

@example Check the field name.

Macro.validate_field_name(Model, :name)

@param [ Class ] klass The model class. @param [ Symbol ] name The field name.

@raise [ Errors::InvalidField ] If the name is not allowed.

@api private

# File lib/mongoid/fields/validators/macro.rb, line 76
def validate_field_name(klass, name)
  [name, "#{name}?".to_sym, "#{name}=".to_sym].each do |n|
    if Mongoid.destructive_fields.include?(n)
      raise Errors::InvalidField.new(klass, n)
    end
  end
end
validate_relation(klass, name, options = {}) click to toggle source

Validate the association definition.

@example Validate the association definition.

Macro.validate(Model, :name)

@param [ Class ] klass The model class. @param [ Symbol ] name The field name. @param [ Hash ] options The provided options.

@since 6.0.0

# File lib/mongoid/fields/validators/macro.rb, line 57
def validate_relation(klass, name, options = {})
  [name, "#{name}?".to_sym, "#{name}=".to_sym].each do |n|
    if Mongoid.destructive_fields.include?(n)
      raise Errors::InvalidRelation.new(klass, n)
    end
  end
end

Private Instance Methods

validate_name_uniqueness(klass, name, options) click to toggle source

Determine if the field name is unique, if not raise an error.

@example Check the field name.

Macro.validate_name_uniqueness(Model, :name, {})

@param [ Class ] klass The model class. @param [ Symbol ] name The field name. @param [ Hash ] options The provided options.

@raise [ Errors::InvalidField ] If the name is not allowed.

@api private

# File lib/mongoid/fields/validators/macro.rb, line 98
def validate_name_uniqueness(klass, name, options)
  if !options[:overwrite] && klass.fields.keys.include?(name.to_s)
    if Mongoid.duplicate_fields_exception
      raise Errors::InvalidField.new(klass, name)
    else
      Mongoid.logger.warn("Overwriting existing field #{name} in class #{klass.name}.") if Mongoid.logger
    end
  end
end
validate_options(klass, name, options) click to toggle source

Validate that the field options are allowed.

@api private

@example Validate the field options.

Macro.validate_options(Model, :name, { localized: true })

@param [ Class ] klass The model class. @param [ Symbol ] name The field name. @param [ Hash ] options The provided options.

@raise [ Errors::InvalidFieldOption ] If an option is invalid.

@since 3.0.0

# File lib/mongoid/fields/validators/macro.rb, line 122
def validate_options(klass, name, options)
  options.keys.each do |option|
    if !OPTIONS.include?(option) && !Fields.options.include?(option)
      raise Errors::InvalidFieldOption.new(klass, name, option, OPTIONS)
    end

    if option == :type && options[option] == Symbol
      @field_type_is_symbol_warned ||= begin
        Mongoid.logger.warn(FIELD_TYPE_IS_SYMBOL)
        true
      end
    end
  end
end