
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
activerecord-databasevalidations
Advanced tools
Add validations to your ActiveRecord models based on your database constraints.
This gem is primarily intended for MySQL databases not running in strict mode, which can easily cause data loss. These problems are documented in DataLossTest
Should be thread-safe. Please submit an issue or PR if you can prove otherwise.
Add this line to your application's Gemfile:
gem 'activerecord-databasevalidations'
And then execute:
$ bundle
Or install it yourself as:
$ gem install activerecord-databasevalidations
You can use ActiveModel's validates
method to define what fields you want
to validate based on the database constraints.
class Foo < ActiveRecord::Base
validates :boolean_field, database_constraints: :not_null
validates :string_field, database_constraints: [:size]
validates :decimal_field, :integer_field, database_constraints: :range
end
You can also use validates_database_constraints_of
:
class Bar < ActiveRecord::Base
validates_database_constraints_of :my_field, with: :size
end
You have to specify what conatrints you want to validate for. Valid values are:
:range
to validate the numeric range of a column based on it's type.:size
to validate for the size of textual and binary columns. It will pick character
size or bytesize based on the column's type.:not_null
to validate a NOT NULL contraint.The validations will only be created if it makes sense for the column, e.g. a :not_null
validation will only be added if the column has a NOT NULL constraint defined on it.
You can also instantiate the validators yourself:
class Bar < ActiveRecord::Base
validates :string_field, bytesize: { maximum: 255 }
validates :string_field, not_null: true
end
Note that this will create validations without inspecting the column to see if it actually makes sense.
Sometimes, truncated a string that goes over the column's limit is the best option, if you don't want one field's value being too long prevent the record from saving.
You can use truncate_string
to replicate MySQL's non-strict truncating behavior, so
you can prepare yourself for eventually turning on strict mode.
class Unicorn < ActiveRecord::Base
include ActiveRecord::DatabaseValidations::StringTruncator
before_validation truncate_string(:string_field)
validates :string_field, database_constraints: [:size]
end
In this example, it will truncate the string to a size that will fit before validation, so the subsequent size validation will now always pass.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that activerecord-databasevalidations demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.