validates_by_schema 0.1.1 → 0.2.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.
- data/README.md +20 -8
- data/lib/validates_by_schema.rb +8 -29
- data/lib/validates_by_schema/validation_option.rb +76 -0
- data/lib/validates_by_schema/version.rb +1 -1
- metadata +35 -2
data/README.md
CHANGED
@@ -3,22 +3,34 @@
|
|
3
3
|
|
4
4
|
Automatic validation based on your database schema column types and limits. Keep your code DRY by inferring column validations from table properties!
|
5
5
|
|
6
|
-
##
|
6
|
+
## Install
|
7
7
|
|
8
8
|
1. Add it to your Gemfile:
|
9
9
|
```ruby
|
10
|
-
gem "validates_by_schema"
|
10
|
+
gem "validates_by_schema"
|
11
11
|
```
|
12
12
|
|
13
13
|
2. Then `bundle`
|
14
14
|
|
15
|
-
3.
|
15
|
+
3. Call it from your ActiveRecord model:
|
16
16
|
```ruby
|
17
17
|
class Widget < ActiveRecord::Base
|
18
|
-
|
18
|
+
validates_by_schema
|
19
19
|
end
|
20
20
|
```
|
21
21
|
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
You can also whitelist/blacklist columns with :only and :except options
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
validates_by_schema only: [:body, :description]
|
28
|
+
```
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
validates_by_schema except: [:name, :title]
|
32
|
+
```
|
33
|
+
|
22
34
|
## Example
|
23
35
|
|
24
36
|
Say you had a table setup like this:
|
@@ -30,11 +42,11 @@ create_table "widgets", :force => true do |t|
|
|
30
42
|
end
|
31
43
|
```
|
32
44
|
|
33
|
-
Then these validations run when your model `
|
45
|
+
Then these validations run when your model `validates_by_schema`:
|
34
46
|
```ruby
|
35
|
-
validates :wheel_count, presence: true, numericality {allow_nil: false, less_than: 2147483647}
|
36
|
-
validates :thickness, numericality: {allow_nil: true,
|
47
|
+
validates :wheel_count, presence: true, numericality {allow_nil: false, greater_than: -2147483647, less_than: 2147483647}
|
48
|
+
validates :thickness, numericality: {allow_nil: true, less_than_or_equal_to: 0.999, greater_than_or_equal_to: -0.999}
|
37
49
|
validates :color, length: {allow_nil: true, maximum: 255}
|
38
50
|
```
|
39
51
|
|
40
|
-
This
|
52
|
+
This is tested with postgres, mysql, and sqlite3. It will most likely work with any other database Rails supports.
|
data/lib/validates_by_schema.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
module ValidatesBySchema
|
2
|
+
autoload :ValidationOption, 'validates_by_schema/validation_option'
|
3
|
+
|
2
4
|
extend ActiveSupport::Concern
|
3
5
|
|
4
6
|
module ClassMethods
|
5
7
|
def validates_by_schema options={}
|
6
8
|
return unless table_exists?
|
7
|
-
# Don't auto
|
9
|
+
# Don't auto validate primary keys or timestamps
|
8
10
|
columns = self.columns.reject(&:primary)
|
9
11
|
columns.reject!{|c| ['updated_at', 'created_at'].include? c.name}
|
10
12
|
|
@@ -18,36 +20,13 @@ module ValidatesBySchema
|
|
18
20
|
end
|
19
21
|
|
20
22
|
columns.each do |c|
|
21
|
-
|
22
|
-
|
23
|
-
val_hash = {presence: !c.null, numericality: {
|
24
|
-
only_integer: true, allow_nil: c.null}}
|
25
|
-
if c.limit
|
26
|
-
max = (2 ** (8 * c.limit)) / 2
|
27
|
-
val_hash[:numericality][:less_than] = max
|
28
|
-
val_hash[:numericality][:greater_than] = -max
|
29
|
-
end
|
30
|
-
validates c.name, val_hash
|
31
|
-
when :decimal
|
32
|
-
maximum = 10.0**(c.precision-c.scale) - 10.0**(-c.scale)
|
33
|
-
validates c.name, presence: !c.null, numericality: {
|
34
|
-
allow_nil: c.null, greater_than_or_equal_to: -maximum,
|
35
|
-
less_than_or_equal_to: maximum }
|
36
|
-
when :float
|
37
|
-
validates c.name, presence: !c.null,
|
38
|
-
numericality: { allow_nil: c.null }
|
39
|
-
when :string, :text
|
40
|
-
validates c.name, presence: !c.null, length: { maximum: c.limit.to_i,
|
41
|
-
allow_nil: c.null}
|
42
|
-
when :boolean
|
43
|
-
validates c.name, inclusion: { in: [true, false],
|
44
|
-
allow_nil: c.null }
|
45
|
-
when :date, :datetime, :time
|
46
|
-
validates c.name, presence: !c.null
|
47
|
-
end
|
23
|
+
vo = ValidationOption.new(c).to_hash
|
24
|
+
validates c.name, vo if vo.present?
|
48
25
|
end
|
49
26
|
end
|
50
27
|
end
|
51
28
|
end
|
52
29
|
|
53
|
-
|
30
|
+
ActiveSupport.on_load :active_record do
|
31
|
+
include ValidatesBySchema
|
32
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class ValidatesBySchema::ValidationOption
|
2
|
+
# column here must be an ActiveRecord column
|
3
|
+
# i.e. MyARModel.columns.first
|
4
|
+
attr_accessor :column
|
5
|
+
|
6
|
+
def initialize(column)
|
7
|
+
@column = column
|
8
|
+
end
|
9
|
+
|
10
|
+
def presence?
|
11
|
+
presence && column.type != :boolean
|
12
|
+
end
|
13
|
+
|
14
|
+
def presence
|
15
|
+
!column.null
|
16
|
+
end
|
17
|
+
|
18
|
+
def numericality?
|
19
|
+
[:integer, :decimal, :float].include? column.type
|
20
|
+
end
|
21
|
+
|
22
|
+
def numericality
|
23
|
+
numericality = {}
|
24
|
+
if column.type == :integer
|
25
|
+
numericality[:only_integer] = true
|
26
|
+
if integer_max
|
27
|
+
numericality[:less_than] = integer_max
|
28
|
+
numericality[:greater_than] = -integer_max
|
29
|
+
end
|
30
|
+
elsif column.type == :decimal
|
31
|
+
numericality[:less_than_or_equal_to] = decimal_max
|
32
|
+
numericality[:greater_than_or_equal_to] = -decimal_max
|
33
|
+
end
|
34
|
+
numericality[:allow_nil] = column.null
|
35
|
+
numericality
|
36
|
+
end
|
37
|
+
|
38
|
+
def length?
|
39
|
+
[:string, :text].include?(column.type) && column.limit
|
40
|
+
end
|
41
|
+
|
42
|
+
def length
|
43
|
+
{:maximum => column.limit, :allow_nil => column.null }
|
44
|
+
end
|
45
|
+
|
46
|
+
def inclusion?
|
47
|
+
column.type == :boolean
|
48
|
+
end
|
49
|
+
|
50
|
+
def inclusion
|
51
|
+
{:in => [true, false], :allow_nil => column.null}
|
52
|
+
end
|
53
|
+
|
54
|
+
def integer_max
|
55
|
+
(2 ** (8 * column.limit)) / 2 if column.limit
|
56
|
+
end
|
57
|
+
|
58
|
+
def decimal_max
|
59
|
+
10.0**(column.precision-column.scale) - 10.0**(-column.scale)
|
60
|
+
end
|
61
|
+
|
62
|
+
def string_max
|
63
|
+
column.limit
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_hash
|
67
|
+
[:presence, :numericality, :length, :inclusion].inject({}) do |h,k|
|
68
|
+
send(:"#{k}?") ? h.merge(k => send(k)) : h
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_s
|
73
|
+
to_hash.inspect
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -75,6 +75,38 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pg
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: mysql2
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
78
110
|
description: Keep your code DRY by inferring column validations from table properties!
|
79
111
|
Automagically validate presence, length, numericality, and inclusion of ActiveRecord
|
80
112
|
backed columns.
|
@@ -85,6 +117,7 @@ extensions: []
|
|
85
117
|
extra_rdoc_files: []
|
86
118
|
files:
|
87
119
|
- lib/tasks/validates_by_schema_tasks.rake
|
120
|
+
- lib/validates_by_schema/validation_option.rb
|
88
121
|
- lib/validates_by_schema/version.rb
|
89
122
|
- lib/validates_by_schema.rb
|
90
123
|
- MIT-LICENSE
|