easy_params 0.3.0 → 0.3.1

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: d5a19f19d20a42ef6a6ba3e606298c3dbf696a22c9dcdc9e42a8ffe48e1e67db
4
- data.tar.gz: fce70967d59989d5e650289483d5437f4ebb80793d49b3a446d252796017117c
3
+ metadata.gz: 38358431f3a00f55f6a46aaa72941c1cbe0fe5f81d6a2fd898a019e09c2cf069
4
+ data.tar.gz: 218c09133f6cc7220545fe2fbdf34deefc8a6ab4941d6ef9e729d264f6ecca93
5
5
  SHA512:
6
- metadata.gz: 3f690f1a17df11da94e8f707c99f97854aec48ca890320c0345159286c4e10ac19448a5b3a6557936c6076b4bf4e886cd51841ef394b3a205c91626a680b598f
7
- data.tar.gz: c43521492ce73961d7ff7fb562ba7b8ba944f03c928a8c68298f8f6f168c21ab698d4acdf3fe2dd073296bec99418cf14adffd8446cc751541b32136b452e972
6
+ metadata.gz: 8674d76046f76b6ce08e0e1a7c2a1d8c12f55bf549b4ea580fcfcf1b6a5b50cda6caa6621b684fd94a41a8bad3a25044de5ea2b8b2f68e7496e6dec00e86d3ea
7
+ data.tar.gz: 268b513fff84704447102ee24f1cfef76e271c73a7ab7379eef1fc1093307c69d462d0679eb387d85bc90031e588d399b53ba2c233c94829e335efe4abf7eee6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_params (0.3.0)
4
+ easy_params (0.3.1)
5
5
  activemodel (>= 3.2, < 8)
6
6
  dry-struct (~> 1.4)
7
7
  dry-types (~> 1.5)
@@ -9,9 +9,9 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activemodel (7.0.4.3)
13
- activesupport (= 7.0.4.3)
14
- activesupport (7.0.4.3)
12
+ activemodel (7.0.6)
13
+ activesupport (= 7.0.6)
14
+ activesupport (7.0.6)
15
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
16
  i18n (>= 1.6, < 2)
17
17
  minitest (>= 5.1)
data/README.md CHANGED
@@ -51,10 +51,10 @@ end
51
51
  Validation messages for nested attributes will look like this.
52
52
  ```ruby
53
53
  {
54
- :"sections/0/id"=>an_instance_of(Array),
55
- :"sections/0/post/id"=>an_instance_of(Array),
56
- :"post/id"=>an_instance_of(Array),
57
- :"post/sections/0/id"=>an_instance_of(Array)
54
+ :"sections[0].id"=>an_instance_of(Array),
55
+ :"sections[0].post.id"=>an_instance_of(Array),
56
+ :"post.id"=>an_instance_of(Array),
57
+ :"post.sections[0].id"=>an_instance_of(Array)
58
58
  }
59
59
  ```
60
60
  Optionally you can use more compact form
@@ -22,44 +22,45 @@ module EasyParams
22
22
  private
23
23
 
24
24
  def validate_nested
25
- attributes.each(&run_nested_validations)
25
+ attributes.each do |_, value|
26
+ case value
27
+ when *EasyParams::Types::ARRAY_OF_STRUCTS_TYPES_LIST
28
+ value.each(&:valid?)
29
+ when *EasyParams::Types::STRUCT_TYPES_LIST
30
+ value.valid?
31
+ end
32
+ end
33
+ attributes.each(&aggregate_nested_errors)
26
34
  end
27
35
 
28
- def run_nested_validations
36
+ def aggregate_nested_errors
29
37
  proc do |attr_name, value, array_index, error_key_prefix|
30
38
  case value
31
- when Array
39
+ when *EasyParams::Types::ARRAY_OF_STRUCTS_TYPES_LIST
32
40
  value.each.with_index do |element, i|
33
- run_nested_validations[attr_name, element, i, error_key_prefix]
41
+ aggregate_nested_errors[attr_name, element, "[#{i}]", error_key_prefix]
34
42
  end
35
- when self.class.struct
36
- handle_struct_validation(value, error_key_prefix, attr_name, array_index)
43
+ when *EasyParams::Types::STRUCT_TYPES_LIST
44
+ handle_nested_errors(value, error_key_prefix, attr_name, array_index)
37
45
  end
38
46
  end
39
47
  end
40
48
 
41
- def handle_struct_validation(value, error_key_prefix, attr_name, array_index)
42
- if value.invalid?
43
- error_key_components = [error_key_prefix, attr_name, array_index]
44
- attr_error_key_prefix = error_key_components.compact.join('/')
45
- add_errors_on_top_level(value, attr_error_key_prefix)
46
- end
47
- value.attributes.each do |nested_attr_name, nested_value|
48
- run_nested_validations[nested_attr_name, nested_value, nil, attr_error_key_prefix]
49
- end
49
+ def handle_nested_errors(value, error_key_prefix, attr_name, array_index)
50
+ return if value.errors.blank?
51
+
52
+ error_key_components = [error_key_prefix, attr_name, array_index]
53
+ attr_error_key_prefix = error_key_components.compact.join('.').gsub(/\.\[(\d+)\]/, '[\1]')
54
+ add_errors_on_top_level(value, attr_error_key_prefix)
50
55
  end
51
56
 
52
57
  if defined? ActiveModel::Error
53
58
  def add_errors_on_top_level(value, attr_error_key_prefix)
54
- value.errors.each do |error|
55
- next unless error.options[:message]
56
-
57
- errors.add("#{attr_error_key_prefix}/#{error.attribute}", error.options[:message])
58
- end
59
+ value.errors.each { |error| errors.add("#{attr_error_key_prefix}.#{error.attribute}", error.message) }
59
60
  end
60
61
  else
61
62
  def add_errors_on_top_level(value, attr_error_key_prefix)
62
- value.errors.each { |key, message| errors.add("#{attr_error_key_prefix}/#{key}", message) }
63
+ value.errors.each { |key, message| errors.add("#{attr_error_key_prefix}.#{key}", message) }
63
64
  end
64
65
  end
65
66
  end
@@ -9,9 +9,12 @@ module EasyParams
9
9
  Float = Dry::Types['params.float'].optional.meta(omittable: true).default(nil)
10
10
  Bool = Dry::Types['strict.bool'].optional.meta(omittable: true).default(nil)
11
11
  String = Dry::Types['string'].optional.meta(omittable: true).default(nil)
12
- Array = Dry::Types['array'].of(Struct).meta(omittable: true).default([])
12
+ Array = Dry::Types['array'].meta(omittable: true).default([])
13
13
  Date = Dry::Types['params.date'].optional.meta(omittable: true).default(nil)
14
14
  DateTime = Dry::Types['params.date_time'].optional.meta(omittable: true).default(nil)
15
15
  Time = Dry::Types['params.time'].optional.meta(omittable: true).default(nil)
16
+
17
+ STRUCT_TYPES_LIST = [Struct, StructDSL].freeze
18
+ ARRAY_OF_STRUCTS_TYPES_LIST = [Array.of(Struct), Array.of(StructDSL)].freeze
16
19
  end
17
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyParams
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-24 00:00:00.000000000 Z
11
+ date: 2023-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel