dry-validation-matchers 1.0.0 → 1.2.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 +5 -5
- data/.github/workflows/ruby.yml +35 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +22 -0
- data/README.md +22 -9
- data/dry-validation-matchers.gemspec +2 -2
- data/lib/dry/validation/matchers/validate_matcher.rb +67 -8
- data/lib/dry/validation/matchers/version.rb +1 -1
- metadata +13 -14
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fb356fd9e7bd319025961ef39a1ea6d0418de27d0898035b3076bd3b3226e74d
|
4
|
+
data.tar.gz: 5cda89f507dfc43126811c8c14fbb742e3a310f06caae942e8c572df84ad1804
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b80ecd94935e51a6cb59458fd27476c12bb76565e1a182325a33e76f5387c52c74f3e3e411a05cec7df184480cddc841187b307bb9fb5e023f0eeea914692a96
|
7
|
+
data.tar.gz: 1299dd2ef1395819995aa17835eeb442452357e36701500893e14dac2551bc01b8ab7beb680527b32b180f1e7d6b48a6632d1642d8f42e20a265373e776f0e52
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ master ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ master ]
|
15
|
+
|
16
|
+
jobs:
|
17
|
+
test:
|
18
|
+
|
19
|
+
runs-on: ubuntu-latest
|
20
|
+
strategy:
|
21
|
+
matrix:
|
22
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
23
|
+
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v2
|
26
|
+
- name: Set up Ruby
|
27
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
28
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
29
|
+
# uses: ruby/setup-ruby@v1
|
30
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
31
|
+
with:
|
32
|
+
ruby-version: ${{ matrix.ruby-version }}
|
33
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
34
|
+
- name: Run tests
|
35
|
+
run: bundle exec rspec
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.6.
|
1
|
+
ruby-2.6.3
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,28 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
6
|
|
7
|
+
## [1.2.2] - 2021-06-15
|
8
|
+
### Fixed
|
9
|
+
- Use long-hand name for types. Not sure when it changed, but the short-hand
|
10
|
+
(i.e. `str?`) no longer works
|
11
|
+
|
12
|
+
## [1.2.1] - 2019-11-22
|
13
|
+
### Fixed
|
14
|
+
- Deprecation messages re BigDecimal()
|
15
|
+
|
16
|
+
## [1.2.0] - 2019-11-17
|
17
|
+
### Added
|
18
|
+
- Add support for checking that the macro is used through the method `macro_use?`
|
19
|
+
- Updated gem dependency `"dry-validation", "~> 1.3"`
|
20
|
+
|
21
|
+
## [1.1.1] - 2019-04-10
|
22
|
+
### Fixed
|
23
|
+
- when `min_size` is set to 1 (and 0 in tests)
|
24
|
+
|
25
|
+
## [1.1.0] - 2019-04-10
|
26
|
+
### Added
|
27
|
+
- `min_size` validation
|
28
|
+
|
7
29
|
## [1.0.0] - 2019-04-04
|
8
30
|
### Added
|
9
31
|
- Remove (I know, under "added") activesupport requirement
|
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# Dry::Validation::Matchers
|
2
2
|
|
3
|
-
[](https://travis-ci.org/bloom-solutions/dry-validation-matchers)
|
4
|
-
|
5
3
|
RSpec matchers for [Dry::Validation](dry-rb.org/gems/dry-validation).
|
6
4
|
|
7
5
|
## Installation
|
@@ -23,25 +21,40 @@ Or install it yourself as:
|
|
23
21
|
## Usage
|
24
22
|
|
25
23
|
```ruby
|
26
|
-
RSpec.describe "
|
24
|
+
RSpec.describe "Integration with RSpec", type: %i[dry_validation] do
|
25
|
+
|
27
26
|
subject(:schema_class) do
|
28
|
-
Class.new(Dry::Validation::
|
29
|
-
|
27
|
+
Class.new(Dry::Validation::Contract) do
|
28
|
+
register_macro(:email) do
|
29
|
+
key.failure('must_be_a_valid_email') if value.is_a?(String) &&
|
30
|
+
!value.match?(/\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i)
|
31
|
+
end
|
32
|
+
|
33
|
+
register_macro(:precision) do |macro:|
|
34
|
+
num = macro.args[0]
|
35
|
+
key.failure("cant_have_more_than_#{num}_decimal_numbers") if value && value.to_s.split('.').last.size > num
|
36
|
+
end
|
37
|
+
|
38
|
+
params do
|
30
39
|
required(:username).filled
|
31
40
|
required(:first_name)
|
32
|
-
required(:age).filled(:
|
33
|
-
required(:last_name).filled(:
|
34
|
-
required(:rank).value(included_in?: %w(sarge chief))
|
41
|
+
required(:age).filled(:integer)
|
42
|
+
required(:last_name).filled(:string)
|
35
43
|
optional(:mobile).filled
|
36
44
|
optional(:email)
|
45
|
+
optional(:decimal_value)
|
37
46
|
end
|
47
|
+
|
48
|
+
rule(:email).validate(:email)
|
49
|
+
rule(:decimal_value).validate(precision: 5)
|
38
50
|
end
|
39
51
|
end
|
40
52
|
|
41
53
|
it { is_expected.to validate(:username, :required).filled }
|
42
54
|
it { is_expected.to validate(:mobile, :optional).filled }
|
43
55
|
it { is_expected.to validate(:email, :optional) }
|
44
|
-
it { is_expected.to validate(:
|
56
|
+
it { is_expected.to validate(:email, :optional).macro_use?(:email) }
|
57
|
+
it { is_expected.to validate(:decimal_value, :optional).macro_use?(precision: 5) }
|
45
58
|
end
|
46
59
|
```
|
47
60
|
|
@@ -30,8 +30,8 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
|
-
spec.add_runtime_dependency "dry-validation"
|
33
|
+
spec.add_runtime_dependency "dry-validation", "~> 1.3"
|
34
34
|
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
-
spec.add_development_dependency "bundler", "~>
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
36
36
|
spec.add_development_dependency "rake", "~> 10.0"
|
37
37
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module Dry::Validation::Matchers
|
2
2
|
class ValidateMatcher
|
3
3
|
|
4
|
-
DEFAULT_TYPE = :
|
4
|
+
DEFAULT_TYPE = :string
|
5
5
|
TYPE_ERRORS = {
|
6
|
-
|
6
|
+
string: {
|
7
7
|
test_value: "str",
|
8
8
|
message: "must be a string",
|
9
9
|
},
|
10
|
-
|
10
|
+
integer: {
|
11
11
|
test_value: 43,
|
12
12
|
message: "must be an integer",
|
13
13
|
},
|
@@ -16,7 +16,7 @@ module Dry::Validation::Matchers
|
|
16
16
|
message: "must be a float",
|
17
17
|
},
|
18
18
|
decimal: {
|
19
|
-
test_value: BigDecimal
|
19
|
+
test_value: BigDecimal("41.5"),
|
20
20
|
message: "must be a decimal",
|
21
21
|
},
|
22
22
|
bool: {
|
@@ -50,7 +50,9 @@ module Dry::Validation::Matchers
|
|
50
50
|
@acceptance = acceptance
|
51
51
|
@type = DEFAULT_TYPE
|
52
52
|
@value_rules = []
|
53
|
+
@macro_usage_params = []
|
53
54
|
@check_filled = false
|
55
|
+
@check_macro = false
|
54
56
|
end
|
55
57
|
|
56
58
|
def description
|
@@ -59,6 +61,7 @@ module Dry::Validation::Matchers
|
|
59
61
|
|
60
62
|
validation_details_message = []
|
61
63
|
validation_details_message << "filled with #{@type}" if @check_filled
|
64
|
+
validation_details_message << "macro usage `#{@macro_usage_params.to_s}`" if @check_macro
|
62
65
|
|
63
66
|
unless validation_details_message.empty?
|
64
67
|
@desc << " ("
|
@@ -76,6 +79,7 @@ module Dry::Validation::Matchers
|
|
76
79
|
|
77
80
|
validation_details_message = []
|
78
81
|
validation_details_message << "filled with #{@type}" if @check_filled
|
82
|
+
validation_details_message << "macro usage `#{@macro_usage_params.to_s}`" if @check_macro
|
79
83
|
|
80
84
|
unless validation_details_message.empty?
|
81
85
|
@desc << " ("
|
@@ -87,10 +91,10 @@ module Dry::Validation::Matchers
|
|
87
91
|
end
|
88
92
|
|
89
93
|
def matches?(schema_or_schema_class)
|
90
|
-
if schema_or_schema_class.is_a?(Dry::Validation::
|
94
|
+
if schema_or_schema_class.is_a?(Dry::Validation::Contract)
|
91
95
|
schema = schema_or_schema_class
|
92
96
|
elsif schema_or_schema_class.is_a?(Class) &&
|
93
|
-
schema_or_schema_class.ancestors.include?(Dry::Validation::
|
97
|
+
schema_or_schema_class.ancestors.include?(Dry::Validation::Contract)
|
94
98
|
|
95
99
|
schema = schema_or_schema_class.new
|
96
100
|
else
|
@@ -103,10 +107,11 @@ module Dry::Validation::Matchers
|
|
103
107
|
check_required_or_optional!(schema) &&
|
104
108
|
check_filled!(schema) &&
|
105
109
|
check_filled_with_type!(schema) &&
|
106
|
-
check_value!(schema)
|
110
|
+
check_value!(schema) &&
|
111
|
+
check_macro_usage!(schema)
|
107
112
|
end
|
108
113
|
|
109
|
-
def filled(type
|
114
|
+
def filled(type=DEFAULT_TYPE)
|
110
115
|
@check_filled = true
|
111
116
|
@type = type
|
112
117
|
self
|
@@ -117,6 +122,12 @@ module Dry::Validation::Matchers
|
|
117
122
|
self
|
118
123
|
end
|
119
124
|
|
125
|
+
def macro_use?(macro_params)
|
126
|
+
@check_macro = true
|
127
|
+
@macro_usage_params = macro_params
|
128
|
+
self
|
129
|
+
end
|
130
|
+
|
120
131
|
private
|
121
132
|
|
122
133
|
def check_required_or_optional!(schema)
|
@@ -184,6 +195,31 @@ module Dry::Validation::Matchers
|
|
184
195
|
false
|
185
196
|
end
|
186
197
|
|
198
|
+
def check_value_min_size!(schema, rule)
|
199
|
+
predicate = rule[0]
|
200
|
+
min_size = rule[1]
|
201
|
+
|
202
|
+
expected_error_message = "size cannot be less than #{min_size}"
|
203
|
+
|
204
|
+
result = schema.(@attr => "a" * (min_size+1))
|
205
|
+
error_messages = result.errors[@attr]
|
206
|
+
no_error_when_over = error_messages.nil? ||
|
207
|
+
!error_messages.include?(expected_error_message)
|
208
|
+
|
209
|
+
result = schema.(@attr => "a" * (min_size))
|
210
|
+
error_messages = result.errors[@attr]
|
211
|
+
no_error_when_exact = error_messages.nil? ||
|
212
|
+
!error_messages.include?(expected_error_message)
|
213
|
+
|
214
|
+
result = schema.(@attr => "a" * (min_size-1))
|
215
|
+
error_messages = result.errors[@attr]
|
216
|
+
error_when_below = (min_size-1).zero? ||
|
217
|
+
!error_messages.nil? &&
|
218
|
+
error_messages.include?(expected_error_message)
|
219
|
+
|
220
|
+
no_error_when_over && no_error_when_exact && error_when_below
|
221
|
+
end
|
222
|
+
|
187
223
|
def check_value_max_size!(schema, rule)
|
188
224
|
predicate = rule[0]
|
189
225
|
max_size = rule[1]
|
@@ -203,6 +239,29 @@ module Dry::Validation::Matchers
|
|
203
239
|
error_when_over && no_error_when_within
|
204
240
|
end
|
205
241
|
|
242
|
+
def check_macro_usage!(schema)
|
243
|
+
return true if @macro_usage_params.empty?
|
244
|
+
|
245
|
+
is_present = false
|
246
|
+
|
247
|
+
schema.rules.each do |obj|
|
248
|
+
next if obj.keys.first != @attr
|
249
|
+
|
250
|
+
value = if @macro_usage_params.is_a?(Hash) && obj.macros.flatten.count > 1
|
251
|
+
obj.macros.to_h.map { |k, v| [k, v.first] }.to_h
|
252
|
+
else
|
253
|
+
obj.macros.flatten.first
|
254
|
+
end
|
255
|
+
|
256
|
+
if value == @macro_usage_params
|
257
|
+
is_present = true
|
258
|
+
break
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
is_present
|
263
|
+
end
|
264
|
+
|
206
265
|
def type_error_messages
|
207
266
|
type_error_messages = []
|
208
267
|
TYPE_ERRORS.each_pair do |type, hash|
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-validation-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ramon Tayag
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-validation
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '1.3'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '2.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '2.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,10 +73,10 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- ".github/workflows/ruby.yml"
|
76
77
|
- ".gitignore"
|
77
78
|
- ".rspec"
|
78
79
|
- ".ruby-version"
|
79
|
-
- ".travis.yml"
|
80
80
|
- CHANGELOG.md
|
81
81
|
- CODE_OF_CONDUCT.md
|
82
82
|
- Gemfile
|
@@ -96,7 +96,7 @@ licenses:
|
|
96
96
|
- MIT
|
97
97
|
metadata:
|
98
98
|
allowed_push_host: https://rubygems.org
|
99
|
-
post_install_message:
|
99
|
+
post_install_message:
|
100
100
|
rdoc_options: []
|
101
101
|
require_paths:
|
102
102
|
- lib
|
@@ -111,9 +111,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
111
|
- !ruby/object:Gem::Version
|
112
112
|
version: '0'
|
113
113
|
requirements: []
|
114
|
-
|
115
|
-
|
116
|
-
signing_key:
|
114
|
+
rubygems_version: 3.1.6
|
115
|
+
signing_key:
|
117
116
|
specification_version: 4
|
118
117
|
summary: RSpec matchers for dry-validation
|
119
118
|
test_files: []
|