schema_validations 1.4.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8484a054c29074d10d3fa8dbb06df1206e4f3d3f
4
- data.tar.gz: a6d45b80b265de0de5c5d23a33ddfcfbafc4c2e4
3
+ metadata.gz: 57990870ff9defa81491b9a1ddfeb8070ce0f4da
4
+ data.tar.gz: 3553e2c896fbde23dc9458ebc4b4c0ddefa67594
5
5
  SHA512:
6
- metadata.gz: d0e13def81c02bf86fe2a0df3f557cccfc9ec5f50a1501a43bedb5837f3b311a64e13867e8d8bacb682ddfddb068f5ff936d72514d6d3d99be54ebb5cea9412c
7
- data.tar.gz: 302f4ea589910494881650a2aae57ed654cadc880622acfc0b645a5d66c89122bbbaff1f55ecc59ebb27efff05af58d0473d4285aa510b4d05907a02f2cea325
6
+ metadata.gz: 09d958820120ccb7a284996ca3308ed3ecee9971ff194c5c9a8de67f4a4d311cd53ae3fc4e9d0ff51c802e8f607da2343e9f1f07dbd2a6161e25e5b366f26d8b
7
+ data.tar.gz: 32906f059b3b7ae7eac3ba36b0b400d6edf3ec39bd43d8555f7ce024c983106c3551f83c887dfceee48526305c44645a6ad77f61222fd665473db4f6a1922529
data/README.md CHANGED
@@ -22,8 +22,7 @@ constraints.
22
22
  t.boolean :confirmed, :null => false
23
23
  end
24
24
 
25
- In that case :null => false, :limit => 30 and :boolean must be covered on the
26
- model level.
25
+ In that case the constraints `:null => false`, `:limit => 30` and `:boolean` must be validated on the model level, to avoid ugly database exceptions:
27
26
 
28
27
  class User < ActiveRecord::Base
29
28
  validates :email, :presence => true, :length => { :maximum => 30 }
@@ -32,9 +31,7 @@ model level.
32
31
 
33
32
  ...which isn't the most DRY approach.
34
33
 
35
- SchemaValidations aims to cover that and does boring work for you. It inspect
36
- the database and automatically creates validations basing on the schema. After
37
- installing it your model is as simple as it can be.
34
+ SchemaValidations aims to DRY up your models, doing that boring work for you. It inspects the database and automatically creates validations based on the schema. After installing it your model is as simple as it can be.
38
35
 
39
36
  class User < ActiveRecord::Base
40
37
  end
@@ -42,64 +39,19 @@ installing it your model is as simple as it can be.
42
39
  Validations are there but they are created by schema_validations under the
43
40
  hood.
44
41
 
45
- ## Compatibility
46
-
47
- As of version 1.2.0, SchemaValidations supports and is tested on:
48
-
49
- <!-- SCHEMA_DEV: MATRIX - begin -->
50
- <!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->
51
- * ruby **2.1.5** with activerecord **4.2**, using **mysql2**, **postgresql** or **sqlite3**
52
-
53
- <!-- SCHEMA_DEV: MATRIX - end -->
54
-
55
- Earlier versions of SchemaValidations supported:
56
-
57
- * rails 3.2, 4.1, and 4.2.0
58
- * MRI ruby 1.9.3 and 2.1.5
59
-
60
42
  ## Installation
61
43
 
62
44
  Simply add schema_validations to your Gemfile.
63
45
 
64
46
  gem "schema_validations"
65
-
66
- ### What if I want something special?
67
-
68
- SchemaValidations is highly customizable. You can configure behavior globally
69
- via SchemaValidations.setup or per-model via
70
- SchemaValidations::ActiveRecord::schema_validations, such as:
71
-
72
- class User < ActiveRecord::Base
73
- schema_validations :except => :email
74
- validates :email, :presence => true, :length => { :in => 5..30 }
75
- end
76
-
77
- See SchemaValidations::Config for the available options.
78
-
79
- ### This seems cool, but I'm worried about too much automagic
80
-
81
- You can globally turn off automatic creation in
82
- `config/initializers/schema_validations.rb`:
83
-
84
- SchemaValidations.setup do |config|
85
- config.auto_create = false
86
- end
87
-
88
- Then in any model where you want automatic validations, just do
89
-
90
- class Post < ActiveRecord::Base
91
- schema_validations
92
- end
93
-
94
- You can also pass options as per above.
95
-
47
+
96
48
  ## Which validations are covered?
97
49
 
98
50
  Constraints:
99
51
 
100
52
  | Constraint | Validation |
101
53
  |---------------------|----------------------------------------------------------|
102
- | :null => false | validates ... :presence => true |
54
+ | `:null => false | validates ... :presence => true |
103
55
  | :limit => 100 | validates ... :length => { :maximum => 100 } |
104
56
  | :unique => true | validates ... :uniqueness => true |
105
57
  | :unique => true, :case_sensitive => false <br>(If [schema_plus_pg_indexes](https://github.com/SchemaPlus/schema_plus_pg_indexes) is also in use) | validates ... :uniqueness => { :case_sensitive => false } |
@@ -112,6 +64,82 @@ Data types:
112
64
  | :float | :validates ... :numericality => true |
113
65
  | :integer | :validates ... :numericality => { :only_integer => true, :greater_than_or_equal_to => ..., :less_than => ... } |
114
66
 
67
+
68
+ ## What if I want something special?
69
+
70
+ SchemaValidations' behavior can be configured globally and per-model.
71
+
72
+ ### Global configuration
73
+
74
+ In an initializer, such as `config/initializers/schema_validations.rb`, you can set any of these options. The default values are shown.
75
+
76
+ ```ruby
77
+ SchemaValidations.setup do |config|
78
+
79
+ # Whether to automatically create validations based on database constraints.
80
+ # (Can be set false globally to disable the gem by default, and set true per-model to enable.)
81
+ config.auto_create = true
82
+
83
+ # Restricts the set of field names to include in automatic validation.
84
+ # Value is a single name, an array of names, or nil.
85
+ config.only = nil
86
+
87
+ # Restricts the set of validation types to include in automatic validation.
88
+ # Value is a single type, an array of types, or nil.
89
+ # A type is specified as, e.g., `:validates_presence_of` or simply `:presence`.
90
+ config.only_type = nil
91
+
92
+ # A list of field names to exclude from automatic validation.
93
+ # Value is a single name, an array of names, or nil.
94
+ # (Providing a value per-model will completely replace a globally-configured list)
95
+ config.except = nil
96
+
97
+ # A list of validation types to exclude from automatic validation.
98
+ # Value is a single type, an array of types, or nil.
99
+ # (Providing a value per-model will completely replace a globally-configured list)
100
+ config.except_type = nil
101
+
102
+ # The base set of field names to always exclude from automatic validation.
103
+ # Value is a single name, an array of names, or nil.
104
+ # (This whitelist applies after all other considerations, global or per-model)
105
+ config.whitelist = [:created_at, :updated_at, :created_on, :updated_on]
106
+
107
+ # The base set of validation types to always exclude from automatic validation.
108
+ # Value is a single type, an array of types, or nil.
109
+ # (This whitelist applies after all other considerations, global or per-model)
110
+ config.whitelist_type = nil
111
+ end
112
+ ```
113
+
114
+ ### Per-model validation
115
+
116
+ You can override the global configuration per-model, using the `schema_validations` class method. All global configuration options are available as keyword options. For example:
117
+
118
+ ##### Disable per model:
119
+ ```ruby
120
+ class User < ActiveRecord::Base
121
+ schema_validations auto_create: false
122
+ end
123
+ ```
124
+
125
+ ##### Use a custom validation rather than schema_validations automatic default:
126
+ ```ruby
127
+ class User < ActiveRecord::Base
128
+ schema_validations except: :email # don't create default validation for email
129
+ validates :email, presence: true, length: { in: 5..30 }
130
+ end
131
+ ```
132
+
133
+ ##### Include validations every field, without a whitelist:
134
+
135
+ ```ruby
136
+ class User < ActiveRecord::Base
137
+ schema_validations :whitelist => nil
138
+ end
139
+ ```
140
+
141
+
142
+
115
143
  ## How do I know what it did?
116
144
  If you're curious (or dubious) about what validations SchemaValidations
117
145
  defines, you can check the log file. For every assocation that
@@ -123,14 +151,36 @@ which shows the exact validation definition call.
123
151
 
124
152
 
125
153
  SchemaValidations defines the validations lazily for each class, only creating
126
- them when they are needed (to validate a record of the class, or in response
154
+ them when they are needed (in order to validate a record of the class, or in response
127
155
  to introspection on the class). So you may need to search through the log
128
156
  file for "schema_validations" to find all the validations, and some classes'
129
157
  validations may not be defined at all if they were never needed for the logged
130
158
  use case.
131
159
 
160
+ ## Compatibility
161
+
162
+ As of version 1.2.0, SchemaValidations supports and is tested on:
163
+
164
+ <!-- SCHEMA_DEV: MATRIX - begin -->
165
+ <!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->
166
+ * ruby **2.1.5** with activerecord **4.2**, using **mysql2**, **postgresql** or **sqlite3**
167
+
168
+ <!-- SCHEMA_DEV: MATRIX - end -->
169
+
170
+ Earlier versions of SchemaValidations supported:
171
+
172
+ * rails 3.2, 4.1, and 4.2.0
173
+ * MRI ruby 1.9.3 and 2.1.5
174
+
175
+
132
176
  ## Release Notes
133
177
 
178
+ ### 2.0.0
179
+
180
+ This major version is backwards compatible for most uses. Only those who specified a per-model `:except` clause would be affected.
181
+
182
+ * Add whitelist configuration option (thanks to [@allenwq](https://github.com/allenwq)). Previously, overriding `:except` per-model would clobber the default values. E.g. using the documented example `except: :mail` would accidentally cause validations to be issued `updated_at` to be validated. Now `:except` works more naturally. This is however technically a breaking change, hence the version bump.
183
+
134
184
  ### 1.4.0
135
185
 
136
186
  * Add support for case-insensitive uniqueness. Thanks to [allenwq](https://github.com/allenwq)
@@ -36,11 +36,26 @@ module SchemaValidations
36
36
  has_value :only, :default => nil
37
37
 
38
38
  ##
39
- # :attr_accessor: except
39
+ # :attr_accessor: whitelist
40
40
  #
41
41
  # List of field names to exclude from automatic validation.
42
42
  # Value is a single name, an array of names, or +nil+. Default is <tt>[:created_at, :updated_at, :created_on, :updated_on]</tt>.
43
- has_value :except, :default => [:created_at, :updated_at, :created_on, :updated_on]
43
+ has_value :whitelist, :default => [:created_at, :updated_at, :created_on, :updated_on]
44
+
45
+ ##
46
+ # :attr_accessor: except
47
+ #
48
+ # List of field names to exclude from automatic validation.
49
+ # Value is a single name, and array of names, or +nil+. Default is +nil+.
50
+ has_value :except, :default => nil
51
+
52
+ ##
53
+ # :attr_accessor: whitelist_type
54
+ #
55
+ # List of validation types to exclude from automatic validation.
56
+ # Value is a single type, and array of types, or +nil+. Default is +nil+.
57
+ # A type is specified as, e.g., +:validates_presence_of+ or simply +:presence+.
58
+ has_value :whitelist_type, :default => nil
44
59
 
45
60
  ##
46
61
  # :attr_accessor: except_type
@@ -201,10 +201,12 @@ module SchemaValidations
201
201
  if match = macro.to_s.match(/^validates_(.*)_of$/)
202
202
  types << match[1].to_sym
203
203
  end
204
- return false if config.only and not Array.wrap(config.only).include?(name)
205
- return false if config.except and Array.wrap(config.except).include?(name)
206
- return false if config.only_type and not (Array.wrap(config.only_type) & types).any?
207
- return false if config.except_type and (Array.wrap(config.except_type) & types).any?
204
+ return false if config.only and not Array.wrap(config.only).include?(name)
205
+ return false if config.except and Array.wrap(config.except).include?(name)
206
+ return false if config.whitelist and Array.wrap(config.whitelist).include?(name)
207
+ return false if config.only_type and not (Array.wrap(config.only_type) & types).any?
208
+ return false if config.except_type and (Array.wrap(config.except_type) & types).any?
209
+ return false if config.whitelist_type and (Array.wrap(config.whitelist_type) & types).any?
208
210
  return true
209
211
  end
210
212
 
@@ -1,3 +1,3 @@
1
1
  module SchemaValidations
2
- VERSION = "1.4.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -21,6 +21,7 @@ describe "Validations" do
21
21
  t.string :author, :null => false
22
22
  t.string :content, :limit => 200
23
23
  t.string :type
24
+ t.timestamps :null => false
24
25
  end
25
26
  add_index :reviews, :article_id, :unique => true
26
27
 
@@ -175,6 +176,18 @@ describe "Validations" do
175
176
  expect(@review.error_on(:author).size).to eq(1)
176
177
  end
177
178
 
179
+ it "shouldn't validate the fields in default whitelist" do
180
+ Review.schema_validations :except => :content
181
+ expect(Review.new.error_on(:updated_at).size).to eq(0)
182
+ expect(Review.new.error_on(:created_at).size).to eq(0)
183
+ end
184
+
185
+ it "shouldn't validate the fields in whitelist" do
186
+ Review.schema_validations :except => :content, whitelist: [:updated_at]
187
+ expect(Review.new.error_on(:updated_at).size).to eq(0)
188
+ expect(Review.new.error_on(:created_at).size).to eq(1)
189
+ end
190
+
178
191
  it "shouldn't validate types passed to :except_type option using full validation" do
179
192
  Review.schema_validations :except_type => :validates_length_of
180
193
  @review = Review.new(:content => @too_big_content)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronen Barzel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-10-28 00:00:00.000000000 Z
12
+ date: 2016-01-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: schema_plus_columns