Migration Validators project. SQLite driver.

Define validations directly in DB as SQLite 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 SQLite driver.

Table Of Contents

Validations

uniqueness

Examples:

validate uniqueness of the column 'column_name':

“`ruby 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:

“`ruby 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:

“`ruby 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:

ruby def change create_table :table_name do |t| t.string :column_name, validates: { uniqueness: true } end end

“`ruby 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):

ruby def change create_table :table_name do |t| t.string :column_name, uniqueness: true end end

Options:

length

Examples:

column value length should be more than 4 symbols and less than 9. Otherwise 'Wrong length message' error will be raised:

“`ruby 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:

“`ruby 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:

“`ruby 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:

ruby def change create_table :table_name do |t| t.string :column_name, validates: { length: { is: 3, allow_nil: true} } end end

“`ruby 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):

ruby 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:

inclusion

Examples:

valid values array:

“`ruby def up validates :table_name, :column_name, inclusion: { in: [1, 2, 3] } end

def down validates :table_name, :column_name, inclusion: false end “`

with failure message specified:

“`ruby 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:

ruby def change create_table :table_name do |t| t.integer :column_name, validates: { inclusion: { in: 1..3 } } end end

“`ruby 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):

ruby 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:

exclusion

Examples:

exclude 1, 2, and 3:

“`ruby 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:

“`ruby def up validates :table_name, :column_name, exclusion: { in: [1, 2, 3], message: “Column 'column_name' 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:

ruby def change create_table :table_name do |t| t.integer :column_name, validates: { exclusion: { in: 1..3 } } end end

“`ruby 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):

ruby 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:

presence

Examples:

“`ruby def up validates :table_name, :column_name, presence: true end

def down validates :table_name, :column_name, presence: false end “`

with failure message:

“`ruby 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:

“`ruby 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:

ruby def change create_table :table_name do |t| t.string :column_name, validates: { presence: true } end end

“`ruby 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: { presence: false } end end “`

simplifications (version >= 2.1 is required):

ruby 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:

absence

Examples:

“`ruby def up validates :table_name, :column_name, absence: true end

def down validates :table_name, :column_name, absence: false end “`

with failure message:

“`ruby 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:

“`ruby 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:

ruby def change create_table :table_name do |t| t.string :column_name, validates: { absence: true } end end

“`ruby 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):

ruby 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:

format

WARNING SQLite does NOT have default REGEXP implementation. mv-sqlite contains own implementation that is available with active record connection only. In other words everything should be ok if you access database from withing rails console or rails application or other application that uses activerecord internally. But not defined function error will be thrown when you access database direclty. You can build SQLite ICU extension to enable native SQLite REGEXP. See details here: www.sqlite.org/src/artifact?ci=trunk&filename=ext/icu/README.txt.

Examples:

allows only values that contains 'word' inside:

“`ruby def up validates :table_name, :column_name, format: { with: /word/ } end

def down validates :table_name, :column_name, format: false end “`

with failure message:

“`ruby 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:

“`ruby 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:

ruby def change create_table :table_name do |t| t.string :column_name, validates { format: { with: /word/ } } end end

“`ruby 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):

ruby 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:

custom

(version >= 2.1 is required)

Examples:

allows only values that equals 'word' when trimmed:

“`ruby 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:

“`ruby 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:

“`ruby 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:

ruby def change create_table :table_name do |t| t.string :column_name, validates: { custom: { statement: "TRIM({column_name}) = 'word'"} } end end

“`ruby 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):

ruby 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:

Version History

(2.0.0) (17 Jan, 2015)

(2.1.0) (22 Jan, 2015)

(2.2.0) (28 Jan, 2015)

(2.2.1) (29 May, 2015)

(2.2.2) (1 June, 2015)

(2.2.3) (20 Jul, 2015)

(2.2.4) (23 Nov, 2015)

(2.2.5) (23 Feb, 2016)

(2.2.6) (12 Sep, 2016)

Contributing