dry-validation-matchers 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c5fd64bd7d180697dc6d6618b349a71029ea939c52b446b4411c3b373cb7e43
4
- data.tar.gz: f62e21ef82e36bf102092dff264be1931c664e048813c8784405436a4b6ceb11
3
+ metadata.gz: b0ee4b633e751ffaa7fdafb364f58990e9ebf09a952b87075a77ad36c84eb278
4
+ data.tar.gz: 2cbeb8982b547830905e1b4e7783e7bad557138a47c0ec5160e25c66d1ac95bc
5
5
  SHA512:
6
- metadata.gz: 3c0144666861c44c01c0b9fab6e842f9be15a0eace36f8b5e300fb2b9272e49ea069c9a753018dad008a215891befc99e03c53f4ec66933ba90c98c588215d8b
7
- data.tar.gz: 1bcf615053e306e858e7852486acb8ae53fe72120d802ea68cd689d24028727d0a1db8ae828dd8beb49b7908ea758e3c093b878b6cb3506670b37a25cc2f2413
6
+ metadata.gz: d9e3c8cb7a06630dbc433284bb9e8e720a5b80b4ad11b59c7f134e406b5355fef5e7ff1586785d815a83e78b931ac69424ba1ecba9f17b5ead299f408dab8328
7
+ data.tar.gz: 442e5b9d788d9f01422d4d56d457e65bb98d89360f1dc5133f8550410c183443287bf01944f1448794961c30607e88a8be82af3386429621fc685013f8bdca2e
@@ -1 +1 @@
1
- ruby-2.6.2
1
+ ruby-2.6.3
@@ -1,7 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.6.2
4
+ - 2.6.3
5
5
  before_install:
6
6
  - gem update --system
7
7
  - gem install bundler -v 2.0.1
@@ -4,6 +4,11 @@ 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.0] - 2019-11-17
8
+ ### Added
9
+ - Add support for checking that the macro is used through the method `macro_use?`
10
+ - Updated gem dependency `"dry-validation", "~> 1.3"`
11
+
7
12
  ## [1.1.1] - 2019-04-10
8
13
  ### Fixed
9
14
  - when `min_size` is set to 1 (and 0 in tests)
data/README.md CHANGED
@@ -23,25 +23,40 @@ Or install it yourself as:
23
23
  ## Usage
24
24
 
25
25
  ```ruby
26
- RSpec.describe "SomeSchema class", type: [:dry_validation]
26
+ RSpec.describe "Integration with RSpec", type: %i[dry_validation] do
27
+
27
28
  subject(:schema_class) do
28
- Class.new(Dry::Validation::Schema) do
29
- define! do
29
+ Class.new(Dry::Validation::Contract) do
30
+ register_macro(:email) do
31
+ key.failure('must_be_a_valid_email') if value.is_a?(String) &&
32
+ !value.match?(/\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i)
33
+ end
34
+
35
+ register_macro(:precision) do |macro:|
36
+ num = macro.args[0]
37
+ key.failure("cant_have_more_than_#{num}_decimal_numbers") if value && value.to_s.split('.').last.size > num
38
+ end
39
+
40
+ params do
30
41
  required(:username).filled
31
42
  required(:first_name)
32
43
  required(:age).filled(:int?)
33
44
  required(:last_name).filled(:str?)
34
- required(:rank).value(included_in?: %w(sarge chief))
35
45
  optional(:mobile).filled
36
46
  optional(:email)
47
+ optional(:decimal_value)
37
48
  end
49
+
50
+ rule(:email).validate(:email)
51
+ rule(:decimal_value).validate(precision: 5)
38
52
  end
39
53
  end
40
54
 
41
55
  it { is_expected.to validate(:username, :required).filled }
42
56
  it { is_expected.to validate(:mobile, :optional).filled }
43
57
  it { is_expected.to validate(:email, :optional) }
44
- it { is_expected.to validate(:rank, :optional).value(included_in: %w(sarge chief)) }
58
+ it { is_expected.to validate(:email, :optional).macro_use?(:email) }
59
+ it { is_expected.to validate(:decimal_value, :optional).macro_use?(precision: 5) }
45
60
  end
46
61
  ```
47
62
 
@@ -30,7 +30,7 @@ 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
35
  spec.add_development_dependency "bundler", "~> 2.0"
36
36
  spec.add_development_dependency "rake", "~> 10.0"
@@ -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::Schema)
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::Schema)
97
+ schema_or_schema_class.ancestors.include?(Dry::Validation::Contract)
94
98
 
95
99
  schema = schema_or_schema_class.new
96
100
  else
@@ -103,7 +107,8 @@ 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
114
  def filled(type=:str)
@@ -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)
@@ -228,6 +239,29 @@ module Dry::Validation::Matchers
228
239
  error_when_over && no_error_when_within
229
240
  end
230
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
+
231
265
  def type_error_messages
232
266
  type_error_messages = []
233
267
  TYPE_ERRORS.each_pair do |type, hash|
@@ -1,7 +1,7 @@
1
1
  module Dry
2
2
  module Validation
3
3
  module Matchers
4
- VERSION = "1.1.1"
4
+ VERSION = "1.2.0"
5
5
  end
6
6
  end
7
7
  end
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.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramon Tayag
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-10 00:00:00.000000000 Z
11
+ date: 2019-11-18 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: '0'
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: '0'
26
+ version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -111,8 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
113
  requirements: []
114
- rubyforge_project:
115
- rubygems_version: 2.7.8
114
+ rubygems_version: 3.0.6
116
115
  signing_key:
117
116
  specification_version: 4
118
117
  summary: RSpec matchers for dry-validation