validates_lengths_from_database 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b858050297bdf446939c8671bc1b2260345e6bc223762cb0a533264e8dce2b1a
4
- data.tar.gz: a5f5f3f169cb2919f1bedd7baa548cebb4a75730ca953280636e4f1fa3058a73
3
+ metadata.gz: 9b22b423bd4a2bdda0b3acd66414b3621c2a003ef8f80b8983aca80989a45331
4
+ data.tar.gz: 695bf47b2bcd53dd6d7847df84ef296f68590121c958db72dfedaea60e120cde
5
5
  SHA512:
6
- metadata.gz: 0d704d3980c28cbca2d1c09936d664e02d457ad311b570c1e01c5ca247b7e43129d90b8c82b8edb6c22978971a0a3c96e3dcf49e1791b06f33d9ec7db61b8e2a
7
- data.tar.gz: b5b2d7c0c087479073b8b983c2dbc067531802a9f86b4eea76e72ec6d36e96bc85b34d01125ea3bb7316172e6fae8751b2a1c10ad58ef472bd7fa36caf8e1992
6
+ metadata.gz: f645747fd9a6669e2680f7a9bdb5b8c5e046a38f3289cc90dc0c08141b17df9dbe4f3023795b5158a9851aff9733ac3b3db6dec257ed2268e2623278a7e08986
7
+ data.tar.gz: 1b4eff8c550f2bb5c02073cc6c5c88f9e4c1e224d1eebbf517e7bfdde57d43bb47d4b8019e264fc45167fea87622952372fcdd8c249d724b4c7dc8efed3a9964
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v1.1.0 (unreleased)
4
+
5
+ - Validate that integer columns can hold the value, including MySQL `TINYINT` and `UNSIGNED` columns. Values Rails would refuse to save with an `ActiveModel::RangeError` now fail validation with "is out of range" (the `out_of_range` key) instead (#33)
6
+
3
7
  ## v1.0.1 (unreleased)
4
8
 
5
9
  - Add `validates_lengths_from_database` to ActiveRecord::Base whenever the gem is required, so it works without Rails and no longer depends on Rails being loaded first (#23)
data/README.md CHANGED
@@ -87,6 +87,7 @@ end
87
87
  * **String (`VARCHAR`) columns** are limited to the column's length in characters.
88
88
  * **`TEXT` columns** are limited to the column's length in bytes, since that is how MySQL measures them.
89
89
  * **`DECIMAL` columns** with a precision and scale must fit within them. For example, `DECIMAL(5,2)` accepts -999.99 through 999.99.
90
+ * **Integer columns** must fit the column's size, such as -32,768 through 32,767 for `SMALLINT`, including MySQL's `TINYINT` and `UNSIGNED` columns. These are exactly the values Rails would otherwise refuse to save with an `ActiveModel::RangeError`. Use `except:` to skip a column.
90
91
 
91
92
  Columns without a limit are skipped, so what this gem does depends on your database:
92
93
 
@@ -94,10 +95,11 @@ Columns without a limit are skipped, so what this gem does depends on your datab
94
95
  * **PostgreSQL**: Rails creates `t.string` columns with no length limit, and `TEXT` columns never have one, so only columns declared with an explicit `limit:` are validated.
95
96
  * **SQLite**: limits declared in migrations are validated, even though SQLite itself does not enforce them.
96
97
 
97
- ## Translating the bytes error message
98
+ ## Translating error messages
98
99
 
99
100
  `TEXT` columns that are too long get the `too_long_bytes` error, which defaults to "is too long (maximum is %{count} bytes)".
100
- Override it in your locale files like any other ActiveModel error message:
101
+ Integers that don't fit their column get the `out_of_range` error, which defaults to "is out of range".
102
+ Override them in your locale files like any other ActiveModel error message:
101
103
 
102
104
  ```yaml
103
105
  en:
@@ -106,6 +108,7 @@ en:
106
108
  too_long_bytes:
107
109
  one: "is too long (maximum is 1 byte)"
108
110
  other: "is too long (maximum is %{count} bytes)"
111
+ out_of_range: "is out of range"
109
112
  ```
110
113
 
111
114
  ## Running Tests
@@ -4,3 +4,4 @@ en:
4
4
  too_long_bytes:
5
5
  one: "is too long (maximum is 1 byte)"
6
6
  other: "is too long (maximum is %{count} bytes)"
7
+ out_of_range: "is out of range"
@@ -57,7 +57,7 @@ module ValidatesLengthsFromDatabase
57
57
 
58
58
  next if column_schema.nil?
59
59
  next if column_schema.respond_to?(:array) && column_schema.array
60
- next unless [:string, :text, :decimal].include?(column_schema.type)
60
+ next unless [:string, :text, :decimal, :integer].include?(column_schema.type)
61
61
 
62
62
  case column_schema.type
63
63
  when :string
@@ -72,6 +72,8 @@ module ValidatesLengthsFromDatabase
72
72
  if column_schema.precision && column_schema.scale
73
73
  validate_numericality_with_precision(column, column_schema)
74
74
  end
75
+ when :integer
76
+ validate_integer_range(column)
75
77
  end
76
78
  end
77
79
  end
@@ -105,4 +107,13 @@ module ValidatesLengthsFromDatabase
105
107
  :greater_than => -max_val
106
108
  ).validate(self)
107
109
  end
110
+
111
+ # The column type knows its byte size and, on MySQL, whether it is unsigned,
112
+ # and serializable? is false for exactly the values Rails would refuse to save.
113
+ def validate_integer_range(column)
114
+ value = read_attribute_for_validation(column)
115
+ return if value.nil?
116
+
117
+ errors.add(column, :out_of_range) unless self.class.type_for_attribute(column).serializable?(value)
118
+ end
108
119
  end
@@ -1,3 +1,3 @@
1
1
  module ValidatesLengthsFromDatabase
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_lengths_from_database
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hughes