{"id":13879562,"url":"https://github.com/igorkasyanchuk/active_storage_validations","last_synced_at":"2026-04-01T23:16:17.156Z","repository":{"id":38361991,"uuid":"146472360","full_name":"igorkasyanchuk/active_storage_validations","owner":"igorkasyanchuk","description":"Do it like =\u003e validates :photos, attached: true, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: 500.kilobytes }, limit: { min: 1, max: 3 }, aspect_ratio: :landscape, dimension: { width: { in: 800..1600 }","archived":false,"fork":false,"pushed_at":"2026-03-22T12:18:02.000Z","size":2891,"stargazers_count":1169,"open_issues_count":21,"forks_count":148,"subscribers_count":9,"default_branch":"master","last_synced_at":"2026-03-27T02:54:17.280Z","etag":null,"topics":["active-storage","activestorage","rails","validations"],"latest_commit_sha":null,"homepage":"https://www.railsjazz.com/","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/igorkasyanchuk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":".github/FUNDING.yml","license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"Mth0158","patreon":"igorkasyanchuk"}},"created_at":"2018-08-28T15:59:26.000Z","updated_at":"2026-03-22T09:36:33.000Z","dependencies_parsed_at":"2023-02-10T18:01:00.095Z","dependency_job_id":"96d48a1a-fa2e-4bf1-9418-a14693a7c03f","html_url":"https://github.com/igorkasyanchuk/active_storage_validations","commit_stats":{"total_commits":408,"total_committers":75,"mean_commits":5.44,"dds":0.6813725490196079,"last_synced_commit":"74ab67f73cb63cd08278a2e420d520d6b340c81d"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/igorkasyanchuk/active_storage_validations","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Factive_storage_validations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Factive_storage_validations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Factive_storage_validations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Factive_storage_validations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/igorkasyanchuk","download_url":"https://codeload.github.com/igorkasyanchuk/active_storage_validations/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Factive_storage_validations/sbom","scorecard":{"id":482714,"data":{"date":"2025-08-11","repo":{"name":"github.com/igorkasyanchuk/active_storage_validations","commit":"139ec2436c3b69a8514953e3488fde9586c1663b"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.6,"checks":[{"name":"Maintained","score":10,"reason":"30 commit(s) and 10 issue activity found in the last 90 days -- score normalized to 10","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":2,"reason":"Found 4/18 approved changesets -- score normalized to 2","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/main.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/igorkasyanchuk/active_storage_validations/main.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/igorkasyanchuk/active_storage_validations/main.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:64: update your workflow using https://app.stepsecurity.io/secureworkflow/igorkasyanchuk/active_storage_validations/main.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:87: update your workflow using https://app.stepsecurity.io/secureworkflow/igorkasyanchuk/active_storage_validations/main.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:96: update your workflow using https://app.stepsecurity.io/secureworkflow/igorkasyanchuk/active_storage_validations/main.yml/master?enable=pin","Info:   0 out of   2 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   3 third-party GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: MIT-LICENSE:0","Info: FSF or OSI recognized license: MIT License: MIT-LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 21 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"12 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-76r7-hhxj-r776","Warn: Project is vulnerable to: GHSA-r4mg-4433-c7g3","Warn: Project is vulnerable to: GHSA-353f-x4gh-cqq8","Warn: Project is vulnerable to: GHSA-5w6v-399v-w3cc","Warn: Project is vulnerable to: GHSA-mrxw-mxhj-p664","Warn: Project is vulnerable to: GHSA-vvfq-8hwr-qm4m","Warn: Project is vulnerable to: GHSA-47m2-26rw-j2jw","Warn: Project is vulnerable to: GHSA-7g2v-jj9q-g3rg","Warn: Project is vulnerable to: GHSA-7wqh-767x-r66v","Warn: Project is vulnerable to: GHSA-8cgq-6mh2-7j6v","Warn: Project is vulnerable to: GHSA-gjh7-p2fx-99vx","Warn: Project is vulnerable to: GHSA-9j94-67jr-4cqj"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-19T17:00:58.787Z","repository_id":38361991,"created_at":"2025-08-19T17:00:58.792Z","updated_at":"2025-08-19T17:00:58.792Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31205534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-30T15:24:02.938Z","status":"ssl_error","status_checked_at":"2026-03-30T15:23:44.804Z","response_time":138,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["active-storage","activestorage","rails","validations"],"created_at":"2024-08-06T08:02:25.282Z","updated_at":"2026-04-01T23:16:17.145Z","avatar_url":"https://github.com/igorkasyanchuk.png","language":"Ruby","funding_links":["https://github.com/sponsors/Mth0158","https://patreon.com/igorkasyanchuk","https://www.patreon.com/igorkasyanchuk","https://buymeacoffee.com/igorkasyanchuk"],"categories":["Ruby"],"sub_categories":[],"readme":"[\u003cimg src=\"https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true\"\n/\u003e](https://www.railsjazz.com/?utm_source=github\u0026utm_medium=top\u0026utm_campaign=active_storage_validations)\n\n# Active Storage Validations\n\n[![MiniTest](https://github.com/igorkasyanchuk/active_storage_validations/workflows/MiniTest/badge.svg)](https://github.com/igorkasyanchuk/active_storage_validations/actions)\n[![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)\n[![https://www.patreon.com/igorkasyanchuk](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/patron.svg?raw=true)](https://www.patreon.com/igorkasyanchuk)\n\n[![\"Buy Me A Coffee\"](https://github.com/igorkasyanchuk/get-smart/blob/main/docs/snapshot-bmc-button-small.png?raw=true)](https://buymeacoffee.com/igorkasyanchuk)\n\nActive Storage Validations is a gem that allows you to add validations for Active Storage attributes.\n\nThis gems is doing it right for you! Just use `validates :avatar, attached: true, content_type: 'image/png'` and that's it!\n\n## Table of Contents\n\n- [Getting started](#getting-started)\n  - [Installation](#installation)\n  - [Error messages (I18n)](#error-messages-i18n)\n  - [Using image metadata validators](#using-image-metadata-validators)\n  - [Using video and audio metadata validators](#using-video-and-audio-metadata-validators)\n  - [Using pdf metadata validators](#using-pdf-metadata-validators)\n  - [Using content type spoofing protection validator option](#using-content-type-spoofing-protection-validator-option)\n- [Validators](#validators)\n  - [Attached](#attached)\n  - [Limit](#limit)\n  - [Content type](#content-type)\n  - [Size](#size)\n  - [Total size](#total-size)\n  - [Dimension](#dimension)\n  - [Duration](#duration)\n  - [Aspect ratio](#aspect-ratio)\n  - [Processable file](#processable-file)\n  - [Pages](#pages)\n- [Upgrading from 1.x to 2.x](#upgrading-from-1x-to-2x)\n- [Upgrading from 2.x to 3.x](#upgrading-from-2x-to-3x)\n- [Internationalization (I18n)](#internationalization-i18n)\n- [Test matchers](#test-matchers)\n- [Contributing](#contributing)\n- [Additional information](#additional-information)\n\n## Getting started\n\n### Installation\n\nActive Storage Validations work with Rails 6.1.4 onwards. Add this line to your application's Gemfile:\n\n```ruby\ngem 'active_storage_validations'\n```\n\nAnd then execute:\n\n```sh\n$ bundle\n```\n\n### Error messages (I18n)\n\nOnce you have installed the gem, I18n error messages will be added automatically to your app. See [Internationalization (I18n)](#internationalization-i18n) section for more details.\n\n### Using image metadata validators\n\nOptionally, to use the image metadata validators (`dimension`, `aspect_ratio` and `processable_file`), you will have to add one of the corresponding gems:\n\n```ruby\ngem 'mini_magick', '\u003e= 4.9.5'\n# Or\ngem 'ruby-vips', '\u003e= 2.1.0'\n```\n\nPlus, you have to be sure to have the corresponding command-line tool installed on your system. For example, to use `mini_magick` gem, you need to have `imagemagick` installed on your system (both on your local and in your CI / production environments).\n\n### Using video and audio metadata validators\n\nTo use the video and audio metadata validators (`dimension`, `aspect_ratio`, `processable_file` and `duration`), you will not need to add any gems. However you will need to have the `ffmpeg` command-line tool installed on your system (once again, be sure to have it installed both on your local and in your CI / production environments).\n\n### Using pdf metadata validators\n\nTo use the pdf metadata validators (`dimension`, `aspect_ratio`, `processable_file` and `pages`), you will not need to add any gems. However you will need to have the `poppler` tool installed on your system (once again, be sure to have it installed both on your local and in your CI / production environments).\n\n### Using content type spoofing protection validator option\n\nTo use the `spoofing_protection` option with the `content_type` validator, you only need to have the UNIX `file` command on your system.\n\nIf you want some inspiration about how to add `imagemagick`, `libvips`, `ffmpeg` or `poppler` to your docker image, you can check how we do it for the gem CI (https://github.com/igorkasyanchuk/active_storage_validations/blob/master/.github/workflows/main.yml)\n\n## Validators\n\n**List of validators:**\n- [Attached](#attached): validates if file(s) attached\n- [Limit](#limit): validates number of uploaded files\n- [Content type](#content-type): validates file content type\n- [Size](#size): validates file size\n- [Total size](#total-size): validates total file size for several files\n- [Dimension](#dimension): validates image / video dimensions\n- [Duration](#duration): validates video / audio duration\n- [Aspect ratio](#aspect-ratio): validates image / video aspect ratio\n- [Processable file](#processable-file): validates if a file can be processed\n- [Pages](#pages): validates pdf number of pages\n\u003cbr\u003e\n\u003cbr\u003e\n\n**Proc usage**\u003cbr\u003e\nEvery validator can use procs instead of values in all the validator examples:\n```ruby\nclass User \u003c ApplicationRecord\n  has_many_attached :files\n\n  validates :files, limit: { max: -\u003e (record) { record.admin? ? 100 : 10 } }\nend\n```\n\n**Performance optimization**\u003cbr\u003e\nSome validators rely on an expensive operation (metadata analysis and content type analysis). To mitigate the performance cost, the gem leverages the `ActiveStorage::Blob.metadata` method to store retrieved metadata. Therefore, once the file has been analyzed by our gem, the expensive analysis operation will not be triggered again for new validations.\n\nAs stated in the Rails documentation: \"Blobs are intended to be immutable in so far as their reference to a specific file goes\". We based our performance optimization on the same assumption, so if you do not follow it, the gem will not work as expected.\n\n---\n\n### Attached\n\nValidates if the attachment is present.\n\n#### Options\n\nThe `attached` validator has no options.\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_one_attached :avatar\n\n  validates :avatar, attached: true # ensures that avatar has an attached file\nend\n```\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      blank: \"can't be blank\"\n```\n\nThe error message for this validator relies on Rails own `blank` error message.\n\n---\n\n### Limit\n\nValidates the number of uploaded files.\n\n#### Options\n\nThe `limit` validator has 2 possible options:\n- `min`: defines the minimum allowed number of files\n- `max`: defines the maximum allowed number of files\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_many_attached :certificates\n\n  validates :certificates, limit: { min: 1, max: 10 } # restricts the number of files to between 1 and 10\nend\n```\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      limit_out_of_range:\n        zero: \"no files attached (must have between %{min} and %{max} files)\"\n        one: \"only 1 file attached (must have between %{min} and %{max} files)\"\n        other: \"total number of files must be between %{min} and %{max} files (there are %{count} files attached)\"\n      limit_min_not_reached:\n        zero: \"no files attached (must have at least %{min} files)\"\n        one: \"only 1 file attached (must have at least %{min} files)\"\n        other: \"%{count} files attached (must have at least %{min} files)\"\n      limit_max_exceeded:\n        zero: \"no files attached (maximum is %{max} files)\"\n        one: \"too many files attached (maximum is %{max} files, got %{count})\"\n        other: \"too many files attached (maximum is %{max} files, got %{count})\"\n```\n\nThe `limit` validator error messages expose 3 values that you can use:\n- `min` containing the minimum allowed number of files (e.g. `1`)\n- `max` containing the maximum allowed number of files (e.g. `10`)\n- `count` containing the current number of files (e.g. `5`)\n\n---\n\n### Content type\n\nValidates if the attachment has an allowed content type.\n\n#### Options\n\nThe `content_type` validator has 3 possible options:\n- `with`: defines the allowed content type (string, symbol or regex)\n- `in`: defines the allowed content types (array of strings or symbols)\n- `spoofing_protection`: enables content type spoofing protection (boolean, defaults to `false`)\n\nAs mentioned above, this validator can define content types in several ways:\n- String: `image/png` or `png`\n- Symbol: `:png`\n- Regex: `/\\Avideo\\/.*\\z/`\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_one_attached :avatar\n\n  validates :avatar, content_type: 'image/png' # only allows PNG images\n  validates :avatar, content_type: :png # only allows PNG images, same as { with: :png }\n  validates :avatar, content_type: /\\Avideo\\/.*\\z/ # only allows video files\n  validates :avatar, content_type: ['image/png', 'image/jpeg'] # only allows PNG and JPEG images\n  validates :avatar, content_type: { in: [:png, :jpeg], spoofing_protection: true } # only allows PNG, JPEG and their variants, with spoofing protection enabled\nend\n```\n\n#### Best practices\n\nWhen using the `content_type` validator, it is recommended to reflect the allowed content types in the html [`accept`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept) attribute in the corresponding file field in your views. This will prevent users from trying to upload files with not allowed content types (however it is only an UX improvement, a malicious user can still try to upload files with not allowed content types therefore the backend validation).\n\nFor example, if you want to allow PNG and JPEG images only, you can do this:\n```ruby\nclass User \u003c ApplicationRecord\n  ACCEPTED_CONTENT_TYPES = ['image/png', 'image/jpeg'].freeze\n\n  has_one_attached :avatar\n\n  validates :avatar, content_type: ACCEPTED_CONTENT_TYPES\nend\n```\n\n```erb\n\u003c%= form_with model: @user do |f| %\u003e\n  \u003c%= f.file_field :avatar,\n                   accept: ACCEPTED_CONTENT_TYPES.join(',') %\u003e\n\u003c% end %\u003e\n```\n\n#### Content type shorthands\n\nIf you choose to use a content_type 'shorthand' (like `png`), note that it will be converted to a full content type using `Marcel::MimeType.for` under the hood. Therefore, you should check if the content_type is registered by [`Marcel::EXTENSIONS`](https://github.com/rails/marcel/blob/main/lib/marcel/tables.rb). If it's not, you can register it by adding the following code to your `config/initializers/mime_types.rb` file:\n\n```ruby\nMarcel::MimeType.extend \"application/ino\", extensions: %w(ino), parents: \"text/plain\" # Registering arduino INO files\n```\n\nBe sure to at least include one the `extensions`, `parents` or `magic` option, otherwise the content type will not be registered.\n\n#### Content type spoofing protection\n\nBy default, the gem does not prevent content type spoofing. You can enable it by setting the `spoofing_protection` option to `true` in your validator options.\n\n\u003cdetails\u003e\n\u003csummary\u003e\nWhat is content type spoofing?\n\u003c/summary\u003e\n\nFile content type spoofing happens when an ill-intentioned user uploads a file which hides its true content type by faking its extension and its declared content type value. For example, a user may try to upload a `.exe` file (application/x-msdownload content type) dissimulated as a `.jpg` file (image/jpeg content type).\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nHow do we prevent it?\n\u003c/summary\u003e\n\nThe spoofing protection relies on both the UNIX `file` command and `Marcel` gem. Be careful, since it needs to load the whole file io to perform the analysis, it will use a lot of RAM for very large files. Therefore it could be a wise decision not to enable it in this case.\n\nTake note that the `file` analyzer will not find the exactly same content type as the ActiveStorage blob (ActiveStorage content type detection relies on a different logic using first 4kb of content + filename + extension). To handle this issue, we consider a close parent content type to be a match. For example, for an ActiveStorage blob which content type is `video/x-ms-wmv`, the `file` analyzer will probably detect a `video/x-ms-asf` content type, this will be considered as a valid match because these 2 content types are closely related. The correlation mapping is based on `Marcel::TYPE_PARENTS` table.\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nEdge cases\n\u003c/summary\u003e\n\nThe difficulty to accurately predict a mime type may generate false positives, if so there are two solutions available:\n- If the ActiveStorage blob content type is closely related to the detected content type using the `file` analyzer, you can enhance `Marcel::TYPE_PARENTS` mapping using `Marcel::MimeType.extend \"application/x-rar-compressed\", parents: %(application/x-rar)` in the `config/initializers/mime_types.rb` file. (Please drop an issue so we can add it to the gem for everyone!)\n- If the ActiveStorage blob content type is not closely related, you still can disable the content type spoofing protection in the validator, if so, please drop us an issue so we can fix it for everyone!\n\u003c/details\u003e\n\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      content_type_invalid:\n        one: \"has an invalid content type (authorized content type is %{authorized_human_content_types})\"\n        other: \"has an invalid content type (authorized content types are %{authorized_human_content_types})\"\n      content_type_spoofed:\n        one: \"has a content type that is not equivalent to the one that is detected through its content (authorized content type is %{authorized_human_content_types})\"\n        other: \"has a content type that is not equivalent to the one that is detected through its content (authorized content types are %{authorized_human_content_types})\"\n```\n\nThe `content_type` validator error messages expose 7 values that you can use:\n- `content_type` containing the content type of the sent file (e.g. `image/png`)\n- `human_content_type` containing a more user-friendly version of the sent file content type (e.g. 'TXT' for 'text/plain')\n- `detected_content_type` containing the detected content type of the sent file using `spoofing_protection` option (e.g. `image/png`)\n- `detected_human_content_type` containing a more user-friendly version of the sent file detected content type using `spoofing_protection` option (e.g. 'TXT' for 'text/plain')\n- `authorized_human_content_types` containing the list of authorized content types (e.g. 'PNG, JPEG' for `['image/png', 'image/jpeg']`)\n- `count` containing the number of authorized content types (e.g. `2`)\n- `filename` containing the filename\n\n---\n\n### Size\n\nValidates each attached file size.\n\n#### Options\n\nThe `size` validator has 5 possible options:\n- `less_than`: defines the strict maximum allowed file size\n- `less_than_or_equal_to`: defines the maximum allowed file size\n- `greater_than`: defines the strict minimum allowed file size\n- `greater_than_or_equal_to`: defines the minimum allowed file size\n- `between`: defines the allowed file size range\n- `equal_to`: defines the allowed file size\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_one_attached :avatar\n\n  validates :avatar, size: { less_than: 2.megabytes } # restricts the file size to \u003c 2MB\n  validates :avatar, size: { less_than_or_equal_to: 2.megabytes } # restricts the file size to \u003c= 2MB\n  validates :avatar, size: { greater_than: 1.kilobyte } # restricts the file size to \u003e 1KB\n  validates :avatar, size: { greater_than_or_equal_to: 1.kilobyte } # restricts the file size to \u003e= 1KB\n  validates :avatar, size: { between: 1.kilobyte..2.megabytes } # restricts the file size to between 1KB and 2MB\n  validates :avatar, size: { equal_to: 1.megabyte } # restricts the file size to exactly 1MB\nend\n```\n\n#### Best practices\n\nIt is always a good practice to limit the maximum file size to a reasonable value (like 2MB for avatar images). This helps prevent server storage issues, reduces upload/download times, and ensures better performance. Large files can consume excessive bandwidth and storage space, potentially impacting both server resources and user experience.\nPlus, not setting a size limit inside your Rails app might lead into your server throwing a `413 Content Too Large` error, which is not as nice as a Rails validation error.\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      file_size_not_less_than: \"file size must be less than %{max} (current size is %{file_size})\"\n      file_size_not_less_than_or_equal_to: \"file size must be less than or equal to %{max} (current size is %{file_size})\"\n      file_size_not_greater_than: \"file size must be greater than %{min} (current size is %{file_size})\"\n      file_size_not_greater_than_or_equal_to: \"file size must be greater than or equal to %{min} (current size is %{file_size})\"\n      file_size_not_between: \"file size must be between %{min} and %{max} (current size is %{file_size})\"\n      file_size_not_equal_to: \"file size must be equal to %{exact} (current size is %{file_size})\"\n```\n\nThe `size` validator error messages expose 4 values that you can use:\n- `file_size` containing the current file size (e.g. `1.5MB`)\n- `min` containing the minimum allowed file size (e.g. `1KB`)\n- `exact` containing the allowed file size (e.g. `1MB`)\n- `max` containing the maximum allowed file size (e.g. `2MB`)\n- `filename` containing the current file name\n\n---\n\n### Total size\n\nValidates the total file size for several files.\n\n#### Options\n\nThe `total_size` validator has 5 possible options:\n- `less_than`: defines the strict maximum allowed total file size\n- `less_than_or_equal_to`: defines the maximum allowed total file size\n- `greater_than`: defines the strict minimum allowed total file size\n- `greater_than_or_equal_to`: defines the minimum allowed total file size\n- `between`: defines the allowed total file size range\n- `equal_to`: defines the allowed total file size\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_many_attached :certificates\n\n  validates :certificates, total_size: { less_than: 10.megabytes } # restricts the total size to \u003c 10MB\n  validates :certificates, total_size: { less_than_or_equal_to: 10.megabytes } # restricts the total size to \u003c= 10MB\n  validates :certificates, total_size: { greater_than: 1.kilobyte } # restricts the total size to \u003e 1KB\n  validates :certificates, total_size: { greater_than_or_equal_to: 1.kilobyte } # restricts the total size to \u003e= 1KB\n  validates :certificates, total_size: { between: 1.kilobyte..10.megabytes } # restricts the total size to between 1KB and 10MB\n  validates :certificates, total_size: { equal_to: 1.megabyte } # restricts the total file size to exactly 1MB\nend\n```\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      total_file_size_not_less_than: \"total file size must be less than %{max} (current size is %{total_file_size})\"\n      total_file_size_not_less_than_or_equal_to: \"total file size must be less than or equal to %{max} (current size is %{total_file_size})\"\n      total_file_size_not_greater_than: \"total file size must be greater than %{min} (current size is %{total_file_size})\"\n      total_file_size_not_greater_than_or_equal_to: \"total file size must be greater than or equal to %{min} (current size is %{total_file_size})\"\n      total_file_size_not_between: \"total file size must be between %{min} and %{max} (current size is %{total_file_size})\"\n      total_file_size_not_equal_to: \"total file size must be equal to %{exact} (current size is %{total_file_size})\"\n```\n\nThe `total_size` validator error messages expose 4 values that you can use:\n- `total_file_size` containing the current total file size (e.g. `1.5MB`)\n- `min` containing the minimum allowed total file size (e.g. `1KB`)\n- `exact` containing the allowed total file size (e.g. `1MB`)\n- `max` containing the maximum allowed total file size (e.g. `2MB`)\n\n---\n\n### Dimension\n\nValidates the dimension of the attached image / video files.\nIt can also be used for pdf files, but it will only analyze the pdf first page, and will assume a DPI of 72.\n(be sure to have the right dependencies installed as mentioned in [installation](#installation))\n\n#### Options\n\nThe `dimension` validator has several possible options:\n- `width`: defines the allowed width (integer)\n  - `min`: defines the minimum allowed width (integer)\n  - `max`: defines the maximum allowed width (integer)\n  - `in`: defines the allowed width range (range)\n- `height`: defines the allowed height (integer)\n  - `min`: defines the minimum allowed height (integer)\n  - `max`: defines the maximum allowed height (integer)\n  - `in`: defines the allowed height range (range)\n- `min`: defines the minimum allowed width and height (range)\n- `max`: defines the maximum allowed width and height (range)\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_one_attached :avatar\n\n  validates :avatar, dimension: { width: 100 } # restricts the width to 100 pixels\n  validates :avatar, dimension: { width: { min: 80, max: 100 } } # restricts the width to between 80 and 100 pixels\n  validates :avatar, dimension: { width: { in: 80..100 } } # restricts the width to between 80 and 100 pixels\n  validates :avatar, dimension: { height: 100 } # restricts the height to 100 pixels\n  validates :avatar, dimension: { height: { min: 600, max: 1800 } } # restricts the height to between 600 and 1800 pixels\n  validates :avatar, dimension: { height: { in: 600..1800 } } # restricts the height to between 600 and 1800 pixels\n  validates :avatar, dimension: { min: 80..600, max: 100..1800 } # restricts the width to between 80 and 100 pixels, and the height to between 600 and 1800 pixels\nend\n```\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      dimension_min_not_included_in: \"must be greater than or equal to %{width} x %{height} pixels\"\n      dimension_max_not_included_in: \"must be less than or equal to %{width} x %{height} pixels\"\n      dimension_width_not_included_in: \"width is not included between %{min} and %{max} pixels\"\n      dimension_height_not_included_in: \"height is not included between %{min} and %{max} pixels\"\n      dimension_width_not_greater_than_or_equal_to: \"width must be greater than or equal to %{length} pixels\"\n      dimension_height_not_greater_than_or_equal_to: \"height must be greater than or equal to %{length} pixels\"\n      dimension_width_not_less_than_or_equal_to: \"width must be less than or equal to %{length} pixels\"\n      dimension_height_not_less_than_or_equal_to: \"height must be less than or equal to %{length} pixels\"\n      dimension_width_not_equal_to: \"width must be equal to %{length} pixels\"\n      dimension_height_not_equal_to: \"height must be equal to %{length} pixels\"\n      media_metadata_missing: \"is not a valid media file\"\n```\n\nThe `dimension` validator error messages expose 6 values that you can use:\n- `min` containing the minimum width or height allowed\n- `max` containing the maximum width or height allowed\n- `width` containing the minimum or maximum width allowed\n- `height` containing the minimum or maximum width allowed\n- `length` containing the exact width or height allowed\n- `filename` containing the current filename in error\n\n---\n\n### Duration\n\nValidates the duration of the attached audio / video files.\n(be sure to have the right dependencies installed as mentioned in [installation](#installation))\n\n#### Options\n\nThe `duration` validator has 5 possible options:\n- `less_than`: defines the strict maximum allowed file duration\n- `less_than_or_equal_to`: defines the maximum allowed file duration\n- `greater_than`: defines the strict minimum allowed file duration\n- `greater_than_or_equal_to`: defines the minimum allowed file duration\n- `between`: defines the allowed file duration range\n- `equal_to`: defines the allowed duration\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_one_attached :intro_song\n\n  validates :intro_song, duration: { less_than: 2.minutes } # restricts the file duration to \u003c 2 minutes\n  validates :intro_song, duration: { less_than_or_equal_to: 2.minutes } # restricts the file duration to \u003c= 2 minutes\n  validates :intro_song, duration: { greater_than: 1.second } # restricts the file duration to \u003e 1 second\n  validates :intro_song, duration: { greater_than_or_equal_to: 1.second } # restricts the file duration to \u003e= 1 second\n  validates :intro_song, duration: { between: 1.second..2.minutes } # restricts the file duration to between 1 second and 2 minutes\n  validates :intro_song, duration: { equal_to: 1.minute } # restricts the duration to exactly 1 minute\nend\n```\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      duration_not_less_than: \"duration must be less than %{max} (current duration is %{duration})\"\n      duration_not_less_than_or_equal_to: \"duration must be less than or equal to %{max} (current duration is %{duration})\"\n      duration_not_greater_than: \"duration must be greater than %{min} (current duration is %{duration})\"\n      duration_not_greater_than_or_equal_to: \"duration must be greater than or equal to %{min} (current duration is %{duration})\"\n      duration_not_between: \"duration must be between %{min} and %{max} (current duration is %{duration})\"\n      duration_not_equal_to: \"duration must be equal to %{exact} (current duration is %{duration})\"\n```\n\nThe `duration` validator error messages expose 4 values that you can use:\n- `duration` containing the current duration size (e.g. `2 minutes`)\n- `min` containing the minimum allowed duration size (e.g. `1 second`)\n- `exact` containing the allowed duration (e.g. `3 seconds`)\n- `max` containing the maximum allowed duration size (e.g. `2 minutes`)\n- `filename` containing the current file name\n\n---\n\n### Aspect ratio\n\nValidates the aspect ratio of the attached image / video files.\nIt can also be used for pdf files, but it will only analyze the pdf first page.\n(be sure to have the right dependencies installed as mentioned in [installation](#installation))\n\n#### Options\n\nThe `aspect_ratio` validator has several options:\n- `with`: defines the allowed aspect ratio (e.g. `:is_16/9`)\n- `in`: defines the allowed aspect ratios (e.g. `%i[square landscape]`)\n\nThis validator can define aspect ratios in several ways:\n- Symbols:\n  - prebuilt aspect ratios: `:square`, `:portrait`, `:landscape`\n  - custom aspect ratios (it must be of type `is_xx_yy`): `:is_16_9`, `:is_4_3`, etc.\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_one_attached :avatar\n\n  validates :avatar, aspect_ratio: :square # restricts the aspect ratio to 1:1\n  validates :avatar, aspect_ratio: :portrait # restricts the aspect ratio to x:y where y \u003e x\n  validates :avatar, aspect_ratio: :landscape # restricts the aspect ratio to x:y where x \u003e y\n  validates :avatar, aspect_ratio: :is_16_9 # restricts the aspect ratio to 16:9\n  validates :avatar, aspect_ratio: %i[square is_16_9] # restricts the aspect ratio to 1:1 and 16:9\nend\n```\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      aspect_ratio_not_square: \"must be square (current file is %{width}x%{height}px)\"\n      aspect_ratio_not_portrait: \"must be portrait (current file is %{width}x%{height}px)\"\n      aspect_ratio_not_landscape: \"must be landscape (current file is %{width}x%{height}px)\"\n      aspect_ratio_not_x_y: \"must be %{authorized_aspect_ratios} (current file is %{width}x%{height}px)\"\n      aspect_ratio_invalid: \"has an invalid aspect ratio (valid aspect ratios are %{authorized_aspect_ratios})\"\n      media_metadata_missing: \"is not a valid media file\"\n```\n\nThe `aspect_ratio` validator error messages expose 4 values that you can use:\n- `authorized_aspect_ratios` containing the authorized aspect ratios\n- `width` containing the current width of the image/video\n- `height` containing the current height of the image/video\n- `filename` containing the current filename in error\n\n---\n\n### Processable file\n\nValidates if the attached files can be processed by MiniMagick or Vips (image), ffmpeg (video/audio) or poppler (pdf).\n(be sure to have the right dependencies installed as mentioned in [installation](#installation))\n\n#### Options\n\nThe `processable_file` validator has no options.\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_one_attached :avatar\n\n  validates :avatar, processable_file: true # ensures that the file is processable by MiniMagick or Vips (image) or ffmpeg (video/audio)\nend\n```\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      file_not_processable: \"is not identified as a valid media file\"\n```\n\nThe `processable_file` validator error messages expose 1 value that you can use:\n- `filename` containing the current filename in error\n\n---\n\n### Pages\n\nValidates each attached pdf file number of pages.\n(be sure to have the right dependencies installed as mentioned in [installation](#installation))\n\n#### Options\n\nThe `pages` validator has 6 possible options:\n- `less_than`: defines the strict maximum allowed number of pages\n- `less_than_or_equal_to`: defines the maximum allowed number of pages\n- `greater_than`: defines the strict minimum allowed number of pages\n- `greater_than_or_equal_to`: defines the minimum allowed number of pages\n- `between`: defines the allowed number of pages range\n- `equal_to`: defines the allowed number of pages\n\n#### Examples\n\nUse it like this:\n```ruby\nclass User \u003c ApplicationRecord\n  has_one_attached :contract\n\n  validates :contract, pages: { less_than: 2 } # restricts the number of pages to \u003c 2\n  validates :contract, pages: { less_than_or_equal_to: 2 } # restricts the number of pages to \u003c= 2\n  validates :contract, pages: { greater_than: 1 } # restricts the number of pages to \u003e 1\n  validates :contract, pages: { greater_than_or_equal_to: 1 } # restricts the number of pages to \u003e= 1\n  validates :contract, pages: { between: 1..2 } # restricts the number of pages to between 1 and 2\n  validates :contract, pages: { equal_to: 1 } # restricts the number of pages to exactly 1\nend\n```\n\n#### Error messages (I18n)\n\n```yml\nen:\n  errors:\n    messages:\n      pages_not_less_than: \"page count must be less than %{max} (current page count is %{pages})\"\n      pages_not_less_than_or_equal_to: \"page count must be less than or equal to %{max} (current page count is %{pages})\"\n      pages_not_greater_than: \"page count must be greater than %{min} (current page count is %{pages})\"\n      pages_not_greater_than_or_equal_to: \"page count must be greater than or equal to %{min} (current page count is %{pages})\"\n      pages_not_between: \"page count must be between %{min} and %{max} (current page count is %{pages})\"\n      pages_not_equal_to: \"page count must be equal to %{exact} (current page count is %{pages})\"\n```\n\nThe `pages` validator error messages expose 5 values that you can use:\n- `pages` containing the current file number of pages (e.g. `7`)\n- `min` containing the minimum allowed number of pages (e.g. `1`)\n- `exact` containing the allowed number of pages (e.g. `3`)\n- `max` containing the maximum allowed number of pages (e.g. `5`)\n- `filename` containing the current file name\n\n---\n\n## Upgrading from 1.x to 2.x\n\nIf you are upgrading from 1.x to 2.x, you will be pleased to note that a lot of things have been added and improved!\n\nAdded features:\n- `duration` validator has been added for audio / video files\n- `dimension` validator now supports videos\n- `aspect_ratio` validator now supports videos\n- `processable_image` validator is now `processable_file` validator and supports image/video/audio\n- Major performance improvement have been added: we now only perform the expensive io analysis operation on the newly attached files. For previously attached files, we validate them using Rails `ActiveStorage::Blob#metadata` internal mecanism ([more here](https://github.com/rails/rails/blob/main/activestorage/app/models/active_storage/blob/analyzable.rb)).\n- All error messages have been given an upgrade and new variables that you can use\n\nBut this major version bump also comes with some breaking changes. Below are the main breaking changes you need to be aware of:\n- Error messages\n  - We advise you to replace all the v1 translations by the new v2 rather than changing them one by one. A majority of messages have been completely rewritten to be more consistent and easier to understand.\n  - If you wish to change them one by one, here is the list of changes to make:\n    - Some validator errors have been totally changed:\n      - `limit` validator keys have been totally reworked\n      - `dimension` validator keys have been totally reworked\n      - `content_type` validator keys have been totally reworked\n      - `processable_image` validator keys have been totally reworked\n    - Some keys have been changed:\n      - `image_metadata_missing` has been replaced by `media_metadata_missing`\n      - `aspect_ratio_is_not` has been replaced by `aspect_ratio_not_x_y`\n    - Some error messages variables names have been changed to improve readability:\n      - `aspect_ratio` validator:\n        - `aspect_ratio` has been replaced by `authorized_aspect_ratios`\n      - `content_type` validator:\n        - `authorized_types` has been replaced by `authorized_human_content_types`\n      - `size` validator:\n        - `min_size` has been replaced by `min`\n        - `max_size` has been replaced by `max`\n      - `total_size` validator:\n        - `min_size` has been replaced by `min`\n        - `max_size` has been replaced by `max`\n\n- `content_type` validator\n  - The `:in` option now only accepts 'valid' content types (ie content types deemed by Marcel as valid).\n    - The check was mistakenly only performed on the `:with` option previously. Therefore, invalid content types were accepted in the `:in` option, which is not the expected behavior.\n    - This might break some cases when you had for example `content_type: ['image/png', 'image/jpg']`, because `image/jpg` is not a valid content type, it should be replaced by `image/jpeg`.\n  - An `ArgumentError` is now raised if `image/jpg` is used to make it easier to fix. You should now only use `image/jpeg`.\n\n- `processable_image` validator\n  - The validator has been replaced by `processable_file` validator, be sure to replace `processable_image: true` to `processable_file: true`\n  - The associated matcher has also been updated accordingly, be sure to replace `validate_processable_image_of` to `validate_processable_file_of`\n\n## Upgrading from 2.x to 3.x\n\nVersion 3 comes with the ability to support single page pdf `dimension` / `aspect_ratio` analysis, we had to make a breaking change:\n- To analyze PDFs, you must install the `poppler` PDF processing dependency\n  - It's a  Rails-supported PDF processing dependency (https://guides.rubyonrails.org/active_storage_overview.html#requirements)\n  - To install it, check their documentation at this [link](https://pdf2image.readthedocs.io/en/latest/installation.html).\n  - To check if it's installed, execute `pdftoppm -h`.\n  - To install this tool in your CI / production environments, you can check how we do it in our own CI (https://github.com/igorkasyanchuk/active_storage_validations/blob/master/.github/workflows/main.yml)\n\nWe also added the `pages` validator to validate pdf number of pages, and the `equal_to` option to `duration`, `size` and `total_size` validators.\n\nNote that, if you do not perform these metadata validations on pdfs, the gem will work the same as in version 2.\n\n## Internationalization (I18n)\n\nActive Storage Validations uses I18n for error messages. The error messages are automatically loaded in your Rails app if your language translations are present in the gem.\n\nTranslation files are available [here](https://github.com/igorkasyanchuk/active_storage_validations/tree/master/config/locales). We currently have translations for `da`, `de`, `en`, `en-GB`, `es`, `fr`, `it`, `ja`, `nl`, `pl`, `pt-BR`, `ru`, `sv`, `tr`, `uk`, `vi` and `zh-CN`. Feel free to drop a PR to add your language ✌️.\n\nIf you wish to customize the error messages, just copy, paste and update the translation files into your application locales.\n\n## Test matchers\n\nThe gem also provides RSpec-compatible and Minitest-compatible matchers for testing the validators.\n\n### RSpec\n\n#### Setup\nIn `spec_helper.rb`, you'll need to require the matchers:\n\n```ruby\nrequire 'active_storage_validations/matchers'\n```\n\nAnd include the module:\n\n```ruby\nRSpec.configure do |config|\n  config.include ActiveStorageValidations::Matchers\nend\n```\n\n#### Matchers\nMatcher methods available:\n\n```ruby\ndescribe User do\n  # aspect_ratio:\n  # #allowing, #rejecting\n  it { is_expected.to validate_aspect_ratio_of(:avatar).allowing(:square, :portrait) } # possible to use an Array or *splatted array\n  it { is_expected.to validate_aspect_ratio_of(:avatar).rejecting(:square, :landscape) } # possible to use an Array or *splatted array\n\n  # attached\n  it { is_expected.to validate_attached_of(:avatar) }\n\n  # processable_file\n  it { is_expected.to validate_processable_file_of(:avatar) }\n\n  # limit\n  # #min, #max\n  it { is_expected.to validate_limits_of(:avatar).min(1) }\n  it { is_expected.to validate_limits_of(:avatar).max(5) }\n\n  # content_type:\n  # #allowing, #rejecting\n  it { is_expected.to validate_content_type_of(:avatar).allowing('image/png', 'image/gif') } # possible to use an Array or *splatted array\n  it { is_expected.to validate_content_type_of(:avatar).rejecting('text/plain', 'text/xml') } # possible to use an Array or *splatted array\n\n  # dimension:\n  # #width, #height, #width_min, #height_min, #width_max, #height_max, #width_between, #height_between\n  it { is_expected.to validate_dimensions_of(:avatar).width(250) }\n  it { is_expected.to validate_dimensions_of(:avatar).height(200) }\n  it { is_expected.to validate_dimensions_of(:avatar).width_min(200) }\n  it { is_expected.to validate_dimensions_of(:avatar).height_min(100) }\n  it { is_expected.to validate_dimensions_of(:avatar).width_max(500) }\n  it { is_expected.to validate_dimensions_of(:avatar).height_max(300) }\n  it { is_expected.to validate_dimensions_of(:avatar).width_between(200..500) }\n  it { is_expected.to validate_dimensions_of(:avatar).height_between(100..300) }\n\n  # size:\n  # #less_than, #less_than_or_equal_to, #greater_than, #greater_than_or_equal_to, #between, #equal_to\n  it { is_expected.to validate_size_of(:avatar).less_than(50.kilobytes) }\n  it { is_expected.to validate_size_of(:avatar).less_than_or_equal_to(50.kilobytes) }\n  it { is_expected.to validate_size_of(:avatar).greater_than(1.kilobyte) }\n  it { is_expected.to validate_size_of(:avatar).greater_than_or_equal_to(1.kilobyte) }\n  it { is_expected.to validate_size_of(:avatar).between(100..500.kilobytes) }\n  it { is_expected.to validate_size_of(:avatar).equal_to(5.megabytes) }\n\n  # total_size:\n  # #less_than, #less_than_or_equal_to, #greater_than, #greater_than_or_equal_to, #between, #equal_to\n  it { is_expected.to validate_total_size_of(:avatar).less_than(50.kilobytes) }\n  it { is_expected.to validate_total_size_of(:avatar).less_than_or_equal_to(50.kilobytes) }\n  it { is_expected.to validate_total_size_of(:avatar).greater_than(1.kilobyte) }\n  it { is_expected.to validate_total_size_of(:avatar).greater_than_or_equal_to(1.kilobyte) }\n  it { is_expected.to validate_total_size_of(:avatar).between(100..500.kilobytes) }\n  it { is_expected.to validate_total_size_of(:avatar).equal_to(5.megabytes) }\n\n  # duration:\n  # #less_than, #less_than_or_equal_to, #greater_than, #greater_than_or_equal_to, #between, #equal_to\n  it { is_expected.to validate_duration_of(:introduction).less_than(50.seconds) }\n  it { is_expected.to validate_duration_of(:introduction).less_than_or_equal_to(50.seconds) }\n  it { is_expected.to validate_duration_of(:introduction).greater_than(1.minute) }\n  it { is_expected.to validate_duration_of(:introduction).greater_than_or_equal_to(1.minute) }\n  it { is_expected.to validate_duration_of(:introduction).between(100..500.seconds) }\n  it { is_expected.to validate_duration_of(:avatar).equal_to(5.minutes) }\n\n  # pages:\n  # #less_than, #less_than_or_equal_to, #greater_than, #greater_than_or_equal_to, #between, #equal_to\n  it { is_expected.to validate_pages_of(:contract).less_than(50) }\n  it { is_expected.to validate_pages_of(:contract).less_than_or_equal_to(50) }\n  it { is_expected.to validate_pages_of(:contract).greater_than(5) }\n  it { is_expected.to validate_pages_of(:contract).greater_than_or_equal_to(5) }\n  it { is_expected.to validate_pages_of(:contract).between(100..500) }\n  it { is_expected.to validate_pages_of(:contract).equal_to(5) }\nend\n```\n(Note that matcher methods are chainable)\n\nAll matchers can currently be customized with Rails validation options:\n\n```ruby\ndescribe User do\n  # :allow_blank\n  it { is_expected.to validate_attached_of(:avatar).allow_blank }\n\n  # :on\n  it { is_expected.to validate_attached_of(:avatar).on(:update) }\n  it { is_expected.to validate_attached_of(:avatar).on(%i[update custom]) }\n\n  # :message\n  it { is_expected.to validate_dimensions_of(:avatar).width(250).with_message('Invalid dimensions.') }\nend\n```\n\n### Minitest\n\n#### Setup\nTo use the matchers, make sure you have the [shoulda-context](https://github.com/thoughtbot/shoulda-context) gem up and running.\n\nYou need to require the matchers:\n\n```ruby\nrequire 'active_storage_validations/matchers'\n```\n\nAnd extend the module:\n\n```ruby\nclass ActiveSupport::TestCase\n  extend ActiveStorageValidations::Matchers\nend\n```\n\n#### Matchers\nThen you can use the matchers with the syntax specified in the RSpec section, just use `should validate_method` instead of `it { is_expected_to validate_method }` as specified in the [shoulda-context](https://github.com/thoughtbot/shoulda-context) gem.\n\n\n## Contributing\n\nIf you want to contribute to the project, you will have to fork the repository and create a new branch from the `master` branch. Then build your feature, or fix the issue, and create a pull request. Be sure to add tests for your changes.\n\nBefore submitting your pull request, run the tests to make sure everything works as expected.\n\nTo run the gem tests, launch the following commands in the root folder of gem repository:\n\n* `BUNDLE_GEMFILE=gemfiles/rails_6_1_4.gemfile bundle exec rake test` to run for Rails 6.1.4\n* `BUNDLE_GEMFILE=gemfiles/rails_7_0.gemfile bundle exec rake test` to run for Rails 7.0\n* `BUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bundle exec rake test` to run for Rails 7.1\n* `BUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle exec rake test` to run for Rails 7.2\n* `BUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle exec rake test` to run for Rails 8.0\n* `BUNDLE_GEMFILE=gemfiles/rails_8_1.gemfile bundle exec rake test` to run for Rails 8.1\n* `BUNDLE_GEMFILE=gemfiles/rails_next.gemfile bundle exec rake test` to run for Rails main\n\nSnippet to run in console:\n\n```bash\nBUNDLE_GEMFILE=gemfiles/rails_6_1_4.gemfile bundle\nBUNDLE_GEMFILE=gemfiles/rails_7_0.gemfile bundle\nBUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bundle\nBUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle\nBUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle\nBUNDLE_GEMFILE=gemfiles/rails_8_1.gemfile bundle\nBUNDLE_GEMFILE=gemfiles/rails_next.gemfile bundle\nBUNDLE_GEMFILE=gemfiles/rails_6_1_4.gemfile bundle exec rake test\nBUNDLE_GEMFILE=gemfiles/rails_7_0.gemfile bundle exec rake test\nBUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bundle exec rake test\nBUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle exec rake test\nBUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle exec rake test\nBUNDLE_GEMFILE=gemfiles/rails_8_1.gemfile bundle exec rake test\nBUNDLE_GEMFILE=gemfiles/rails_next.gemfile bundle exec rake test\n```\n\nTips:\n- To focus a specific test, use the `focus` class method provided by [minitest-focus](https://github.com/minitest/minitest-focus)\n- To focus a specific file, use the TEST option provided by minitest, e.g. to only run `size_validator_test.rb` file you will launch the following command: `bundle exec rake test TEST=test/validators/size_validator_test.rb`\n\n\n## Additional information\n\n### Contributors (BIG THANK YOU!)\n\nWe have a long list of valued contributors. Check them all at:\n\nhttps://github.com/igorkasyanchuk/active_storage_validations/graphs/contributors\n\n### License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n\u003cbr\u003e\n\n[\u003cimg src=\"https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true\"\n/\u003e](https://www.railsjazz.com/?utm_source=github\u0026utm_medium=bottom\u0026utm_campaign=active_storage_validations)\n\n[![\"Buy Me A Coffee\"](https://github.com/igorkasyanchuk/get-smart/blob/main/docs/snapshot-bmc-button.png?raw=true)](https://buymeacoffee.com/igorkasyanchuk)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figorkasyanchuk%2Factive_storage_validations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Figorkasyanchuk%2Factive_storage_validations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figorkasyanchuk%2Factive_storage_validations/lists"}