Migration Validators
project. MySQL driver.
Define validations directly in DB as MySQL constraints and integrate them into your model transparently. See mv-core for details. There you will be able to review high level project information. Below you can see details of the migration validations that are supported by MySQL driver.
#Table Of Contents
Validations
uniqueness
Examples:
validate uniqueness of the column 'column_name':
def up
validates :table_name, :column_name, uniqueness: true
end
def down
validates :table_name, :column_name, uniqueness: false
end
define validation as trigger with specified failure message:
def up
validates :table_name, :column_name,
uniqueness: { message: 'Error message', as: :trigger }
end
def down
validates :table_name, :column_name, uniqueness: false
end
define validation as unique index:
def up
validates :table_name, :column_name, uniqueness: { as: :index }
end
def down
validates :table_name, :column_name, uniqueness: false
end
all above are available in a create and change table blocks:
def change
create_table :table_name do |t|
t.string :column_name, validates: { uniqueness: true }
end
end
def up
change :table_name do |t|
t.change :column_name, :string, :validates: { uniqueness: true }
end
end
def down
change :table_name do |t|
t.change :column_name, :string, :validates: { uniqueness: false }
end
end
simplifications (version >= 2.1 is required):
def change
create_table :table_name do |t|
t.string :column_name, uniqueness: true
end
end
Options:
message
- text of the error message that will be shown if constraint violated. Ignored unless :as == :trigger
index_name
- name of the index that will be created for validator. Ignored unless :as == :index
on
- validation event. Possible values [:save, :update, :create]
. Ignored unless :as == :trigger
. Default value :save
create_tigger_name
- name of the 'before insert' trigger that will be created if :as == :trigger
&& :on
in [:save, :create]
update_tigger_name
- name of the 'before update' trigger that will be created if :as == :trigger
&& :on
in [:save, :update]
allow_nil
- ignore validation for nil values. Ignored unless :as == :trigger
. Default value: false
allow_blank
- ignore validation for blank values. Ignored unless :as == :trigger
. Default value: false
as
- defines the way how constraint will be implemented. Possible values: [:index, :trigger]
. Default value: :index
length
Examples:
column value length should be more than 4 symbols and less than 9. Otherwise 'Wrong length message' error will be raised:
def up
validates :table_name, :column_name,
length: { in: 5..8,
message: 'Wrong length message' }
end
def down
validates :table_name, :column_name, length: false
end
allow NULL
:
def up
validates :table_name, :column_name,
length: { is: 3, allow_nil: true}
end
def down
validates :table_name, :column_name, length: false
end
allow blank values:
def up
validates :table_name, :column_name,
length: { maximum: 3,
too_long: 'Value is longer than 3 symbols' }
end
def down
validates :table_name, :column_name, length: false
end
all above are available in a create and change table blocks:
def change
create_table :table_name do |t|
t.string :column_name, validates: { length: { is: 3, allow_nil: true} }
end
end
def up
change :table_name do |t|
t.change :column_name, :string, validates: { length: { is: 3 } }
end
end
def down
change :table_name do |t|
t.change :column_name, :string, validates: { length: false }
end
end
simplifications (version >= 2.1 is required):
def change
create_table :table_name do |t|
t.string :string_3, length: 3
t.string :string_from_1_to_3, length: 1..3,
t.string :string_1_or_3, length: [1, 3]
t.string :string_4, validates: { length: 4 }
t.string :string_4_in_trigger: length: { is: 4, as: :trigger }
end
end
Options:
in
- range or array that length of the value should be contained in.within
- synonym of :in
is
- exact length of the valuemaximum
- maximum allowed lengthminimum
- minimum allowed lengthmessage
- message that should be shown if validation failed and specific message is not defined. Ignored unless :as == :trigger
too_long
- message that will be shown if value longer than allowed. Ignored unless maximum value is definedtoo_short
- message that will be shown if value shorter than allowed. Ignored unless minimum value is definedon
- validation event. Possible values [:save, :update, :create]
. Default value: :save
create_tigger_name
- Name of the 'before insert' triggerupdate_tigger_name
- Name of the 'before update' triggerallow_nil
- ignore validation for nil values. Default value: false
allow_blank
- ignore validation for blank values. Default value: false
as
- defines the way how constraint will be implemented. Possible values: [:trigger]
inclusion
Examples:
valid values array:
def up
validates :table_name, :column_name, inclusion: { in: [1, 2, 3] }
end
def up
validates :table_name, :column_name, inclusion: false
end
with failure message specified:
def up
validates :table_name, :column_name,
inclusion: { in: [1, 2, 3],
message: "Column value should be equal to 1 or 2 or 3" }
end
def down
validates :table_name, :column_name, inclusion: false
end
all above are available in a create and change table blocks:
def change
create_table :table_name do |t|
t.integer :column_name, validates: { inclusion: { in: 1..3 } }
end
end
def up
change :table_name do |t|
t.change :column_name, :integer, validates: { inclusion: { in: 1..3 } }
end
end
def down
change :table_name do |t|
t.change :column_name, :integer, validates: { inclusion: false }
end
end
simplifications (version >= 2.1 is required):
def change
create_table :table_name do |t|
t.string :str_or_str_1, inclusion: ['str', 'str1']
t.string :from_str_to_str_1, inclusion: 'str'..'str1'
t.string :str_or_str_1_in_trigger, inclusion: { in: ['str', 'str1'],
as: :trigger}
end
end
Options:
in
- range or array that column value should be contained in.message
- that should be shown if validation failed. Ignored unless :as == :trigger
on
- validation event. Possible values [:save, :update, :create]
. Default value: :save
create_tigger_name
- Name of the 'before insert' triggerupdate_tigger_name
- Name of the 'before update' triggerallow_nil
- ignore validation for nil values. Default value: false
allow_blank
- ignore validation for blank values. Default value: false
as
- defines the way how constraint will be implemented. Possible values: [:trigger]
exclusion
Examples:
exclude 1, 2, and 3:
def up
validates :table_name, :column_name, exclusion: { in: [1, 2, 3] }
end
def down
validates :table_name, :column_name, exclusion: false
end
the same with failure message:
def up
validates :table_name, :column_name,
exclusion: {
in: [1, 2, 3],
message: "Column value should not be equal to 1 or 2 or 3" }
end
def down
validates :table_name, :column_name, exclusion: false
end
all above are available in a create and change table blocks:
def change
create_table :table_name do |t|
t.integer :column_name, validates: { exclusion: { in: 1..3 } }
end
end
def up
change :table_name do |t|
t.change :column_name, :integer, validates: { exclusion: { in: 1..3 } }
end
end
def down
change :table_name do |t|
t.change :column_name, :integer, validates: { exclusion: false }
end
end
simplifications (version >= 2.1 is required):
def change
create_table :table_name do |t|
t.string :neither_str_nor_str_1, exclusion: ['str', 'str1']
t.string :from_str_to_str_1, exclusion: 'str'..'str1'
t.string :str_or_str_1_in_trigger, exclusion: { in: ['str', 'str1'],
as: :trigger}
end
end
Options:
in
- range or array that column value should NOT be contained in.message
- message that should be shown if validation failed. Ignored unless :as == :trigger
on
- validation event. Possible values [:save, :update, :create]
. Default value: :save
create_tigger_name
- Name of the 'before insert' triggerupdate_tigger_name
- Name of the 'before update' triggerallow_nil
- ignore validation for nil values. Default value: false
allow_blank
- ignore validation for blank values. Default value: false
as
- defines the way how constraint will be implemented. Possible values: [:trigger]
presence
Examples:
def up
validates :table_name, :column_name, presence: true
end
def down
validates :table_name, :column_name, presence: false
end
with failure message:
def up
validates :table_name, :column_name,
presence: { message: 'value should not be empty' }
end
def down
validates :table_name, :column_name, presence: false
end
check when record is inserted only:
def up
validates :table_name, :column_name,
presence: { message: 'value should not be empty',
as: :trigger,
on: :create }
end
def down
validates :table_name, :column_name, presence: false
end
all above are available in a create and change table blocks:
def change
create_table :table_name do |t|
t.string :column_name, validates: { presence: true }
end
end
def up
change :table_name do |t|
t.change :column_name, :string, validates: { presence: true }
end
end
def down
change :table_name do |t|
t.change :column_name, :string, validates: false
end
end
simplifications (version >= 2.1 is required):
def change
create_table :table_name do |t|
t.string :presence_in_check, presence: true
t.string :presence_in_trigger, presence: { as: :trigger, on: :create }
end
end
Options:
message
- message that should be shown if validation failed. Ignored unless :as == :trigger
on
- validation event. Possible values [:save, :update, :create]
. Ignored unless :as == :trigger
. Default value: :save
create_tigger_name
- Name of the 'before insert' trigger that will be created if :as == :trigger
&& :on
in [:save, :create]
update_tigger_name
- Name of the 'before update' trigger that will be created if :as == :trigger
&& :on
in [:save, :update]
allow_nil
- ignore validation for nil
values. Default value: false
allow_blank
- ignore validation for blank values. Default value: false
as
- defines the way how constraint will be implemented. Possible values: [:trigger]
Default value: :trigger
absence
Examples:
def up
validates :table_name, :column_name, absence: true
end
def down
validates :table_name, :column_name, absence: false
end
with failure message:
def up
validates :table_name, :column_name,
absence: { message: 'value should be empty' }
end
def down
validates :table_name, :column_name, absence: false
end
check when record is inserted only:
def up
validates :table_name, :column_name,
absence: { message: 'value should be empty',
as: :trigger,
on: :create }
end
def down
validates :table_name, :column_name, absence: false
end
all above are available in a create and change table blocks:
def change
create_table :table_name do |t|
t.string :column_name, validates: { absence: true }
end
end
def up
change :table_name do |t|
t.change :column_name, :string, validates: { absence: true }
end
end
def down
change :table_name do |t|
t.change :column_name, :string, validates: { absence: false }
end
end
simplifications (version >= 2.1 is required):
def change
create_table :table_name do |t|
t.string :absence_in_check, absence: true
t.string :absence_in_trigger, absence: { as: :trigger, on: :create }
end
end
Options:
message
- message that should be shown if validation failed. Ignored unless :as == :trigger
on
- validation event. Possible values [:save, :update, :create]
. Ignored unless :as == :trigger
. Default value: :save
create_tigger_name
- Name of the 'before insert' trigger that will be created if :as == :trigger
&& :on
in [:save, :create]
update_tigger_name
- Name of the 'before update' trigger that will be created if :as == :trigger
&& :on
in [:save, :update]
allow_nil
- ignore validation for nil
values. Default value: true
allow_blank
- ignore validation for blank values. Default value: true
as
- defines the way how constraint will be implemented. Possible values: [:trigger]
Default value: :trigger
format
Examples:
allows only values that contains 'word' inside:
def up
validates :table_name, :column_name, format: { with: /word/ }
end
def down
validates :table_name, :column_name, format: false
end
with failure message:
def up
validates :table_name, :column_name,
format: { with: /word/,
message: 'Column_name value should contain start word' }
end
def down
validates :table_name, :column_name, format: false
end
implemented as trigger:
def up
validates :table_name, :column_name,
format: { with: /word/,
message: 'Column_name value should contain start word',
as: :trigger }
end
def down
validates :table_name, :column_name, format: false
end
all above are available in a create and change table blocks:
def change
create_table :table_name do |t|
t.string :column_name, validates { format: { with: /word/ } }
end
end
def up
change :table_name do |t|
t.change :column_name, :string, validates: { format: { with: /word/ } }
end
end
def down
change :table_name do |t|
t.change :column_name, :string, validates: { format: false }
end
end
simplifications (version >= 2.1 is required):
def change
create_table :table_name do |t|
t.string :contains_word, format: /word/
t.string :contains_word_in_trigger, format: { with: /word/,
as: :trigger }
end
end
Options:
with
- regular expression that column value should be matched tomessage
- message that should be shown if validation failed. Ignored unless :as == :trigger
on
- validation event. Possible values [:save, :update, :create]
. Ignored unless :as == :trigger
. Default value: :save
create_tigger_name
- Name of the 'before insert' trigger that will be created if :as == :trigger
&& :on
in [:save, :create]
update_tigger_name
- Name of the 'before update' trigger that will be created if :as == :trigger
&& :on
in [:save, :update]
allow_nil
- ignore validation for nil
values. Default value: false
allow_blank
- ignore validation for blank values. Default value: false
as
- defines the way how constraint will be implemented. Possible values: [:trigger]
Default value: :trigger
custom
(version >= 2.1 is required)
Examples:
allows only values that equals 'word' when trimmed:
def up
validates :table_name, :column_name,
custom: { statement: "TRIM({column_name}) = 'word'" }
end
def down
validates :table_name, :column_name, custom: false
end
with failure message:
def up
validates :table_name, :column_name,
custom: { statement: "TRIM({column_name}) = 'word'",
message: 'Column_name value should contain start word' }
end
def down
validates :table_name, :column_name, custom: false
end
implemented as trigger on insert event:
def up
validates :table_name, :column_name,
custom: { statement: "TRIM({column_name}) = 'word'",
message: 'Column_name value should contain start word',
as: :trigger,
on: :create }
end
def down
validates :table_name, :column_name, custom: false
end
all above are available in a create and change table blocks:
def change
create_table :table_name do |t|
t.string :column_name,
validates: { custom: { statement: "TRIM({column_name}) = 'word'"} }
end
end
def up
change :table_name do |t|
t.change :column_name, :string,
validates: { custom: { statement: "TRIM({column_name}) = 'word'"} }
end
end
def down
change :table_name do |t|
t.change :column_name, :string, validates: { custom: false }
end
end
simplifications (version >= 2.1 is required):
def change
create_table :table_name do |t|
t.string :contains_word, custom: "TRIM({contains_word}) = 'word'"
t.string :contains_word_synonym,
validates: "TRIM({contains_word_synonym}) = 'word'"
t.string :contains_word_in_trigger,
custom: { statement: "TRIM({contains_word_in_trigger}) = 'word'", as: :trigger }
end
end
Options:
message
- message that should be shown if validation failed. Ignored unless :as == :trigger
on
validation event. Possible values [:save, :update, :create]
. Default value: :save
create_tigger_name
- name of the 'before insert' triggerupdate_tigger_name
- name of the 'before update' triggerallow_nil
- ignore validation for nil values. Default value: falseallow_blank
- ignore validation for blank values. Default value: false
as
- defines the way how constraint will be implemented. Possible values: [:trigger]
Version History
(2.0.0) (17 Jan, 2015)
- Completely rewritten. Migrated to Ruby 2.0 and RoR 4
(2.1.0) (22 Jan, 2015)
(2.2.0) (28 Jan, 2015)
- Integration with ActiveRecord
(2.2.1) (23 May, 2015)
(2.2.2) (29 May, 2015)
- Made it possible to use several mv-* drivers in the same project
(2.2.3) (20 Jul, 2015)
- Fix issue with invalid parameters number in
add_column
and change_column
methods
(2.2.4) (23 Nov, 2015)
- Do not camel case column name in error message
- Use SIGNAL to raise validation exception for MySQL >= 5.5
(2.2.5) (23 Feb, 2016)
- Suppress exception while running db:schema:load
(2.2.6) (12 Sep, 2016)
- Escape single quotes in the custom validation statement body
Contributing
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
- Fork the project
- Start a feature/bugfix branch
- Commit and push until you are happy with your contribution
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.