generated_schema_validations 0.3.0 → 0.4.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: 2d7371cf78c33a1ff16867c837724f1dc51bb7145a83b8d6cf90a8f160382fb3
4
- data.tar.gz: 7544fd35cf7dd4daef98a4223c2208bca9a4c74482bbc503290122e216bada3c
3
+ metadata.gz: 6695669fe5683c58ba70f717c3025fff4e51baa0684842603cede227bfed0b83
4
+ data.tar.gz: 80b7ee3f1f023b56704928fc02ddd1930028449ab4162bb9ab48fd0c8ef3049b
5
5
  SHA512:
6
- metadata.gz: 752b409572e541277f7c4313e1c4168223eb740cd4c5a5cdbef4245d1b66953f9f8c34303f25dba4754f00d198d78b59dd2fc1b2704d8d968a1b877cdcbcd295
7
- data.tar.gz: 6495630671eb4abb5e8a2f439f0780a27251b1538b092694593ffecba75a5183bd3934ef65615e4b087098eb311f1fce36e339bb920b601a2d5d235a9dbbd12e
6
+ metadata.gz: 48749688026b1328a620e9d820367e5dc2adbe294a38265aecc3e1d3755d227671b86cf104788d8dce9165d87ca0b5d9ba8a2e4387a02b0538ce830dc1ed7196
7
+ data.tar.gz: fa8fbd3809827765f766962bd928157a058ef68b41e0dc414de706d178b2b64884cf519d4069d54493dcfe851d95f33e571036cfec2f10b413b8bbaf5a455227
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,14 @@ You can watch changes on `schema_validations.rb` to understand the generated val
95
97
 
96
98
  ## Changelog
97
99
 
100
+ ### 0.4
101
+
102
+ * JSON and JSONB fields can have an empty hash and still be considered present
103
+
104
+ ### 0.3.1
105
+
106
+ * Add exclusion for enum fields
107
+
98
108
  ### 0.3.0
99
109
 
100
110
  * Check usage of schema_validations (see #3)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'generated_schema_validations'
5
- spec.version = '0.3.0'
5
+ spec.version = '0.4.0'
6
6
  spec.authors = ['Georg Limbach']
7
7
  spec.email = ['georg.limbach@lichtbit.com']
8
8
 
@@ -7,6 +7,12 @@ 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)
@@ -22,7 +28,7 @@ class GeneratedSchemaValidations::Table
22
28
 
23
29
  def to_s
24
30
  string = "\n"
25
- string += "def dbv_#{table_name}_validations\n"
31
+ string += "def dbv_#{table_name}_validations(enums: [])\n"
26
32
  if @possible_belongs_to_not_null_columns.present?
27
33
  string += " belongs_to_presence_validations_for(#{@possible_belongs_to_not_null_columns.inspect})\n"
28
34
  end
@@ -37,6 +43,10 @@ class GeneratedSchemaValidations::Table
37
43
  @validations.push(Validation.new(attribute, validator, options))
38
44
  end
39
45
 
46
+ def validates_without_enum(attribute, validator, options = {})
47
+ @validations.push(ValidationWithoutEnum.new(attribute, validator, options))
48
+ end
49
+
40
50
  def null_validation(datatype, name, column_options)
41
51
  @column_names.push(name.to_s)
42
52
 
@@ -45,6 +55,8 @@ class GeneratedSchemaValidations::Table
45
55
  @possible_belongs_to_not_null_columns.push(name.to_sym) if datatype.in?(%i[bigint integer uuid])
46
56
  if datatype == :boolean
47
57
  validates name, :inclusion, in: [true, false], message: :blank
58
+ elsif datatype.in?(%i[json jsonb])
59
+ validates name, :exclusion, in: [nil], message: :blank
48
60
  else
49
61
  validates name, :presence
50
62
  end
@@ -57,7 +69,7 @@ class GeneratedSchemaValidations::Table
57
69
  def bigint(name, column_options = {})
58
70
  null_validation(:bigint, name, column_options)
59
71
 
60
- validates name, :numericality, allow_nil: true
72
+ validates_without_enum name, :numericality, allow_nil: true
61
73
  end
62
74
 
63
75
  def integer(name, column_options = {})
@@ -73,7 +85,7 @@ class GeneratedSchemaValidations::Table
73
85
  options[:less_than_or_equal_to] = integer_range.end
74
86
  end
75
87
 
76
- validates name, :numericality, options
88
+ validates_without_enum name, :numericality, options
77
89
  end
78
90
 
79
91
  def datetime(name, column_options = {})
@@ -27,7 +27,7 @@ module SchemaValidations
27
27
  def schema_validations(exclude: [], schema_table_name: table_name)
28
28
  self.schema_validations_called = true
29
29
  self.schema_validations_excluded_columns += exclude.map(&:to_sym)
30
- send("dbv_#{schema_table_name}_validations")
30
+ send("dbv_#{schema_table_name}_validations", enums: defined_enums.keys.map(&:to_sym))
31
31
  end
32
32
 
33
33
  def skip_schema_validations
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georg Limbach
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-20 00:00:00.000000000 Z
11
+ date: 2025-10-21 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.
@@ -38,7 +38,7 @@ licenses:
38
38
  metadata:
39
39
  homepage_uri: https://github.com/Lichtbit/generated_schema_validations
40
40
  source_code_uri: https://github.com/Lichtbit/generated_schema_validations
41
- post_install_message:
41
+ post_install_message:
42
42
  rdoc_options: []
43
43
  require_paths:
44
44
  - lib
@@ -53,8 +53,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  requirements: []
56
- rubygems_version: 3.0.8
57
- signing_key:
56
+ rubygems_version: 3.3.26
57
+ signing_key:
58
58
  specification_version: 4
59
59
  summary: Generate rails validations from schema.rb file
60
60
  test_files: []