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

Validator custom error message causes test matcher error #203

Closed
Mth0158 opened this issue Oct 26, 2023 · 5 comments · Fixed by #206
Closed

Validator custom error message causes test matcher error #203

Mth0158 opened this issue Oct 26, 2023 · 5 comments · Fixed by #206

Comments

@Mth0158
Copy link
Collaborator

Mth0158 commented Oct 26, 2023

From @Alagaesia93 in #201


Hi there!

I got into the same issue today and 1.1.1 does not fix it if you have a custom message.

In your example

  validates :avatar, content_type: %w[image/jpeg], size: { greater_than: 0, message: "whatever foobar" }

The spec fails

My example is with less_than, but I think it's the same

class Shop < ApplicationRecord
  has_one_attached :logo
  validates :logo, size: { less_than: 10.megabytes }, content_type: ['image/png', 'image/jpg', 'image/jpeg'] # works
  validates :logo, size: { less_than: 10.megabytes, message: "must be less than 10 MB" }, content_type: ['image/png', 'image/jpg', 'image/jpeg'] # fails
end
describe Shop do
  it { is_expected.to validate_size_of(:logo).less_than(10.megabytes) }
end

Originally posted by @Alagaesia93 in #201 (comment)

@andyundso
Copy link

I can add more context. We have a test which looks like this:

test 'should validate attachment format' do
  project = create(:project)
  project.image.attach(io: file_fixture("hello.csv"), filename: "hello.csv")
  project.validate

  assert project.errors.of_kind? :image, :content_type_invalid
end

The assertion fails now. When inspecting the project.errors, it looks like this:

#<ActiveModel::Errors [#<ActiveModel::Error attribute=image, type=Das Bild muss im JPG oder PNG-Format sein, options={:message=>"Das Bild muss im JPG oder PNG-Format sein", :authorized_types=>"X-PNG, PNG, JPG, JPEG, PJPEG", :content_type=>"text/csv"}>]>

Prior to v1.1, it looked like:

#<ActiveModel::Errors [#<ActiveModel::Error attribute=image, type=content_type_invalid, options={:authorized_types=>"X-PNG, PNG, JPG, JPEG, PJPEG", :message=>"Das Bild muss im JPG oder PNG-Format sein", :content_type=>"text/csv"}>]>

Think the key difference is that type now includes the translated message, instead of the actual error type.

@Mth0158
Copy link
Collaborator Author

Mth0158 commented Nov 3, 2023

Thanks a lot @andyundso! I am currently refactoring the matcher tests of the gem so we do not get these regressions.
This issue should be corrected soon (1 or 2 weeks), I'll keep you update once it's merged 📦

Mth0158 added a commit to Mth0158/active_storage_validations that referenced this issue Nov 8, 2023
@andyundso
Copy link

Hi @Mth0158

I updated to v1.1.3 today and still see the behaviour as I reported in #203 (comment). Is this the expected behaviour now?

@Mth0158
Copy link
Collaborator Author

Mth0158 commented Dec 22, 2023

Hi @andyundso
I thought it was corrected, but this edge case is not handled properly. I think it's the same as #234 because there are 2 size validators on the same attribute.
I'll work a on a fix in the coming days, if it's the same issue as #234 I'll close this issue again, let me know

@Mth0158 Mth0158 reopened this Dec 22, 2023
@Mth0158
Copy link
Collaborator Author

Mth0158 commented Jan 5, 2024

Hi @andyundso,
The fix has been merged, it'll be available in the next release ;)

@Mth0158 Mth0158 closed this as completed Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants