validates_by_schema 0.5.0 → 0.5.2
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 +4 -4
- data/README.md +4 -4
- data/lib/validates_by_schema/validation_option.rb +7 -11
- data/lib/validates_by_schema/version.rb +1 -1
- data/lib/validates_by_schema.rb +15 -2
- data/spec/ci/{rails52.gemfile → rails70.gemfile} +1 -1
- data/spec/ci/{rails60.gemfile → rails71.gemfile} +1 -1
- data/spec/config/schema.rb +1 -1
- data/spec/db/test.sqlite3 +0 -0
- data/spec/db/test.sqlite3-shm +0 -0
- data/spec/db/test.sqlite3-wal +0 -0
- data/spec/spec_helper.rb +9 -1
- data/spec/support/models/contraption.rb +1 -1
- metadata +23 -23
- data/spec/ci/rails50.gemfile +0 -10
- data/spec/ci/rails50.gemfile.lock +0 -118
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6877c59f4c23593b7c11bb834b7ab9cc6cede8bf24a66fb6b3aef6d6d7d8ca20
|
4
|
+
data.tar.gz: 3c5bcafcfcaa8b2cf6b902fb2c93e85cdc550fe03c740939b9eeb7f1251b4ae0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 692c60647d11664e68d00bdbc7d2a7567277722d3cd95a8a8d608426e7b1d492863301c60ad7e9ffb267af171cdecef22753687cf165e0c68de91c29cfc2768a
|
7
|
+
data.tar.gz: 7c96d752cb3f58938e4ae17ff81900d77cf3f6d6a7f76bf320169e5a4136bac62ba87026b1f2f31e03aa4f9279c31e21ac72652eddc4b7259d4ddeeaa1da3f2c
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# Validates By Schema (validates_by_schema)
|
2
2
|
|
3
|
-
[](https://rubygems.org/gems/validates_by_schema)
|
4
|
+
[](https://github.com/joshwlewis/validates_by_schema/actions/workflows/build.yml)
|
5
|
+
[](https://coveralls.io/r/joshwlewis/validates_by_schema)
|
6
|
+
[](https://codeclimate.com/github/joshwlewis/validates_by_schema)
|
7
7
|
|
8
8
|
Automatic validation based on your database schema column types and limits. Keep your code DRY by inferring column validations from table properties!
|
9
9
|
|
@@ -1,11 +1,12 @@
|
|
1
1
|
class ValidatesBySchema::ValidationOption
|
2
2
|
# column here must be an ActiveRecord column
|
3
3
|
# i.e. MyARModel.columns.first
|
4
|
-
attr_accessor :klass, :column
|
4
|
+
attr_accessor :klass, :column, :unique_indexes
|
5
5
|
|
6
|
-
def initialize(klass, column)
|
6
|
+
def initialize(klass, column, unique_indexes = [])
|
7
7
|
@klass = klass
|
8
8
|
@column = column
|
9
|
+
@unique_indexes = unique_indexes.select { |index| index.columns.first == column.name }
|
9
10
|
end
|
10
11
|
|
11
12
|
def define!
|
@@ -16,13 +17,15 @@ class ValidatesBySchema::ValidationOption
|
|
16
17
|
else
|
17
18
|
define_validations(to_hash)
|
18
19
|
end
|
19
|
-
define_uniqueness_validations
|
20
|
+
define_uniqueness_validations
|
20
21
|
end
|
21
22
|
|
22
23
|
private
|
23
24
|
|
24
25
|
def define_belongs_to_presence_validation
|
25
|
-
|
26
|
+
if !ActiveRecord::Base.belongs_to_required_by_default && presence
|
27
|
+
klass.validates association.name, presence: true
|
28
|
+
end
|
26
29
|
end
|
27
30
|
|
28
31
|
def define_uniqueness_validations
|
@@ -79,13 +82,6 @@ class ValidatesBySchema::ValidationOption
|
|
79
82
|
end
|
80
83
|
end
|
81
84
|
|
82
|
-
def unique_indexes
|
83
|
-
klass
|
84
|
-
.connection
|
85
|
-
.indexes(klass.table_name)
|
86
|
-
.select { |index| index.unique && index.columns.first == column.name }
|
87
|
-
end
|
88
|
-
|
89
85
|
def case_sensitive?
|
90
86
|
!klass.connection.respond_to?(:collation) ||
|
91
87
|
!klass.connection.collation.end_with?('_ci')
|
data/lib/validates_by_schema.rb
CHANGED
@@ -18,6 +18,9 @@ module ValidatesBySchema
|
|
18
18
|
|
19
19
|
def load_schema!
|
20
20
|
super
|
21
|
+
# Set flag here to avoid infinite recursion
|
22
|
+
@schema_loaded = true
|
23
|
+
|
21
24
|
# define schema validations lazy to avoid accessing the database
|
22
25
|
# at class load time.
|
23
26
|
define_schema_validations
|
@@ -28,13 +31,23 @@ module ValidatesBySchema
|
|
28
31
|
def define_schema_validations
|
29
32
|
return unless @validates_by_schema_options
|
30
33
|
|
34
|
+
unique_indexes = ValidatesBySchema.validate_uniqueness ? fetch_unique_indexes : []
|
31
35
|
customized_schema_validatable_columns.each do |c|
|
32
|
-
ValidationOption.new(self, c).define!
|
36
|
+
ValidationOption.new(self, c, unique_indexes).define!
|
33
37
|
end
|
34
38
|
|
35
39
|
@validates_by_schema_options = nil
|
36
40
|
end
|
37
41
|
|
42
|
+
def fetch_unique_indexes
|
43
|
+
if connection.schema_cache.respond_to?(:indexes)
|
44
|
+
connection.schema_cache.indexes(table_name).select(&:unique)
|
45
|
+
else
|
46
|
+
# Rails < 6.0
|
47
|
+
connection.indexes(table_name).select(&:unique)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
38
51
|
def customized_schema_validatable_columns
|
39
52
|
# Allow user to specify :only or :except options
|
40
53
|
schema_validatable_columns.tap do |columns|
|
@@ -54,7 +67,7 @@ module ValidatesBySchema
|
|
54
67
|
end
|
55
68
|
|
56
69
|
def ignored_columns_for_validates_by_schema
|
57
|
-
[
|
70
|
+
['id', 'created_at', 'updated_at', 'deleted_at']
|
58
71
|
end
|
59
72
|
|
60
73
|
end
|
data/spec/config/schema.rb
CHANGED
@@ -32,7 +32,7 @@ ActiveRecord::Schema.define(version: 20_121_210_034_140) do
|
|
32
32
|
t.integer 'parent_id', null: false
|
33
33
|
t.integer 'other_id'
|
34
34
|
t.integer 'kind', null: false
|
35
|
-
t.string 'list', array: true, limit: 3
|
35
|
+
t.string 'list', ENV['DB'] == 'postgresql' ? { array: true, limit: 3 } : {}
|
36
36
|
|
37
37
|
t.index 'parent_id'
|
38
38
|
t.index ['name', 'wheels'], unique: true
|
data/spec/db/test.sqlite3
CHANGED
Binary file
|
Binary file
|
Binary file
|
data/spec/spec_helper.rb
CHANGED
@@ -22,7 +22,15 @@ end
|
|
22
22
|
require 'validates_by_schema'
|
23
23
|
|
24
24
|
# Setup the database
|
25
|
-
conf =
|
25
|
+
conf = nil
|
26
|
+
begin
|
27
|
+
# load with psych 4 (>= ruby 3.1)
|
28
|
+
conf = YAML.load(ERB.new(File.read(File.join(File.dirname(__FILE__), 'config', 'database.yml'))).result, aliases: true)
|
29
|
+
rescue ArgumentError
|
30
|
+
# fallback to psych 3 syntax
|
31
|
+
conf = YAML.load(ERB.new(File.read(File.join(File.dirname(__FILE__), 'config', 'database.yml'))).result)
|
32
|
+
end
|
33
|
+
|
26
34
|
ActiveRecord::Base.establish_connection(conf['test'])
|
27
35
|
|
28
36
|
ActiveRecord::Base.belongs_to_required_by_default = false
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_by_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Lewis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-10-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 6.0.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 6.0.0
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: mysql2
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -99,16 +99,16 @@ dependencies:
|
|
99
99
|
name: sqlite3
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- - "
|
102
|
+
- - "<"
|
103
103
|
- !ruby/object:Gem::Version
|
104
|
-
version: '0'
|
104
|
+
version: '2.0'
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
|
-
- - "
|
109
|
+
- - "<"
|
110
110
|
- !ruby/object:Gem::Version
|
111
|
-
version: '0'
|
111
|
+
version: '2.0'
|
112
112
|
description: Keep your code DRY by inferring column validations from table properties!
|
113
113
|
Automagically validate presence, length, numericality, inclusion and uniqueness
|
114
114
|
of ActiveRecord backed columns.
|
@@ -125,13 +125,13 @@ files:
|
|
125
125
|
- lib/validates_by_schema.rb
|
126
126
|
- lib/validates_by_schema/validation_option.rb
|
127
127
|
- lib/validates_by_schema/version.rb
|
128
|
-
- spec/ci/
|
129
|
-
- spec/ci/
|
130
|
-
- spec/ci/rails52.gemfile
|
131
|
-
- spec/ci/rails60.gemfile
|
128
|
+
- spec/ci/rails70.gemfile
|
129
|
+
- spec/ci/rails71.gemfile
|
132
130
|
- spec/config/database.yml
|
133
131
|
- spec/config/schema.rb
|
134
132
|
- spec/db/test.sqlite3
|
133
|
+
- spec/db/test.sqlite3-shm
|
134
|
+
- spec/db/test.sqlite3-wal
|
135
135
|
- spec/spec_helper.rb
|
136
136
|
- spec/support/models/contraption.rb
|
137
137
|
- spec/support/models/gadget.rb
|
@@ -161,22 +161,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
161
161
|
- !ruby/object:Gem::Version
|
162
162
|
version: '0'
|
163
163
|
requirements: []
|
164
|
-
rubygems_version: 3.
|
164
|
+
rubygems_version: 3.5.16
|
165
165
|
signing_key:
|
166
166
|
specification_version: 4
|
167
167
|
summary: Automatic validation based on your database schema column types and limits.
|
168
168
|
test_files:
|
169
|
-
- spec/
|
169
|
+
- spec/ci/rails70.gemfile
|
170
|
+
- spec/ci/rails71.gemfile
|
171
|
+
- spec/config/database.yml
|
172
|
+
- spec/config/schema.rb
|
170
173
|
- spec/db/test.sqlite3
|
171
|
-
- spec/
|
172
|
-
- spec/
|
174
|
+
- spec/db/test.sqlite3-shm
|
175
|
+
- spec/db/test.sqlite3-wal
|
176
|
+
- spec/spec_helper.rb
|
173
177
|
- spec/support/models/contraption.rb
|
174
|
-
- spec/support/models/sub_contraption.rb
|
175
178
|
- spec/support/models/gadget.rb
|
176
179
|
- spec/support/models/gizmo.rb
|
177
|
-
- spec/
|
178
|
-
- spec/
|
179
|
-
- spec/
|
180
|
-
- spec/ci/rails50.gemfile
|
181
|
-
- spec/ci/rails60.gemfile
|
182
|
-
- spec/ci/rails50.gemfile.lock
|
180
|
+
- spec/support/models/sub_contraption.rb
|
181
|
+
- spec/support/models/widget.rb
|
182
|
+
- spec/validations_spec.rb
|
data/spec/ci/rails50.gemfile
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: ../..
|
3
|
-
specs:
|
4
|
-
validates_by_schema (0.4.0)
|
5
|
-
activerecord (>= 5.0.0)
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
actionpack (5.0.7.2)
|
11
|
-
actionview (= 5.0.7.2)
|
12
|
-
activesupport (= 5.0.7.2)
|
13
|
-
rack (~> 2.0)
|
14
|
-
rack-test (~> 0.6.3)
|
15
|
-
rails-dom-testing (~> 2.0)
|
16
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
17
|
-
actionview (5.0.7.2)
|
18
|
-
activesupport (= 5.0.7.2)
|
19
|
-
builder (~> 3.1)
|
20
|
-
erubis (~> 2.7.0)
|
21
|
-
rails-dom-testing (~> 2.0)
|
22
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
23
|
-
activemodel (5.0.7.2)
|
24
|
-
activesupport (= 5.0.7.2)
|
25
|
-
activerecord (5.0.7.2)
|
26
|
-
activemodel (= 5.0.7.2)
|
27
|
-
activesupport (= 5.0.7.2)
|
28
|
-
arel (~> 7.0)
|
29
|
-
activesupport (5.0.7.2)
|
30
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
|
-
i18n (>= 0.7, < 2)
|
32
|
-
minitest (~> 5.1)
|
33
|
-
tzinfo (~> 1.1)
|
34
|
-
arel (7.1.4)
|
35
|
-
builder (3.2.4)
|
36
|
-
concurrent-ruby (1.1.9)
|
37
|
-
crass (1.0.6)
|
38
|
-
diff-lcs (1.4.4)
|
39
|
-
docile (1.4.0)
|
40
|
-
erubis (2.7.0)
|
41
|
-
i18n (1.8.11)
|
42
|
-
concurrent-ruby (~> 1.0)
|
43
|
-
loofah (2.12.0)
|
44
|
-
crass (~> 1.0.2)
|
45
|
-
nokogiri (>= 1.5.9)
|
46
|
-
method_source (1.0.0)
|
47
|
-
mini_portile2 (2.6.1)
|
48
|
-
minitest (5.14.4)
|
49
|
-
mysql2 (0.5.3)
|
50
|
-
nokogiri (1.12.5)
|
51
|
-
mini_portile2 (~> 2.6.1)
|
52
|
-
racc (~> 1.4)
|
53
|
-
pg (1.2.3)
|
54
|
-
racc (1.6.0)
|
55
|
-
rack (2.2.3)
|
56
|
-
rack-test (0.6.3)
|
57
|
-
rack (>= 1.0)
|
58
|
-
rails-dom-testing (2.0.3)
|
59
|
-
activesupport (>= 4.2.0)
|
60
|
-
nokogiri (>= 1.6)
|
61
|
-
rails-html-sanitizer (1.4.2)
|
62
|
-
loofah (~> 2.3)
|
63
|
-
railties (5.0.7.2)
|
64
|
-
actionpack (= 5.0.7.2)
|
65
|
-
activesupport (= 5.0.7.2)
|
66
|
-
method_source
|
67
|
-
rake (>= 0.8.7)
|
68
|
-
thor (>= 0.18.1, < 2.0)
|
69
|
-
rake (13.0.6)
|
70
|
-
rspec-core (3.10.1)
|
71
|
-
rspec-support (~> 3.10.0)
|
72
|
-
rspec-expectations (3.10.1)
|
73
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
74
|
-
rspec-support (~> 3.10.0)
|
75
|
-
rspec-mocks (3.10.2)
|
76
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
77
|
-
rspec-support (~> 3.10.0)
|
78
|
-
rspec-rails (4.1.2)
|
79
|
-
actionpack (>= 4.2)
|
80
|
-
activesupport (>= 4.2)
|
81
|
-
railties (>= 4.2)
|
82
|
-
rspec-core (~> 3.10)
|
83
|
-
rspec-expectations (~> 3.10)
|
84
|
-
rspec-mocks (~> 3.10)
|
85
|
-
rspec-support (~> 3.10)
|
86
|
-
rspec-support (3.10.3)
|
87
|
-
shoulda-matchers (4.5.1)
|
88
|
-
activesupport (>= 4.2.0)
|
89
|
-
simplecov (0.21.2)
|
90
|
-
docile (~> 1.1)
|
91
|
-
simplecov-html (~> 0.11)
|
92
|
-
simplecov_json_formatter (~> 0.1)
|
93
|
-
simplecov-html (0.12.3)
|
94
|
-
simplecov-lcov (0.8.0)
|
95
|
-
simplecov_json_formatter (0.1.3)
|
96
|
-
sqlite3 (1.3.13)
|
97
|
-
thor (1.1.0)
|
98
|
-
thread_safe (0.3.6)
|
99
|
-
tzinfo (1.2.9)
|
100
|
-
thread_safe (~> 0.1)
|
101
|
-
|
102
|
-
PLATFORMS
|
103
|
-
ruby
|
104
|
-
|
105
|
-
DEPENDENCIES
|
106
|
-
activerecord (~> 5.0.0)
|
107
|
-
mysql2
|
108
|
-
pg
|
109
|
-
rake
|
110
|
-
rspec-rails
|
111
|
-
shoulda-matchers
|
112
|
-
simplecov
|
113
|
-
simplecov-lcov
|
114
|
-
sqlite3 (~> 1.3.7)
|
115
|
-
validates_by_schema!
|
116
|
-
|
117
|
-
BUNDLED WITH
|
118
|
-
2.1.4
|