Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Length validator spec with minimum value doesn't work. #135

Open
joejeet opened this issue Nov 4, 2014 · 4 comments
Open

Length validator spec with minimum value doesn't work. #135

joejeet opened this issue Nov 4, 2014 · 4 comments
Labels

Comments

@joejeet
Copy link

joejeet commented Nov 4, 2014

As in rails has_secure_password the maximum length validation (should be less than or equal to 72 characters) is been added automatically for password field.

So, when you add something like this:

it { should validate_length_of(:password).with_minimum(6) }

it add length validation to the array and in array It just takes the first condition which comes true. So, length validation which is of maximum is accepted and ignores any other validation.

looks like this:

@klass.validators_on(@field)
=> [ Mongoid::Validatable::LengthValidator:0x007facb1068690 @attributes=[:password], @options={:maximum=>72}>,
ActiveModel::Validations::ConfirmationValidator:0x007facb106b688 @attributes=[:password], @options={:allow_blank=>true}>,
Mongoid::Validatable::LengthValidator:0x007facb103cc48 @attributes=[:password], @options={:if=>"password_confirmation.present?", :minimum=>6}>]

and it gives this error:

Failure/Error: it { should validate_length_of(:password).with_minimum(6) }
Expected User to have "length" validator on "password" with minimum of 6; instead got "length" validator on "password" with no minimum

@rodrigopinto
Copy link
Collaborator

@joejeet Could you paste here the snippet of your class with has_secure_password, the spec snippet and which version of the mongoid-rspec you are using. Thanks

@vidkun
Copy link

vidkun commented Jun 17, 2015

Getting same issue with slightly different circumstances.
User model:

class User
  include Mongoid::Document
  include ActiveModel::SecurePassword

  field :username, type: String
  field :email, type: String
  field :first_name, type: String
  field :last_name, type: String
  field :password_digest, type: String

  has_secure_password

  validates :password, length: {minimum: 8}

user_spec:

<snip>
it { is_expected.to validate_length_of(:password).with_minimum(8) }
<snip>

Gems:

mongoid (4.0.2)
mongoid-rspec (2.1.0)

@Tairy
Copy link

Tairy commented Apr 14, 2016

I had the same problem and what should i do to solve it? thx!

@BenMusch
Copy link

BenMusch commented Jul 20, 2016

Having a similar issue, I fixed it by adding on: :create to the initial validation (h/t j-dexx on StackOverflow.) It seems also seems as if this issue affects all validation (e.g. presence: true), not just length.

I was also able to reproduce this error outside of rspec when I ran the server, so there's a good chance that a) this isn't an rspec issue or b) I'm having an unrelated bug

However, this causes an issue with password resets (which I'm assuming are a common feature in most of the apps using has_secure_password). A very shitty, temporary fix I implemented was overriding #password= to decorate the functionality with addition validation like this:

def password=(other_password)
   if {validation logic fails}
      error.add(:password, {some error})
   end
   super other_password
end

@dblock dblock added the bug? label Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants