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 +4 -4
- data/Gemfile.lock +4 -4
- data/README.md +4 -4
- data/lib/easy_params/base.rb +22 -21
- data/lib/easy_params/types.rb +4 -1
- data/lib/easy_params/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38358431f3a00f55f6a46aaa72941c1cbe0fe5f81d6a2fd898a019e09c2cf069
|
4
|
+
data.tar.gz: 218c09133f6cc7220545fe2fbdf34deefc8a6ab4941d6ef9e729d264f6ecca93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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.
|
13
|
-
activesupport (= 7.0.
|
14
|
-
activesupport (7.0.
|
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
|
55
|
-
:"sections
|
56
|
-
:"post
|
57
|
-
:"post
|
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
|
data/lib/easy_params/base.rb
CHANGED
@@ -22,44 +22,45 @@ module EasyParams
|
|
22
22
|
private
|
23
23
|
|
24
24
|
def validate_nested
|
25
|
-
attributes.each
|
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
|
36
|
+
def aggregate_nested_errors
|
29
37
|
proc do |attr_name, value, array_index, error_key_prefix|
|
30
38
|
case value
|
31
|
-
when
|
39
|
+
when *EasyParams::Types::ARRAY_OF_STRUCTS_TYPES_LIST
|
32
40
|
value.each.with_index do |element, i|
|
33
|
-
|
41
|
+
aggregate_nested_errors[attr_name, element, "[#{i}]", error_key_prefix]
|
34
42
|
end
|
35
|
-
when
|
36
|
-
|
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
|
42
|
-
if value.
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
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}
|
63
|
+
value.errors.each { |key, message| errors.add("#{attr_error_key_prefix}.#{key}", message) }
|
63
64
|
end
|
64
65
|
end
|
65
66
|
end
|
data/lib/easy_params/types.rb
CHANGED
@@ -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'].
|
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
|
data/lib/easy_params/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2023-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|