Skip to content

Commit

Permalink
[Validator/Matcher] Add new total_size validator and associated mat…
Browse files Browse the repository at this point in the history
…cher (#113)
  • Loading branch information
Mth0158 committed Jul 5, 2024
1 parent ab27760 commit f9a573e
Show file tree
Hide file tree
Showing 64 changed files with 1,290 additions and 257 deletions.
50 changes: 38 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This gems doing it for you. Just use `attached: true` or `content_type: 'image/p
* validates if file(s) attached
* validates content type
* validates size of files
* validates total size of files
* validates dimension of images/videos
* validates number of uploaded files (min/max required)
* validates aspect ratio (if square, portrait, landscape, is_16_9, ...)
Expand Down Expand Up @@ -62,7 +63,7 @@ class Project < ApplicationRecord
validates :logo, attached: true, size: { less_than: 100.megabytes , message: 'is too large' }
validates :preview, attached: true, size: { between: 1.kilobyte..100.megabytes , message: 'is not given between size' }
validates :attachment, attached: true, content_type: { in: 'application/pdf', message: 'is not a PDF' }
validates :documents, limit: { min: 1, max: 3 }
validates :documents, limit: { min: 1, max: 3 }, total_size: { less_than: 5.megabytes }
end
```

Expand Down Expand Up @@ -159,18 +160,23 @@ en:
file_size_not_greater_than: "file size must be greater than %{min_size} (current size is %{file_size})"
file_size_not_greater_than_or_equal_to: "file size must be greater than or equal to %{min_size} (current size is %{file_size})"
file_size_not_between: "file size must be between %{min_size} and %{max_size} (current size is %{file_size})"
total_file_size_not_less_than: "total file size must be less than %{max_size} (current size is %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "total file size must be less than or equal to %{max_size} (current size is %{total_file_size})"
total_file_size_not_greater_than: "total file size must be greater than %{min_size} (current size is %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "total file size must be greater than or equal to %{min_size} (current size is %{total_file_size})"
total_file_size_not_between: "total file size must be between %{min_size} and %{max_size} (current size is %{total_file_size})"
limit_out_of_range: "total number is out of range"
image_metadata_missing: "is not a valid image"
dimension_min_inclusion: "must be greater than or equal to %{width} x %{height} pixel."
dimension_max_inclusion: "must be less than or equal to %{width} x %{height} pixel."
dimension_width_inclusion: "width is not included between %{min} and %{max} pixel."
dimension_height_inclusion: "height is not included between %{min} and %{max} pixel."
dimension_width_greater_than_or_equal_to: "width must be greater than or equal to %{length} pixel."
dimension_height_greater_than_or_equal_to: "height must be greater than or equal to %{length} pixel."
dimension_width_less_than_or_equal_to: "width must be less than or equal to %{length} pixel."
dimension_height_less_than_or_equal_to: "height must be less than or equal to %{length} pixel."
dimension_width_equal_to: "width must be equal to %{length} pixel."
dimension_height_equal_to: "height must be equal to %{length} pixel."
dimension_min_inclusion: "must be greater than or equal to %{width} x %{height} pixel"
dimension_max_inclusion: "must be less than or equal to %{width} x %{height} pixel"
dimension_width_inclusion: "width is not included between %{min} and %{max} pixel"
dimension_height_inclusion: "height is not included between %{min} and %{max} pixel"
dimension_width_greater_than_or_equal_to: "width must be greater than or equal to %{length} pixel"
dimension_height_greater_than_or_equal_to: "height must be greater than or equal to %{length} pixel"
dimension_width_less_than_or_equal_to: "width must be less than or equal to %{length} pixel"
dimension_height_less_than_or_equal_to: "height must be less than or equal to %{length} pixel"
dimension_width_equal_to: "width must be equal to %{length} pixel"
dimension_height_equal_to: "height must be equal to %{length} pixel"
aspect_ratio_not_square: "must be a square image"
aspect_ratio_not_portrait: "must be a portrait image"
aspect_ratio_not_landscape: "must be a landscape image"
Expand Down Expand Up @@ -232,6 +238,18 @@ For example :
file_size_not_between: "file size must be between %{min_size} and %{max_size} (current size is %{file_size})"
```

### Total file size
The keys starting with `total_file_size_not_` support three variables that you can use:
- `total_file_size` containing the current total file size
- `min` containing the minimum file size
- `max` containing the maximum file size

For example :

```yml
total_file_size_not_between: "total file size must be between %{min_size} and %{max_size} (current size is %{total_file_size})"
```

### Number of files
The `limit_out_of_range` key supports two variables that you can use:
- `min` containing the minimum number of files
Expand Down Expand Up @@ -280,7 +298,7 @@ Very simple example of validation with file attached, content type check and cus
[![Sample](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)

## Test matchers
Provides RSpec-compatible and Minitest-compatible matchers for testing the validators. Only `aspect_ratio`, `attached`, `content_type`, `dimension` and `size` validators currently have their matcher developed.
Provides RSpec-compatible and Minitest-compatible matchers for testing the validators. Only `aspect_ratio`, `attached`, `content_type`, `dimension`, `size` and `total_size` validators currently have their matcher developed.

### RSpec

Expand Down Expand Up @@ -333,6 +351,14 @@ describe User do
it { is_expected.to validate_size_of(:avatar).greater_than(1.kilobyte) }
it { is_expected.to validate_size_of(:avatar).greater_than_or_equal_to(1.kilobyte) }
it { is_expected.to validate_size_of(:avatar).between(100..500.kilobytes) }
# total_size:
# #less_than, #less_than_or_equal_to, #greater_than, #greater_than_or_equal_to, #between
it { is_expected.to validate_total_size_of(:avatar).less_than(50.kilobytes) }
it { is_expected.to validate_total_size_of(:avatar).less_than_or_equal_to(50.kilobytes) }
it { is_expected.to validate_total_size_of(:avatar).greater_than(1.kilobyte) }
it { is_expected.to validate_total_size_of(:avatar).greater_than_or_equal_to(1.kilobyte) }
it { is_expected.to validate_total_size_of(:avatar).between(100..500.kilobytes) }
end
```
(Note that matcher methods are chainable)
Expand Down
5 changes: 5 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ de:
file_size_not_greater_than: "Dateigröße muss größer als %{min_size} sein (aktuelle Dateigröße ist %{file_size})"
file_size_not_greater_than_or_equal_to: "Dateigröße muss größer oder gleich %{min_size} sein (aktuelle Dateigröße ist %{file_size})"
file_size_not_between: "Dateigröße muss zwischen %{min_size} und %{max_size} liegen (aktuelle Dateigröße ist %{file_size})"
total_file_size_not_less_than: "Die gesamte Dateigröße muss kleiner als %{max_size} sein (aktuelle Dateigröße ist %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "Die gesamte Dateigröße muss kleiner oder gleich %{max_size} sein (aktuelle Dateigröße ist %{total_file_size})"
total_file_size_not_greater_than: "Die gesamte Dateigröße muss größer als %{min_size} sein (aktuelle Dateigröße ist %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "Die gesamte Dateigröße muss größer oder gleich %{min_size} sein (aktuelle Dateigröße ist %{total_file_size})"
total_file_size_not_between: "Die gesamte Dateigröße muss zwischen %{min_size} und %{max_size} liegen (aktuelle Dateigröße ist %{total_file_size})"
limit_out_of_range: "Anzahl ist außerhalb des gültigen Bereichs"
image_metadata_missing: "ist kein gültiges Bild"
dimension_min_inclusion: "muss größer oder gleich %{width} x %{height} Pixel sein"
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ en:
file_size_not_greater_than: "file size must be greater than %{min_size} (current size is %{file_size})"
file_size_not_greater_than_or_equal_to: "file size must be greater than or equal to %{min_size} (current size is %{file_size})"
file_size_not_between: "file size must be between %{min_size} and %{max_size} (current size is %{file_size})"
total_file_size_not_less_than: "total file size must be less than %{max_size} (current size is %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "total file size must be less than or equal to %{max_size} (current size is %{total_file_size})"
total_file_size_not_greater_than: "total file size must be greater than %{min_size} (current size is %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "total file size must be greater than or equal to %{min_size} (current size is %{total_file_size})"
total_file_size_not_between: "total file size must be between %{min_size} and %{max_size} (current size is %{total_file_size})"
limit_out_of_range: "total number is out of range"
image_metadata_missing: "is not a valid image"
dimension_min_inclusion: "must be greater than or equal to %{width} x %{height} pixel"
Expand Down
5 changes: 5 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ es:
file_size_not_greater_than: "el tamaño del archivo debe ser mayor que %{min_size} (el tamaño actual es %{file_size})"
file_size_not_greater_than_or_equal_to: "el tamaño del archivo debe ser mayor o igual a %{min_size} (el tamaño actual es %{file_size})"
file_size_not_between: "el tamaño del archivo debe estar entre %{min_size} y %{max_size} (el tamaño actual es %{file_size})"
total_file_size_not_less_than: "el tamaño total del archivo debe ser inferior a %{max_size} (el tamaño actual es %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "el tamaño total del archivo debe ser menor o igual a %{max_size} (el tamaño actual es %{total_file_size})"
total_file_size_not_greater_than: "el tamaño total del archivo debe ser mayor que %{min_size} (el tamaño actual es %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "el tamaño total del archivo debe ser mayor o igual a %{min_size} (el tamaño actual es %{total_file_size})"
total_file_size_not_between: "el tamaño total del archivo debe estar entre %{min_size} y %{max_size} (el tamaño actual es %{total_file_size})"
limit_out_of_range: "el número total está fuera de rango"
image_metadata_missing: "no es una imagen válida"
dimension_min_inclusion: "debe ser mayor o igual a %{width} x %{height} pixel"
Expand Down
5 changes: 5 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ fr:
file_size_not_greater_than: "la taille du fichier doit être supérieure à %{min_size} (la taille actuelle est %{file_size})"
file_size_not_greater_than_or_equal_to: "la taille du fichier doit être supérieure ou égale à %{min_size} (la taille actuelle est %{file_size})"
file_size_not_between: "la taille du fichier doit être comprise entre %{min_size} et %{max_size} (la taille actuelle est %{file_size})"
total_file_size_not_less_than: "la taille totale des fichiers doit être inférieure à %{max_size} (la taille actuelle est %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "la taille totale des fichiers doit être inférieure ou égale à %{max_size} (la taille actuelle est %{total_file_size})"
total_file_size_not_greater_than: "la taille totale des fichiers doit être supérieure à %{min_size} (la taille actuelle est %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "la taille totale des fichiers doit être supérieure ou égale à %{min_size} (la taille actuelle est %{total_file_size})"
total_file_size_not_between: "la taille totale des fichiers doit être comprise entre %{min_size} et %{max_size} (la taille actuelle est %{total_file_size})"
limit_out_of_range: "le nombre total est hors limites"
image_metadata_missing: "n'est pas une image valide"
dimension_min_inclusion: "doit être supérieur ou égal à %{width} x %{height} pixels"
Expand Down
5 changes: 5 additions & 0 deletions config/locales/it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ it:
file_size_not_greater_than: "la dimensione del file deve essere maggiore di %{min_size} (la dimensione attuale è %{file_size})"
file_size_not_greater_than_or_equal_to: "la dimensione del file deve essere maggiore o uguale a %{min_size} (la dimensione attuale è %{file_size})"
file_size_not_between: "la dimensione del file deve essere compresa tra %{min_size} e %{max_size} (la dimensione attuale è %{file_size})"
total_file_size_not_less_than: "la dimensione totale dei file deve essere inferiore a %{max_size} (la dimensione attuale è %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "la dimensione totale dei file deve essere minore o uguale a %{max_size} (la dimensione attuale è %{total_file_size})"
total_file_size_not_greater_than: "la dimensione totale dei file deve essere maggiore di %{min_size} (la dimensione attuale è %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "la dimensione totale dei file deve essere maggiore o uguale a %{min_size} (la dimensione attuale è %{total_file_size})"
total_file_size_not_between: "la dimensione totale dei file deve essere compresa tra %{min_size} e %{max_size} (la dimensione attuale è %{total_file_size})"
limit_out_of_range: "il valore è al di fuori dell’intervallo consentito"
image_metadata_missing: "non è un'immagine valida"
dimension_min_inclusion: "deve essere maggiore o uguale a %{width} x %{height} pixel"
Expand Down
5 changes: 5 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ ja:
file_size_not_greater_than: "ファイル サイズは %{min_size} より大きい必要があります (現在のサイズは %{file_size} です)"
file_size_not_greater_than_or_equal_to: "ファイル サイズは %{min_size} 以上である必要があります (現在のサイズは %{file_size})"
file_size_not_between: "ファイル サイズは %{min_size} から %{max_size} の間でなければなりません (現在のサイズは %{file_size} です)"
total_file_size_not_less_than: "ファイルの合計サイズは %{max_size} 未満である必要があります (現在のサイズは %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "ファイルの合計サイズは %{max_size} 以下である必要があります (現在のサイズは %{total_file_size})"
total_file_size_not_greater_than: "ファイルの合計サイズは %{min_size} より大きくなければなりません (現在のサイズは %{total_file_size} です)"
total_file_size_not_greater_than_or_equal_to: "ファイルの合計サイズは %{min_size} 以上である必要があります (現在のサイズは %{total_file_size})"
total_file_size_not_between: "ファイルの合計サイズは %{min_size} から %{max_size} までである必要があります (現在のサイズは %{total_file_size})"
limit_out_of_range: "の数が許容範囲外です"
image_metadata_missing: "は不正な画像です"
dimension_min_inclusion: "は %{width} x %{height} ピクセル以上の大きさにしてください"
Expand Down
5 changes: 5 additions & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ nl:
file_size_not_greater_than: "bestandsgrootte moet groter zijn dan %{min_size} (huidige grootte is %{file_size})"
file_size_not_greater_than_or_equal_to: "bestandsgrootte moet groter zijn dan of gelijk zijn aan %{min_size} (huidige grootte is %{file_size})"
file_size_not_between: "bestandsgrootte moet tussen %{min_size} en %{max_size} liggen (huidige grootte is %{file_size})"
total_file_size_not_less_than: "totale bestandsgrootte moet kleiner zijn dan %{max_size} (huidige grootte is %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "totale bestandsgrootte moet kleiner zijn dan of gelijk zijn aan %{max_size} (huidige grootte is %{total_file_size})"
total_file_size_not_greater_than: "totale bestandsgrootte moet groter zijn dan %{min_size} (huidige grootte is %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "totale bestandsgrootte moet groter zijn dan of gelijk zijn aan %{min_size} (huidige grootte is %{total_file_size})"
total_file_size_not_between: "totale bestandsgrootte moet tussen %{min_size} en %{max_size} liggen (huidige grootte is %{total_file_size})"
limit_out_of_range: "totaal aantal valt buiten het vereiste bereik"
image_metadata_missing: "is geen geldige afbeelding"
dimension_min_inclusion: "moet groter of gelijk zijn aan %{width} x %{height} pixels"
Expand Down
5 changes: 5 additions & 0 deletions config/locales/pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ pl:
file_size_not_greater_than: "rozmiar pliku musi być większy niż %{min_size} (obecny rozmiar to %{file_size})"
file_size_not_greater_than_or_equal_to: "rozmiar pliku musi być większy lub równy %{min_size} (obecny rozmiar to %{file_size})"
file_size_not_between: "rozmiar pliku musi mieścić się w przedziale od %{min_size} do %{max_size} (obecny rozmiar to %{file_size})"
total_file_size_not_less_than: "całkowity rozmiar pliku musi być mniejszy niż %{max_size} (obecny rozmiar to %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "całkowity rozmiar pliku musi być mniejszy lub równy %{max_size} (obecny rozmiar to %{total_file_size})"
total_file_size_not_greater_than: "całkowity rozmiar pliku musi być większy niż %{min_size} (obecny rozmiar to %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "całkowity rozmiar pliku musi być większy lub równy %{min_size} (obecny rozmiar to %{total_file_size})"
total_file_size_not_between: "całkowity rozmiar pliku musi mieścić się w przedziale od %{min_size} do %{max_size} (obecny rozmiar to %{total_file_size})"
limit_out_of_range: "ilość przekracza dopuszczalny zakres"
image_metadata_missing: "nie jest prawidłowym obrazem"
dimension_min_inclusion: "musi być równe lub większe od %{width} x %{height} pixeli"
Expand Down
5 changes: 5 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ pt-BR:
file_size_not_greater_than: "o tamanho do arquivo deve ser maior que %{min_size} (o tamanho atual é %{file_size})"
file_size_not_greater_than_or_equal_to: "o tamanho do arquivo deve ser maior ou igual a %{min_size} (o tamanho atual é %{file_size})"
file_size_not_between: "o tamanho do arquivo deve estar entre %{min_size} e %{max_size} (o tamanho atual é %{file_size})"
total_file_size_not_less_than: "o tamanho total do arquivo deve ser menor que %{max_size} (o tamanho atual é %{total_file_size})"
total_file_size_not_less_than_or_equal_to: "o tamanho total do arquivo deve ser menor ou igual a %{max_size} (o tamanho atual é %{total_file_size})"
total_file_size_not_greater_than: "o tamanho total do arquivo deve ser maior que %{min_size} (o tamanho atual é %{total_file_size})"
total_file_size_not_greater_than_or_equal_to: "o tamanho total do arquivo deve ser maior ou igual a %{min_size} (o tamanho atual é %{total_file_size})"
total_file_size_not_between: "o tamanho total do arquivo deve estar entre %{min_size} e %{max_size} (o tamanho atual é %{total_file_size})"
limit_out_of_range: "o número total está fora do limite"
image_metadata_missing: "não é uma imagem válida"
dimension_min_inclusion: "deve ser maior ou igual a %{width} x %{height} pixels"
Expand Down
Loading

0 comments on commit f9a573e

Please sign in to comment.