Permalink
Browse files

Ensure we use mongoid's validators. Fixes #1993.

  • Loading branch information...
1 parent a231c02 commit c19a2e4c46aa62f4bf5f66914da11cec7902a127 @durran durran committed May 12, 2012
Showing with 34 additions and 0 deletions.
  1. +34 −0 lib/mongoid/validations.rb
View
@@ -141,6 +141,40 @@ def validates_uniqueness_of(*args)
validates_with(UniquenessValidator, _merge_attributes(args))
end
+ # Validates the format of a field.
+ #
+ # @example
+ # class Person
+ # include Mongoid::Document
+ # field :title
+ #
+ # validates_format_of :title, with: /^[a-z0-9 \-_]*$/i
+ # end
+ #
+ # @param [ Array ] args The names of the fields to validate.
+ #
+ # @since 2.4.0
+ def validates_format_of(*args)
+ validates_with(Mongoid::Validations::FormatValidator, _merge_attributes(args))
+ end
+
+ # Validates the length of a field.
+ #
+ # @example
+ # class Person
+ # include Mongoid::Document
+ # field :title
+ #
+ # validates_length_of :title, minimum: 100
+ # end
+ #
+ # @param [ Array ] args The names of the fields to validate.
+ #
+ # @since 2.4.0
+ def validates_length_of(*args)
+ validates_with(Mongoid::Validations::LengthValidator, _merge_attributes(args))
+ end
+
# Validates whether or not a field is present - meaning nil or empty.
#
# @example

0 comments on commit c19a2e4

Please sign in to comment.