Search code examples
rubyvalidationsequel

Sequel auto_validation not checking strings maxlength


I want to use auto_validations for my sequel models. Documentation says:

The auto_validations plugin automatically sets up the following types of validations for your model columns:

  1. type validations for all columns

  2. not_null validations on NOT NULL columns (optionally, presence validations)

  3. unique validations on columns or sets of columns with unique indexes

  4. max length validations on string columns

Using this code (mysql2 adapter):

class Sequel::Model
    plugin :schema
    plugin :validation_helpers
    plugin :auto_validations, :not_null=>:presence
end

unless DB.table_exists? (:unit_types)
    DB.create_table :unit_types do
        primary_key :id
        String      :name, :null => false
    end
end

class UnitType < Sequel::Model(:unit_types)
end

Created table schema is:

CREATE TABLE `unit_types` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    PRIMARY KEY (`id`)
)

Testing it in irb:

irb(main):009:0> UnitType.create()
Sequel::ValidationFailed: name is not present 

irb(main):010:0> UnitType.create(name: '')
Sequel::ValidationFailed: name is not present

That's good, but max length validation is not working:

irb(main):016:0* UnitType.create(name: "abc"*1000)
Sequel::DatabaseError: Mysql2::Error: Data too long for column 'name' at row 1

What to do?


Solution

  • @ymonad is absolutly right: problem was caused by slightly outdated Sequel version!