Method: Radiant::Config::Definition#validate

Defined in:
lib/radiant/config/definition.rb

#validate(setting) ⇒ Object

Checks the supplied value against the validation rules for this definition. There are several ways in which validations might be defined or implied:

  • if :validate_with specifies a block, the setting object is passed to the block

  • if :type is :integer, we test that the supplied string resolves to a valid integer

  • if the config item is a selector we test that its value is one of the permitted options

  • if :allow_blank has been set to false, we test that the value is not blank



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/radiant/config/definition.rb', line 93

def validate(setting)
  if allow_blank?
    return if setting.value.blank?
  else
    setting.errors.add :value, :blank if setting.value.blank?
  end
  if validate_with.is_a? Proc
    validate_with.call(setting)
  end
  if selector?
    setting.errors.add :value, :not_permitted unless selectable?(setting.value)
  end
  if integer?
    Integer(setting.value) rescue setting.errors.add :value, :not_a_number
  end
end