You are looking at the docs for v4.x. You can check out this page for Mongoid v3.x if you haven't upgraded yet.


Mongoid includes ActiveModel::Validations to supply the basic validation plus an additional associated and uniqueness validator.

See ActiveModel::Validations documentation for more information.

Mongoid behaves slightly different to Active Record when using #valid? on already persisted data. Active Record's #valid? will run all validations whereas Mongoid's #valid? will only run validations on documents that are in memory as an optimization.

Common options that can be passed to all validations:

In addition to those validations, information is provided with each macro about its specific options.

Validation Macro Options

Validate acceptance of terms.

validates_acceptance_of :terms
validates :terms, acceptance: true
:accept # Specify the accepted value. Default: 1.

Validate associated documents when the parent is validated. This only validates documents in memory.

validates_associated :albums
validates :albums, associated: true

Validate confirmation of a field, with a "_confirmation" suffix.

validates_confirmation_of :password
validates :password, confirmation: true

Validate items are not in a list.

validates_exclusion_of :employers, in: [ "SoundCloud" ]
validates :employers, exclusion: { in: [ "SoundCloud" ] }
:in # Required list of values.

Validate the format of a field.

validates_format_of :title, with: /\A\w+\Z/
validates :title, format: { with: /\A\w+\Z/ }
:allow_blank # Whether the field can be blank.
:in # A list or range of values.
:with # A regular expression to match.
:without # A regular expression not to match.

Validate items are included in a list.

validates_inclusion_of :employers, in: [ "SoundCloud" ]
validates :employers, inclusion: { in: [ "SoundCloud" ] }
:allow_blank # Whether the field can be blank.
:in # Required list of values.

Validate the length of a field.

validates_length_of :password, minimum: 8, maximum: 16
validates :password, length: { minimum: 8, maximum: 16 }
:allow_blank # Whether to validate blank attributes.
:in # The range the length can fall within.
:maximum # The maximum length of the attribute.
:minimum # The minimum length of the attribute.
:tokenizer # A block tokenizer.
:too_long # Custom message if too long.
:too_short # Custom message if too short.
:within # Range the length can fall within.
:wrong_length # Custom message for an incorrect length.

Validate the numericality of a field.

validates_numericality_of :age, even: true
validates :age, numericality: { even: true }
:equal_to # A value the field must be exactly.
:even # Set that the value must be even.
:odd # Set that the value must be odd.
:only_integer # Set whether the value has to be an integer.

Validate that an attribute exists. Note that if you add a presence validation to a relation, then Mongoid will enable autosave for that relation.

validates_presence_of :name
validates :name, presence: true

Validate that an attribute is unique. Note that for embedded documents, this will only check that the field is unique within the context of the parent document, not the entire database.

validates_uniqueness_of :name
validates :name, uniqueness: true
:case_sensitive # Whether to use case sensitive matching.
:scope # Scope checks to the value of this field.