Module | Sequel::Plugins::ValidationHelpers |
In: |
lib/sequel/plugins/validation_helpers.rb
|
The validation_helpers plugin contains instance method equivalents for most of the legacy class-level validations. The names and APIs are different, though. Example:
Sequel::Model.plugin :validation_helpers class Album < Sequel::Model def validate validates_min_length 1, :num_tracks end end
The validates_unique validation has a unique API, but the other validations have the API explained here:
Arguments:
Options:
Sequel.extension :blank
The default validation options for all models can be modified by changing the values of the Sequel::Plugins::ValidationHelpers::DEFAULT_OPTIONS hash. You change change the default options on a per model basis by overriding a private instance method default_validation_helpers_options.
DEFAULT_OPTIONS | = | { :exact_length=>{:message=>lambda{|exact| "is not #{exact} characters"}}, :format=>{:message=>lambda{|with| 'is invalid'}}, :includes=>{:message=>lambda{|set| "is not in range or set: #{set.inspect}"}}, :integer=>{:message=>lambda{"is not a number"}}, :length_range=>{:message=>lambda{|range| "is too short or too long"}}, :max_length=>{:message=>lambda{|max| "is longer than #{max} characters"}}, :min_length=>{:message=>lambda{|min| "is shorter than #{min} characters"}}, :not_string=>{:message=>lambda{|type| type ? "is not a valid #{type}" : "is a string"}}, :numeric=>{:message=>lambda{"is not a number"}}, :type=>{:message=>lambda{|klass| "is not a #{klass}"}}, :presence=>{:message=>lambda{"is not present"}}, :unique=>{:message=>lambda{'is already taken'}} | Default validation options used by Sequel. Can be modified to change the error messages for all models (e.g. for internationalization), or to set certain default options for validations (e.g. :allow_nil=>true for all validates_format). |