validated_object 2.3.3 → 2.3.5
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 +129 -110
- data/lib/validated_object/simplified_api.rb +16 -8
- data/lib/validated_object/version.rb +1 -1
- data/lib/validated_object.rb +217 -9
- metadata +130 -116
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a0c1ab12abd25150e5cc29a8ece7710437556f706fac4da1ce3c006b5f573f0c
|
|
4
|
+
data.tar.gz: 0efb1b45fad53fcaeb5f998a703ddcc3dab0320892cb8e670850b01b859c8c78
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c8ab4a77d0c39f3c5663468967c0458b2fe90f5bafc6ecb41c65dca797e8b8764a6b5ea55bff8236c8e07c8ab05df963e409dccecad7861af7e2547631ce1c7c
|
|
7
|
+
data.tar.gz: 4d3b2a09e120e95c7e33cdf58e543973236040c741bff08eb9b58e0a35c8201f1f5d55462ef224a5c6cc2ff5a653019a091700b46352fe7cc3d475038b5c99fd
|
data/README.md
CHANGED
|
@@ -1,196 +1,215 @@
|
|
|
1
1
|
[](https://badge.fury.io/rb/validated_object)
|
|
2
2
|
# ValidatedObject
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
**Self-validating Plain Old Ruby Objects** using Rails validations.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
Create Ruby objects that validate themselves on instantiation, with clear error messages and flexible type checking including union types.
|
|
7
|
+
|
|
8
|
+
Result: Invalid objects can't be instantiated. Illegal states are unrepresentable.
|
|
7
9
|
|
|
8
10
|
```ruby
|
|
9
11
|
class Person < ValidatedObject::Base
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
validates_attr :name, presence: true
|
|
13
|
+
validates_attr :email, format: { with: URI::MailTo::EMAIL_REGEXP }
|
|
12
14
|
end
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
you = Person.new(name: '') # => ArgumentError: "Name can't be blank"
|
|
16
|
+
Person.new(name: 'Alice', email: 'alice@example.com') # ✓ Valid
|
|
17
|
+
Person.new(name: '', email: 'invalid') # ✗ ArgumentError: "Name can't be blank; Email is invalid"
|
|
17
18
|
```
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
## Goals
|
|
23
|
-
|
|
24
|
-
* Very readable error messages
|
|
25
|
-
* Clean, minimal syntax
|
|
26
|
-
|
|
27
|
-
This is a small layer around
|
|
28
|
-
[ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates). (About 25 lines of code.) So if you know how to use [Rails Validations](https://guides.rubyonrails.org/active_record_validations.html), you're good to go. I wrote this to help with CSV data imports and [website structured data](https://github.com/public-law/schema-dot-org).
|
|
20
|
+
## Key Features
|
|
29
21
|
|
|
22
|
+
* **Union Types**: `union(String, Integer)` for flexible type validation
|
|
23
|
+
* **Array Element Validation**: `type: [String]` ensures arrays contain specific types
|
|
24
|
+
* **Clear Error Messages**: Descriptive validation failures for debugging
|
|
25
|
+
* **Rails Validations**: Full ActiveModel::Validations support
|
|
26
|
+
* **Immutable Objects**: Read-only attributes with validation
|
|
30
27
|
|
|
31
|
-
|
|
28
|
+
Perfect for data imports, API boundaries, and structured data generation.
|
|
32
29
|
|
|
30
|
+
## Basic Usage
|
|
33
31
|
|
|
34
|
-
###
|
|
35
|
-
|
|
36
|
-
All of the [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) are available, plus a new one, `TypeValidator`.
|
|
32
|
+
### Simple Validation
|
|
37
33
|
|
|
38
34
|
```ruby
|
|
39
35
|
class Dog < ValidatedObject::Base
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
# Plain old Rails
|
|
44
|
-
validates :name, presence: true
|
|
45
|
-
|
|
46
|
-
# A new type-validation if you'd like to use it
|
|
47
|
-
validates :birthday, type: Date, allow_nil: true # Strongly typed but optional
|
|
36
|
+
validates_attr :name, presence: true
|
|
37
|
+
validates_attr :age, type: Integer, allow_nil: true
|
|
48
38
|
end
|
|
39
|
+
|
|
40
|
+
spot = Dog.new(name: 'Spot', age: 3)
|
|
41
|
+
spot.valid? # => true
|
|
49
42
|
```
|
|
50
43
|
|
|
51
|
-
|
|
44
|
+
### Type Validation
|
|
52
45
|
|
|
53
46
|
```ruby
|
|
54
|
-
class
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
validates :birthday, type: Date, allow_nil: true
|
|
47
|
+
class Document < ValidatedObject::Base
|
|
48
|
+
validates_attr :title, type: String
|
|
49
|
+
validates_attr :published_at, type: Date, allow_nil: true
|
|
50
|
+
validates_attr :active, type: Boolean
|
|
59
51
|
end
|
|
60
52
|
```
|
|
61
53
|
|
|
62
|
-
|
|
54
|
+
The `Boolean` type accepts `true` or `false` values.
|
|
63
55
|
|
|
64
|
-
|
|
56
|
+
## Union Types
|
|
65
57
|
|
|
66
|
-
|
|
58
|
+
Union types allow attributes to accept multiple possible types:
|
|
59
|
+
|
|
60
|
+
### Basic Union Types
|
|
67
61
|
|
|
68
62
|
```ruby
|
|
69
|
-
class
|
|
70
|
-
|
|
71
|
-
validates_attr :
|
|
63
|
+
class Article < ValidatedObject::Base
|
|
64
|
+
# ID can be either a String or Integer
|
|
65
|
+
validates_attr :id, type: union(String, Integer)
|
|
66
|
+
|
|
67
|
+
# Status can be specific symbol values
|
|
68
|
+
validates_attr :status, type: union(:draft, :published, :archived)
|
|
72
69
|
end
|
|
73
|
-
```
|
|
74
70
|
|
|
75
|
-
|
|
71
|
+
article = Article.new(id: "abc123", status: :published) # ✓ String ID
|
|
72
|
+
article = Article.new(id: 42, status: :draft) # ✓ Integer ID
|
|
73
|
+
Article.new(id: 3.14, status: :invalid) # ✗ ArgumentError
|
|
74
|
+
```
|
|
76
75
|
|
|
77
|
-
|
|
76
|
+
### Mixed Type and Array Unions
|
|
78
77
|
|
|
79
78
|
```ruby
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
79
|
+
class Post < ValidatedObject::Base
|
|
80
|
+
# Author can be a Person object or Organization object
|
|
81
|
+
validates_attr :author, type: union(Person, Organization)
|
|
82
|
+
|
|
83
|
+
# Tags can be a single string or array of strings
|
|
84
|
+
validates_attr :tags, type: union(String, [String])
|
|
85
|
+
|
|
86
|
+
# Categories supports multiple formats
|
|
87
|
+
validates_attr :categories, type: union(String, [String], [Category])
|
|
88
|
+
end
|
|
84
89
|
|
|
90
|
+
# All of these are valid:
|
|
91
|
+
Post.new(author: person, tags: "ruby")
|
|
92
|
+
Post.new(author: org, tags: ["ruby", "rails"])
|
|
93
|
+
Post.new(author: person, categories: [category1, category2])
|
|
94
|
+
```
|
|
85
95
|
|
|
86
|
-
###
|
|
96
|
+
### Schema.org Example
|
|
87
97
|
|
|
88
|
-
|
|
98
|
+
Union types are perfect for Schema.org structured data:
|
|
89
99
|
|
|
90
100
|
```ruby
|
|
91
|
-
|
|
92
|
-
|
|
101
|
+
class Organization < ValidatedObject::Base
|
|
102
|
+
# Address can be text or structured PostalAddress
|
|
103
|
+
validates_attr :address, type: union(String, PostalAddress)
|
|
104
|
+
|
|
105
|
+
# Founder can be Person or Organization
|
|
106
|
+
validates_attr :founder, type: union(Person, Organization, [Person], [Organization])
|
|
107
|
+
|
|
108
|
+
# Logo can be URL string or ImageObject
|
|
109
|
+
validates_attr :logo, type: union(String, ImageObject)
|
|
110
|
+
end
|
|
93
111
|
```
|
|
94
112
|
|
|
95
|
-
|
|
113
|
+
## Array Element Validation
|
|
96
114
|
|
|
97
|
-
|
|
98
|
-
### Instantiating and automatically validating
|
|
115
|
+
Validate that arrays contain specific types:
|
|
99
116
|
|
|
100
117
|
```ruby
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
# We can also explicitly test for validity because all of
|
|
107
|
-
# ActiveModel::Validations is available.
|
|
108
|
-
spot.valid? # => true
|
|
118
|
+
class Playlist < ValidatedObject::Base
|
|
119
|
+
validates_attr :songs, type: [Song] # Array of Song objects
|
|
120
|
+
validates_attr :genres, type: [String] # Array of strings
|
|
121
|
+
validates_attr :ratings, type: [Integer] # Array of integers
|
|
122
|
+
end
|
|
109
123
|
|
|
110
|
-
|
|
111
|
-
|
|
124
|
+
playlist = Playlist.new(
|
|
125
|
+
songs: [song1, song2],
|
|
126
|
+
genres: ["rock", "jazz"],
|
|
127
|
+
ratings: [4, 5, 3]
|
|
128
|
+
)
|
|
112
129
|
```
|
|
113
130
|
|
|
114
|
-
|
|
131
|
+
## Alternative Syntax
|
|
115
132
|
|
|
116
|
-
|
|
117
|
-
used to test an instance, plus the custom `check_validations!` convenience method:
|
|
133
|
+
You can also use the standard Rails `validates` method:
|
|
118
134
|
|
|
119
135
|
```ruby
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
136
|
+
class Dog < ValidatedObject::Base
|
|
137
|
+
attr_reader :name, :birthday
|
|
138
|
+
|
|
139
|
+
validates :name, presence: true
|
|
140
|
+
validates :birthday, type: union(Date, DateTime), allow_nil: true
|
|
141
|
+
end
|
|
123
142
|
```
|
|
124
143
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
144
|
+
## Error Messages
|
|
145
|
+
|
|
146
|
+
ValidatedObject provides clear, actionable error messages:
|
|
128
147
|
|
|
129
148
|
```ruby
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
```
|
|
149
|
+
doc = Document.new(title: 123, status: :invalid)
|
|
150
|
+
# => ArgumentError: Title is a Integer, not a String; Status is a Symbol, not one of :draft, :published, :archived
|
|
133
151
|
|
|
152
|
+
post = Post.new(tags: [123, "valid"])
|
|
153
|
+
# => ArgumentError: Tags is a Array, not one of String, Array of String
|
|
154
|
+
```
|
|
134
155
|
|
|
135
|
-
|
|
156
|
+
## Use Cases
|
|
136
157
|
|
|
137
|
-
|
|
158
|
+
### Data Import Validation
|
|
138
159
|
|
|
139
160
|
```ruby
|
|
140
|
-
# Import
|
|
141
|
-
|
|
142
|
-
csv
|
|
161
|
+
# Import CSV with validation
|
|
162
|
+
valid_records = []
|
|
163
|
+
CSV.foreach('data.csv', headers: true) do |row|
|
|
143
164
|
begin
|
|
144
|
-
|
|
165
|
+
valid_records << Person.new(row.to_h)
|
|
145
166
|
rescue ArgumentError => e
|
|
146
|
-
logger.warn
|
|
167
|
+
logger.warn "Invalid row: #{e.message}"
|
|
147
168
|
end
|
|
148
169
|
end
|
|
149
170
|
```
|
|
150
171
|
|
|
151
|
-
|
|
152
|
-
error log lists unparseable rows with good info for tracking down problems in
|
|
153
|
-
the data.
|
|
172
|
+
### API Response Objects
|
|
154
173
|
|
|
155
|
-
|
|
174
|
+
```ruby
|
|
175
|
+
class ApiResponse < ValidatedObject::Base
|
|
176
|
+
validates_attr :data, type: union(Hash, [Hash])
|
|
177
|
+
validates_attr :status, type: union(:success, :error)
|
|
178
|
+
validates_attr :message, type: String, allow_nil: true
|
|
179
|
+
end
|
|
180
|
+
```
|
|
156
181
|
|
|
157
|
-
|
|
182
|
+
### Schema.org Structured Data
|
|
183
|
+
|
|
184
|
+
The [Schema.org gem](https://github.com/public-law/schema-dot-org) uses ValidatedObject for type-safe structured data generation.
|
|
158
185
|
|
|
159
186
|
## Installation
|
|
160
187
|
|
|
161
|
-
Add
|
|
188
|
+
Add to your Gemfile:
|
|
162
189
|
|
|
163
190
|
```ruby
|
|
164
191
|
gem 'validated_object'
|
|
165
192
|
```
|
|
166
193
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
Or install it yourself as:
|
|
172
|
-
|
|
173
|
-
$ gem install validated_object
|
|
174
|
-
|
|
175
|
-
|
|
194
|
+
Then run:
|
|
195
|
+
```bash
|
|
196
|
+
bundle install
|
|
197
|
+
```
|
|
176
198
|
|
|
177
199
|
## Development
|
|
178
200
|
|
|
179
|
-
|
|
180
|
-
to install dependencies. Then, run `rake spec` to run the tests. You can also
|
|
181
|
-
run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
201
|
+
After checking out the repo:
|
|
182
202
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
203
|
+
```bash
|
|
204
|
+
bin/setup # Install dependencies
|
|
205
|
+
bundle exec rspec # Run tests
|
|
206
|
+
bin/console # Interactive prompt
|
|
207
|
+
```
|
|
188
208
|
|
|
189
209
|
## Contributing
|
|
190
210
|
|
|
191
|
-
Bug reports and pull requests
|
|
192
|
-
|
|
211
|
+
Bug reports and pull requests welcome on GitHub.
|
|
193
212
|
|
|
194
213
|
## License
|
|
195
214
|
|
|
196
|
-
|
|
215
|
+
Available as open source under the [MIT License](http://opensource.org/licenses/MIT).
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'active_support/concern'
|
|
2
4
|
|
|
3
|
-
# Enable a simplified API for the common case of
|
|
4
|
-
# read-only ValidatedObjects.
|
|
5
5
|
module ValidatedObject
|
|
6
|
+
# Enable a simplified API for the common case of
|
|
7
|
+
# read-only ValidatedObjects.
|
|
6
8
|
module SimplifiedApi
|
|
7
9
|
extend ActiveSupport::Concern
|
|
8
10
|
|
|
@@ -19,7 +21,6 @@ module ValidatedObject
|
|
|
19
21
|
validates(*args, **kwargs, &block)
|
|
20
22
|
end
|
|
21
23
|
|
|
22
|
-
# Alias for validated_attr for compatibility with test usage.
|
|
23
24
|
def validates_attr(attribute, *options, **kwargs)
|
|
24
25
|
attr_reader attribute
|
|
25
26
|
|
|
@@ -27,16 +28,23 @@ module ValidatedObject
|
|
|
27
28
|
type_val = kwargs.delete(:type)
|
|
28
29
|
element_type = kwargs.delete(:element_type)
|
|
29
30
|
|
|
31
|
+
# Handle Union types - pass them through directly
|
|
32
|
+
if type_val.is_a?(ValidatedObject::Base::Union)
|
|
33
|
+
opts = { type: { with: type_val } }
|
|
34
|
+
validates attribute, opts.merge(kwargs)
|
|
30
35
|
# Parse Array[ElementType] syntax
|
|
31
|
-
|
|
36
|
+
elsif type_val.is_a?(Array) && type_val.length == 1 && type_val[0].is_a?(Class)
|
|
32
37
|
# This handles Array[Comment] syntax
|
|
33
38
|
element_type = type_val[0]
|
|
34
39
|
type_val = Array
|
|
40
|
+
opts = { type: { with: type_val } }
|
|
41
|
+
opts[:type][:element_type] = element_type if element_type
|
|
42
|
+
validates attribute, opts.merge(kwargs)
|
|
43
|
+
else
|
|
44
|
+
opts = { type: { with: type_val } }
|
|
45
|
+
opts[:type][:element_type] = element_type if element_type
|
|
46
|
+
validates attribute, opts.merge(kwargs)
|
|
35
47
|
end
|
|
36
|
-
|
|
37
|
-
opts = { type: { with: type_val } }
|
|
38
|
-
opts[:type][:element_type] = element_type if element_type
|
|
39
|
-
validates attribute, opts.merge(kwargs)
|
|
40
48
|
else
|
|
41
49
|
validates attribute, *options, **kwargs
|
|
42
50
|
end
|
data/lib/validated_object.rb
CHANGED
|
@@ -54,6 +54,21 @@ module ValidatedObject
|
|
|
54
54
|
class Boolean
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
+
# A private class definition for union types.
|
|
58
|
+
# Stores multiple allowed types for validation.
|
|
59
|
+
# Created via ValidatedObject::Base.union(*types)
|
|
60
|
+
#
|
|
61
|
+
# @example
|
|
62
|
+
# validates :id, type: union(String, Integer)
|
|
63
|
+
# validates :data, type: union(Hash, [Hash])
|
|
64
|
+
class Union
|
|
65
|
+
attr_reader :types
|
|
66
|
+
|
|
67
|
+
def initialize(*types)
|
|
68
|
+
@types = types
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
57
72
|
# Instantiate and validate a new object.
|
|
58
73
|
# @example
|
|
59
74
|
# maru = Dog.new(birthday: Date.today, name: 'Maru')
|
|
@@ -61,7 +76,7 @@ module ValidatedObject
|
|
|
61
76
|
# @raise [ArgumentError] if the object is not valid at the
|
|
62
77
|
# end of initialization or `attributes` is not a Hash.
|
|
63
78
|
def initialize(attributes = EMPTY_HASH)
|
|
64
|
-
|
|
79
|
+
assign_declared_attributes attributes
|
|
65
80
|
check_validations!
|
|
66
81
|
end
|
|
67
82
|
|
|
@@ -76,6 +91,7 @@ module ValidatedObject
|
|
|
76
91
|
# @raise [ArgumentError] if any validations fail.
|
|
77
92
|
# @return [ValidatedObject::Base] the receiver
|
|
78
93
|
def check_validations!
|
|
94
|
+
return self if !defined?(@errors) && self.class.fast_valid?(self)
|
|
79
95
|
raise ArgumentError, errors.full_messages.join('; ') if invalid?
|
|
80
96
|
|
|
81
97
|
self
|
|
@@ -99,6 +115,14 @@ module ValidatedObject
|
|
|
99
115
|
validation_options = options
|
|
100
116
|
expected_class = validation_options[:with]
|
|
101
117
|
|
|
118
|
+
# Support union types
|
|
119
|
+
if expected_class.is_a?(Union)
|
|
120
|
+
return if validate_union_type(record, attribute, value, expected_class, validation_options)
|
|
121
|
+
|
|
122
|
+
save_union_error(record, attribute, value, expected_class, validation_options)
|
|
123
|
+
return
|
|
124
|
+
end
|
|
125
|
+
|
|
102
126
|
# Support type: Array, element_type: ElementType
|
|
103
127
|
if expected_class == Array && validation_options[:element_type]
|
|
104
128
|
return save_error(record, attribute, value, validation_options) unless value.is_a?(Array)
|
|
@@ -116,8 +140,34 @@ module ValidatedObject
|
|
|
116
140
|
save_error(record, attribute, value, validation_options)
|
|
117
141
|
end
|
|
118
142
|
|
|
143
|
+
FAST_PATH_OPTIONS = %i[with element_type allow_nil message].freeze
|
|
144
|
+
|
|
145
|
+
# @return [Boolean] whether {#acceptable?} fully decides this validation
|
|
146
|
+
def fast_path?
|
|
147
|
+
options.each_key.all? { |key| FAST_PATH_OPTIONS.include?(key) }
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The allocation-free equivalent of {#validate_each} reporting success.
|
|
151
|
+
# @return [Boolean]
|
|
152
|
+
def acceptable?(value)
|
|
153
|
+
return true if value.nil? && options[:allow_nil]
|
|
154
|
+
|
|
155
|
+
acceptable_type?(value, options[:with])
|
|
156
|
+
end
|
|
157
|
+
|
|
119
158
|
private
|
|
120
159
|
|
|
160
|
+
def acceptable_type?(value, expected_class)
|
|
161
|
+
return validate_union_type(nil, nil, value, expected_class, options) if expected_class.is_a?(Union)
|
|
162
|
+
|
|
163
|
+
element_type = options[:element_type]
|
|
164
|
+
if expected_class == Array && element_type
|
|
165
|
+
return value.is_a?(Array) && validate_array_element_type(value, element_type)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
pseudo_boolean?(expected_class, value) || expected_class?(expected_class, value)
|
|
169
|
+
end
|
|
170
|
+
|
|
121
171
|
def pseudo_boolean?(expected_class, value)
|
|
122
172
|
expected_class == Boolean && boolean?(value)
|
|
123
173
|
end
|
|
@@ -134,6 +184,50 @@ module ValidatedObject
|
|
|
134
184
|
record.errors.add attribute,
|
|
135
185
|
validation_options[:message] || "is a #{value.class}, not a #{validation_options[:with]}"
|
|
136
186
|
end
|
|
187
|
+
|
|
188
|
+
def validate_union_type(_record, _attribute, value, union, _validation_options)
|
|
189
|
+
union.types.any? do |type_spec|
|
|
190
|
+
if type_spec.is_a?(Array) && type_spec.length == 1
|
|
191
|
+
# Handle [ElementType] syntax within union
|
|
192
|
+
validate_array_element_type(value, type_spec[0])
|
|
193
|
+
elsif type_spec.is_a?(Class) || type_spec == Boolean
|
|
194
|
+
# Handle class types (String, Integer, etc.) and pseudo-boolean
|
|
195
|
+
pseudo_boolean?(type_spec, value) || expected_class?(type_spec, value)
|
|
196
|
+
else
|
|
197
|
+
# Handle literal values (symbols, strings, numbers, etc.)
|
|
198
|
+
value == type_spec
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def validate_array_element_type(value, element_type)
|
|
204
|
+
return false unless value.is_a?(Array)
|
|
205
|
+
|
|
206
|
+
value.all? { |el| el.is_a?(element_type) }
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def save_union_error(record, attribute, value, union, validation_options)
|
|
210
|
+
return if validation_options[:message]
|
|
211
|
+
|
|
212
|
+
type_names = union.types.map do |type_spec|
|
|
213
|
+
if type_spec.is_a?(Array) && type_spec.length == 1
|
|
214
|
+
"Array of #{type_spec[0]}"
|
|
215
|
+
elsif type_spec.is_a?(Class) || type_spec == Boolean
|
|
216
|
+
type_spec.to_s
|
|
217
|
+
else
|
|
218
|
+
# For literal values like :active, show them as-is
|
|
219
|
+
type_spec.inspect
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
message = if type_names.length == 1
|
|
224
|
+
"is a #{value.class}, not one of #{type_names.first}"
|
|
225
|
+
else
|
|
226
|
+
"is a #{value.class}, not one of #{type_names.join(', ')}"
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
record.errors.add attribute, message
|
|
230
|
+
end
|
|
137
231
|
end
|
|
138
232
|
|
|
139
233
|
# Register the TypeValidator with ActiveModel so `type:` validation option works
|
|
@@ -146,17 +240,131 @@ module ValidatedObject
|
|
|
146
240
|
validates(*args, **kwargs, &block)
|
|
147
241
|
end
|
|
148
242
|
|
|
149
|
-
|
|
243
|
+
# Create a union type specification for validation
|
|
244
|
+
# @param types [Array] The types that are allowed
|
|
245
|
+
# @return [Union] A union type specification
|
|
246
|
+
# @example
|
|
247
|
+
# validates :id, type: union(String, Integer)
|
|
248
|
+
# validates :data, type: union(Hash, [Hash])
|
|
249
|
+
def self.union(*types)
|
|
250
|
+
Union.new(*types)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# The allocation-free equivalent of a successful `validates_format_of`
|
|
254
|
+
# with a literal `with:` Regexp.
|
|
255
|
+
#
|
|
256
|
+
# @api private
|
|
257
|
+
class FormatCheck
|
|
258
|
+
OPTIONS = %i[with allow_nil allow_blank message].freeze
|
|
150
259
|
|
|
151
|
-
|
|
152
|
-
|
|
260
|
+
def self.from(validator)
|
|
261
|
+
options = validator.options
|
|
262
|
+
return nil unless options[:with].is_a?(Regexp) && options.each_key.all? { |key| OPTIONS.include?(key) }
|
|
263
|
+
|
|
264
|
+
new(options[:with], allow_nil: options[:allow_nil], allow_blank: options[:allow_blank])
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def initialize(regexp, allow_nil:, allow_blank:)
|
|
268
|
+
@regexp = regexp
|
|
269
|
+
@allow_nil = allow_nil
|
|
270
|
+
@allow_blank = allow_blank
|
|
271
|
+
freeze
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def acceptable?(value)
|
|
275
|
+
return true if value.nil? && @allow_nil
|
|
276
|
+
return true if @allow_blank && value.blank?
|
|
277
|
+
|
|
278
|
+
@regexp.match?(value.to_s)
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# Checks the receiver without ActiveModel's per-call allocations when
|
|
283
|
+
# every validation is a plain `type:` check. A `false` result only means
|
|
284
|
+
# the full ActiveModel run must decide, and build the error messages.
|
|
285
|
+
#
|
|
286
|
+
# @api private
|
|
287
|
+
# @return [Boolean]
|
|
288
|
+
def self.fast_valid?(record)
|
|
289
|
+
plan = fast_path_plan
|
|
290
|
+
return false unless plan
|
|
291
|
+
|
|
292
|
+
plan.all? do |attribute, check|
|
|
293
|
+
check.acceptable?(record.read_attribute_for_validation(attribute))
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# The instance variable for an attribute key, only when the key names a
|
|
298
|
+
# public reader declared below {Base}. Anything else, such as `exit!`,
|
|
299
|
+
# `errors` or `instance_eval`, is rejected without being called.
|
|
300
|
+
#
|
|
301
|
+
# @api private
|
|
302
|
+
# @return [Symbol]
|
|
303
|
+
# @raise [NoMethodError] if the key is not a declared attribute.
|
|
304
|
+
def self.instance_variable_for(key)
|
|
305
|
+
attribute_ivars[key] || register_attribute(key)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def self.fast_path_plan
|
|
309
|
+
callbacks = _validate_callbacks
|
|
310
|
+
return @fast_path_plan if callbacks.equal?(@fast_path_callbacks)
|
|
311
|
+
|
|
312
|
+
@fast_path_plan = build_fast_path_plan(callbacks)
|
|
313
|
+
@fast_path_callbacks = callbacks
|
|
314
|
+
@fast_path_plan
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def self.build_fast_path_plan(callbacks)
|
|
318
|
+
return nil unless stock_validation_run?
|
|
319
|
+
|
|
320
|
+
validators = callbacks.map(&:filter)
|
|
321
|
+
checks = validators.map { |validator| fast_check_for(validator) }
|
|
322
|
+
return nil unless checks.all?
|
|
323
|
+
|
|
324
|
+
validators.zip(checks).flat_map do |validator, check|
|
|
325
|
+
validator.attributes.map { |attribute| [attribute, check].freeze }
|
|
326
|
+
end.freeze
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def self.fast_check_for(validator)
|
|
330
|
+
case validator
|
|
331
|
+
when TypeValidator then validator if validator.fast_path?
|
|
332
|
+
when ActiveModel::Validations::FormatValidator then FormatCheck.from(validator)
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def self.stock_validation_run?
|
|
337
|
+
%i[valid? invalid? run_validations!].all? do |name|
|
|
338
|
+
instance_method(name).owner.equal?(ActiveModel::Validations)
|
|
339
|
+
end
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def self.attribute_ivars
|
|
343
|
+
@attribute_ivars ||= {}
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
def self.register_attribute(key)
|
|
347
|
+
name = key.to_sym
|
|
348
|
+
raise NoMethodError.new("undefined attribute '#{name}' for #{self}", name) unless attribute_reader?(name)
|
|
349
|
+
|
|
350
|
+
attribute_ivars[key] = :"@#{name}"
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
def self.attribute_reader?(name)
|
|
354
|
+
public_method_defined?(name) && !Base.ancestors.include?(instance_method(name).owner)
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
private_class_method :fast_path_plan, :build_fast_path_plan, :attribute_ivars,
|
|
358
|
+
:register_attribute, :attribute_reader?, :stock_validation_run?,
|
|
359
|
+
:fast_check_for
|
|
360
|
+
|
|
361
|
+
private
|
|
153
362
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
send variable_name.to_sym
|
|
363
|
+
def assign_declared_attributes(attributes)
|
|
364
|
+
raise TypeError, "#{attributes} is not a hash" unless attributes.is_a?(Hash)
|
|
157
365
|
|
|
158
|
-
|
|
159
|
-
instance_variable_set
|
|
366
|
+
attributes.each do |key, value|
|
|
367
|
+
instance_variable_set self.class.instance_variable_for(key), value
|
|
160
368
|
end
|
|
161
369
|
end
|
|
162
370
|
end
|
metadata
CHANGED
|
@@ -1,143 +1,157 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: validated_object
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.3.
|
|
4
|
+
version: 2.3.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Robb Shecter
|
|
7
|
+
- Robb Shecter
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
|
-
- !ruby/object:Gem::Dependency
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
- !ruby/object:Gem::
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
- !ruby/object:Gem::
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rake
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 12.3.3
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: 12.3.3
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: rspec
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
-
|
|
33
|
+
- ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: 3.9.0
|
|
36
|
+
type: :development
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
-
|
|
41
|
+
- ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: 3.9.0
|
|
44
|
+
- !ruby/object:Gem::Dependency
|
|
45
|
+
name: rubocop
|
|
46
|
+
requirement: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
-
|
|
49
|
+
- ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: 1.80.0
|
|
52
|
+
type: :development
|
|
53
|
+
prerelease: false
|
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
-
|
|
57
|
+
- ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: 1.80.0
|
|
60
|
+
- !ruby/object:Gem::Dependency
|
|
61
|
+
name: rubocop-rake
|
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
-
|
|
65
|
+
- ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: 0.0.0
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
-
|
|
73
|
+
- ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 0.0.0
|
|
76
|
+
- !ruby/object:Gem::Dependency
|
|
77
|
+
name: rubocop-rspec
|
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
-
|
|
81
|
+
- ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: 0.0.0
|
|
84
|
+
type: :development
|
|
85
|
+
prerelease: false
|
|
86
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
-
|
|
89
|
+
- ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: 0.0.0
|
|
92
|
+
- !ruby/object:Gem::Dependency
|
|
93
|
+
name: activemodel
|
|
94
|
+
requirement: !ruby/object:Gem::Requirement
|
|
95
|
+
requirements:
|
|
96
|
+
-
|
|
97
|
+
- ">="
|
|
98
|
+
- !ruby/object:Gem::Version
|
|
99
|
+
version: 3.2.21
|
|
100
|
+
type: :runtime
|
|
101
|
+
prerelease: false
|
|
102
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
103
|
+
requirements:
|
|
104
|
+
-
|
|
105
|
+
- ">="
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: 3.2.21
|
|
96
108
|
description: A small wrapper around ActiveModel Validations.
|
|
97
109
|
email:
|
|
98
|
-
- robb@public.law
|
|
110
|
+
- robb@public.law
|
|
99
111
|
executables: []
|
|
100
112
|
extensions: []
|
|
101
113
|
extra_rdoc_files: []
|
|
102
114
|
files:
|
|
103
|
-
-
|
|
104
|
-
-
|
|
105
|
-
-
|
|
106
|
-
-
|
|
107
|
-
-
|
|
108
|
-
- CLAUDE.md
|
|
109
|
-
- Gemfile
|
|
110
|
-
- HISTORY.md
|
|
111
|
-
- LICENSE.txt
|
|
112
|
-
- README.md
|
|
113
|
-
- Rakefile
|
|
114
|
-
- bin/console
|
|
115
|
-
- bin/setup
|
|
116
|
-
- lib/validated_object.rb
|
|
117
|
-
- lib/validated_object/simplified_api.rb
|
|
118
|
-
- lib/validated_object/version.rb
|
|
119
|
-
- script/demo.rb
|
|
120
|
-
- validated_object.gemspec
|
|
121
|
-
homepage: https://github.com/public-law/validated_object
|
|
115
|
+
- .github/workflows/ruby.yml
|
|
116
|
+
- .gitignore
|
|
117
|
+
- .rspec
|
|
118
|
+
- .rubocop.yml
|
|
119
|
+
- .travis.yml
|
|
120
|
+
- CLAUDE.md
|
|
121
|
+
- Gemfile
|
|
122
|
+
- HISTORY.md
|
|
123
|
+
- LICENSE.txt
|
|
124
|
+
- README.md
|
|
125
|
+
- Rakefile
|
|
126
|
+
- bin/console
|
|
127
|
+
- bin/setup
|
|
128
|
+
- lib/validated_object.rb
|
|
129
|
+
- lib/validated_object/simplified_api.rb
|
|
130
|
+
- lib/validated_object/version.rb
|
|
131
|
+
- script/demo.rb
|
|
132
|
+
- validated_object.gemspec
|
|
133
|
+
homepage: "https://github.com/public-law/validated_object"
|
|
122
134
|
licenses:
|
|
123
|
-
- MIT
|
|
135
|
+
- MIT
|
|
124
136
|
metadata:
|
|
125
|
-
allowed_push_host: https://rubygems.org
|
|
137
|
+
allowed_push_host: "https://rubygems.org"
|
|
126
138
|
rdoc_options: []
|
|
127
139
|
require_paths:
|
|
128
|
-
- lib
|
|
140
|
+
- lib
|
|
129
141
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
130
142
|
requirements:
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
143
|
+
-
|
|
144
|
+
- ">="
|
|
145
|
+
- !ruby/object:Gem::Version
|
|
146
|
+
version: "3.1"
|
|
134
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
148
|
requirements:
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
149
|
+
-
|
|
150
|
+
- ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: "0"
|
|
139
153
|
requirements: []
|
|
140
|
-
rubygems_version:
|
|
154
|
+
rubygems_version: 4.1.0
|
|
141
155
|
specification_version: 4
|
|
142
156
|
summary: Self-validating plain Ruby objects.
|
|
143
157
|
test_files: []
|