DmitryTsepelev/store_model

Merging errors is supressing errors based on order of validations included

sairam opened this issue · 3 comments

Hi,

I have encountered an issue with errors being supressed from the main model. I am using the following versions.

  • Rails: 6.0.3.4

  • store_model 0.8.1

  • ruby version - 2.7.2

Here is my code -

class NotificationConfiguration
  include StoreModel::Model
  EMAIL_REGEXP = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.freeze
  validates :secondary_email, format: { with: EMAIL_REGEXP }, allow_blank: true
end

class User < ApplicationRecord
  attribute :configuration, BackupConfiguration.to_type

  validates_presence_of :name
  validates :configuration, store_model: { merge_errors: true }
  before_validation :build_notification_configuration, on: [:create]
end

user = User.create(name: 'test')
user.configuration.secondary_email = 'example'
user.name = ''
user.valid?
user.errors # => shows only 1 error instead of 2. 

If we modify the order of validations to maintain validates :configuration ... before validates_presence_of :name, it displays 2 errors instead of 1.

Hi @sairam, thank you for the report! Here is a fix #88, would you mind to try it out before I merge it in?

Just validated locally. Works as expected.

Thanks, 0.8.2 is on it's way!