generated_schema_validations 0.2.3 → 0.3.1
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 +12 -1
- data/generated_schema_validations.gemspec +1 -1
- data/lib/generated_schema_validations/table.rb +20 -4
- data/lib/generated_schema_validations/template.rb +28 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4be368a0917bd00a79be8fc0ab6ac7ab4530bff7c3feec32fa731a5ccee2d5e3
|
4
|
+
data.tar.gz: 4cb0120a7cacd178b16253d04bdab2e5aed1edc37195609bf176b510700b4824
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 542791763e87ecc0f8f54fd18e3df3d41aaf4db6c7e146f158d1d4b2d74f75f1e7d69ea6f5aa2235cdcad18fa454849cd3b93396d75d7b67e1570918713d4d48
|
7
|
+
data.tar.gz: bd238fa0f5cc5953202e96893070e1c1c30b823949a11889854ccdba5765ec277192743bad6e018c8c7e287306ea9731716dd91664e9810f6f352368f78c2a59
|
data/README.md
CHANGED
@@ -36,13 +36,15 @@ class ApplicationRecord < ActiveRecord::Base
|
|
36
36
|
end
|
37
37
|
```
|
38
38
|
|
39
|
-
Use it in a model **after** you defined the associations:
|
39
|
+
Use it in a model **after** you defined the associations and enums:
|
40
40
|
|
41
41
|
```ruby
|
42
42
|
class User < ApplicationRecord
|
43
43
|
belongs_to :client
|
44
44
|
belongs_to :other
|
45
45
|
|
46
|
+
enum :actived, no: 0, yes: 1
|
47
|
+
|
46
48
|
validate :email_address, email_format: true
|
47
49
|
|
48
50
|
schema_validations # to use auto generated validations
|
@@ -95,6 +97,15 @@ You can watch changes on `schema_validations.rb` to understand the generated val
|
|
95
97
|
|
96
98
|
## Changelog
|
97
99
|
|
100
|
+
### 0.3.1
|
101
|
+
|
102
|
+
* Add exclusion for enum fields
|
103
|
+
|
104
|
+
### 0.3.0
|
105
|
+
|
106
|
+
* Check usage of schema_validations (see #3)
|
107
|
+
* Force excluding unique validations with where clause (see #2)
|
108
|
+
|
98
109
|
### 0.2.3
|
99
110
|
|
100
111
|
* Add `jsonb` and `xml` as possible field type
|
@@ -7,12 +7,19 @@ class GeneratedSchemaValidations::Table
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
ValidationWithoutEnum = Struct.new(:attribute, :validator, :options) do
|
11
|
+
def to_s
|
12
|
+
"validates_with_filter #{attribute.to_sym.inspect}, #{{ validator => options }.inspect} unless enums.include?(#{attribute.to_sym.inspect})"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
10
16
|
attr_reader :table_name
|
11
17
|
|
12
18
|
def initialize(table_name, &block)
|
13
19
|
@table_name = table_name
|
14
20
|
@column_names = []
|
15
21
|
@possible_belongs_to_not_null_columns = []
|
22
|
+
@bad_indexes = []
|
16
23
|
@unique_indexes = []
|
17
24
|
@validations = []
|
18
25
|
|
@@ -21,10 +28,11 @@ class GeneratedSchemaValidations::Table
|
|
21
28
|
|
22
29
|
def to_s
|
23
30
|
string = "\n"
|
24
|
-
string += "def dbv_#{table_name}_validations\n"
|
31
|
+
string += "def dbv_#{table_name}_validations(enums: [])\n"
|
25
32
|
if @possible_belongs_to_not_null_columns.present?
|
26
33
|
string += " belongs_to_presence_validations_for(#{@possible_belongs_to_not_null_columns.inspect})\n"
|
27
34
|
end
|
35
|
+
string += " bad_uniqueness_validations_for(#{@bad_indexes.inspect})\n" if @bad_indexes.present?
|
28
36
|
string += " belongs_to_uniqueness_validations_for(#{@unique_indexes.inspect})\n" if @unique_indexes.present?
|
29
37
|
string += " uniqueness_validations_for(#{@unique_indexes.inspect})\n" if @unique_indexes.present?
|
30
38
|
string += @validations.uniq.map { |v| " #{v}\n" }.join
|
@@ -35,6 +43,10 @@ class GeneratedSchemaValidations::Table
|
|
35
43
|
@validations.push(Validation.new(attribute, validator, options))
|
36
44
|
end
|
37
45
|
|
46
|
+
def validates_without_enum(attribute, validator, options = {})
|
47
|
+
@validations.push(ValidationWithoutEnum.new(attribute, validator, options))
|
48
|
+
end
|
49
|
+
|
38
50
|
def null_validation(datatype, name, column_options)
|
39
51
|
@column_names.push(name.to_s)
|
40
52
|
|
@@ -55,7 +67,7 @@ class GeneratedSchemaValidations::Table
|
|
55
67
|
def bigint(name, column_options = {})
|
56
68
|
null_validation(:bigint, name, column_options)
|
57
69
|
|
58
|
-
|
70
|
+
validates_without_enum name, :numericality, allow_nil: true
|
59
71
|
end
|
60
72
|
|
61
73
|
def integer(name, column_options = {})
|
@@ -71,7 +83,7 @@ class GeneratedSchemaValidations::Table
|
|
71
83
|
options[:less_than_or_equal_to] = integer_range.end
|
72
84
|
end
|
73
85
|
|
74
|
-
|
86
|
+
validates_without_enum name, :numericality, options
|
75
87
|
end
|
76
88
|
|
77
89
|
def datetime(name, column_options = {})
|
@@ -137,6 +149,10 @@ class GeneratedSchemaValidations::Table
|
|
137
149
|
return unless index_options[:unique]
|
138
150
|
return unless names.all? { |name| name.to_s.in?(@column_names) }
|
139
151
|
|
140
|
-
|
152
|
+
if defined?(Rails::Railtie) && (Rails.env.development? || Rails.env.test?) && index_options[:where]
|
153
|
+
@bad_indexes.push(names.map(&:to_s))
|
154
|
+
else
|
155
|
+
@unique_indexes.push(names.map(&:to_s))
|
156
|
+
end
|
141
157
|
end
|
142
158
|
end
|
@@ -11,12 +11,27 @@ module SchemaValidations
|
|
11
11
|
|
12
12
|
included do
|
13
13
|
class_attribute :schema_validations_excluded_columns, default: %i[id created_at updated_at type]
|
14
|
+
class_attribute :schema_validations_called, default: false
|
15
|
+
|
16
|
+
if defined?(Rails::Railtie) && (Rails.env.development? || Rails.env.test?)
|
17
|
+
TracePoint.trace(:end) do |t|
|
18
|
+
if t.self.respond_to?(:schema_validations_called) && t.self < ApplicationRecord &&
|
19
|
+
!t.self.schema_validations_called
|
20
|
+
raise "#{t.self}: schema_validations or skip_schema_validations missing"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
14
24
|
end
|
15
25
|
|
16
26
|
class_methods do
|
17
27
|
def schema_validations(exclude: [], schema_table_name: table_name)
|
18
|
-
self.
|
19
|
-
|
28
|
+
self.schema_validations_called = true
|
29
|
+
self.schema_validations_excluded_columns += exclude.map(&:to_sym)
|
30
|
+
send("dbv_#{schema_table_name}_validations", enums: defined_enums.keys.map(&:to_sym))
|
31
|
+
end
|
32
|
+
|
33
|
+
def skip_schema_validations
|
34
|
+
self.schema_validations_called = true
|
20
35
|
end
|
21
36
|
|
22
37
|
TABLE_VALIDATIONS
|
@@ -36,6 +51,17 @@ module SchemaValidations
|
|
36
51
|
end
|
37
52
|
end
|
38
53
|
|
54
|
+
def bad_uniqueness_validations_for(unique_indexes)
|
55
|
+
unique_indexes.each do |names|
|
56
|
+
names.each do |name|
|
57
|
+
next if name.to_sym.in?(schema_validations_excluded_columns)
|
58
|
+
|
59
|
+
raise "Unique index with where clause is outside the scope of this gem.\n\n" \
|
60
|
+
"You can exclude this column: `schema_validations exclude: [:#{name}]`"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
39
65
|
def belongs_to_uniqueness_validations_for(unique_indexes)
|
40
66
|
reflect_on_all_associations(:belongs_to).each do |association|
|
41
67
|
dbv_uniqueness_validations_for(unique_indexes, foreign_key: association.foreign_key.to_s,
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: generated_schema_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Limbach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: After each migration it generates a file with some validations. Each
|
14
14
|
active record should include this file and can uns generated validations.
|
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
requirements: []
|
56
|
-
rubygems_version: 3.
|
56
|
+
rubygems_version: 3.3.26
|
57
57
|
signing_key:
|
58
58
|
specification_version: 4
|
59
59
|
summary: Generate rails validations from schema.rb file
|