datacaster 3.1.0 → 3.1.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: c42f42112214378de25b126640a7bb578532c1db0ac9fbb73914ef3c08db76e2
4
- data.tar.gz: 0abba877a155d717b0aa2cb605c9214789c839b381492984cb277a1523c5a7a1
3
+ metadata.gz: d563422d81d874ec9f74b4e95a3d24a7a59cf2c3f50ad709a226346616364882
4
+ data.tar.gz: e33c3dffdbff37e5ce711f56c0196e8955379764c98c2429ec391fd33819797d
5
5
  SHA512:
6
- metadata.gz: 4b07733981e971af666cf9a7688cd35f4a32b80eb2e4a53505620425027f5fdd963c7c63717f6888fe700df4ef6c5eb175a07aac7183cb0df29dd1e8b97fe446
7
- data.tar.gz: a596b40d1ebff4e8abc455bdde09c01b0f1499a35cee44daca329885277448cbe6f66268d568a091fa78e83e0da7adf9d9cb09df0c5711d4bc3243d3b909a886
6
+ metadata.gz: d01eb486e4d11954f672e6958803b7ea4c60430c58850f2b4c8e8b16a500229c28a91ecadc45317775f7ff8098c4beddfb0373d415f72e5c917ccb1a93f589c8
7
+ data.tar.gz: c9bdb6f156a7de6eb81fa9cb87dcbf050fd47292d417afd4d085cb64a9e5460c4022b6471de723a1e112cd730a5b12e14c595e9ce248e30ff18fd84625a2cc8a
data/README.md CHANGED
@@ -32,12 +32,12 @@ It is currently used in production in several projects (mainly as request parame
32
32
  - [`integer32(error_key = nil)`](#integer32error_key--nil)
33
33
  - [`non_empty_string(error_key = nil)`](#non_empty_stringerror_key--nil)
34
34
  - [Special types](#special-types)
35
- - [`absent(error_key = nil)`](#absenterror_key--nil)
35
+ - [`absent(error_key = nil, on: nil)`](#absenterror_key--nil-on-nil)
36
36
  - [`any(error_key = nil)`](#anyerror_key--nil)
37
37
  - [`default(default_value, on: nil)`](#defaultdefault_value-on-nil)
38
38
  - [`merge_message_keys(*keys)`](#merge_message_keyskeys)
39
39
  - [`must_be(klass, error_key = nil)`](#must_beklass-error_key--nil)
40
- - [`optional(base)`](#optionalbase)
40
+ - [`optional(base, on: nil)`](#optionalbase-on-nil)
41
41
  - [`pass`](#pass)
42
42
  - [`pass_if(base)`](#pass_ifbase)
43
43
  - [`pick(key)`](#pickkey)
@@ -502,9 +502,16 @@ Returns ValidResult if and only if provided value is a string and is not empty.
502
502
 
503
503
  ### Special types
504
504
 
505
- #### `absent(error_key = nil)`
505
+ #### `absent(error_key = nil, on: nil)`
506
506
 
507
- Returns ValidResult if and only if provided value is `Datacaster.absent` (this is singleton instance). Relevant only for hash schemas (see below). Doesn't transform the value.
507
+ Returns ValidResult if and only if provided value is absent. Relevant only for hash schemas (see below). Transforms the value to `Datacaster.absent`.
508
+
509
+ The value is considered absent:
510
+
511
+ * if the value is `Datacaster.absent` (`on` is disregarded in such case)
512
+ * if `on` is set to a method name to which the value responds and yields truthy
513
+
514
+ Set `on` to `:nil?`, `:empty?` or similar method names.
508
515
 
509
516
  I18n keys: `error_key`, `'.absent'`, `'datacaster.errors.absent'`.
510
517
 
@@ -636,18 +643,25 @@ Returns ValidResult if and only if the value `#is_a?(klass)`. Doesn't transform
636
643
 
637
644
  I18n keys: `error_key`, `'.must_be'`, `'datacaster.errors.must_be'`. Adds `reference` i18n variable, setting it to `klass.name`.
638
645
 
639
- #### `optional(base)`
646
+ #### `optional(base, on: nil)`
647
+
648
+ Returns ValidResult if and only if the value is either absent or passes `base` validation. In the value is absent, transforms it to the `Datacaster.absent`. Otherwise, returns `base` result.
649
+
650
+ Value is considered absent:
651
+
652
+ * if the value is `Datacaster.absent` (`on` is disregarded in such case)
653
+ * if `on` is set to a method name to which the value responds and yields truthy
640
654
 
641
- Returns ValidResult if and only if the value is either `Datacaster.absent` or passes `base` validation. See below documentation on hash schemas for details on `Datacaster.absent`.
655
+ Set `on` to `:nil?`, `:empty?` or similar method names.
642
656
 
643
657
  ```ruby
644
658
  item_with_optional_price =
645
- Datacaster.schema do
646
- hash_schema(
647
- name: string,
648
- price: optional(float)
649
- )
650
- end
659
+ Datacaster.schema do
660
+ hash_schema(
661
+ name: string,
662
+ price: optional(float)
663
+ )
664
+ end
651
665
 
652
666
  item_with_optional_price.(name: "Book", price: 1.23)
653
667
  # => Datacaster::ValidResult({:name=>"Book", :price=>1.23})
@@ -1025,7 +1039,7 @@ restricted_params.(username: "test", is_admin: nil)
1025
1039
 
1026
1040
  More practical case is to include `absent` validator in logical expressions, e.g. `something: absent | string`. If `something` is set to `nil`, this validation will fail, which could be the desired (and hardly achieved by any other validation framework) behavior.
1027
1041
 
1028
- Also, see documentation for [`optional(base)`](#optionalbase) and [`optional_param(base)`](#optional_parambase). If some value becomes `Datacaster.absent` in its chain of validations-transformations, it is removed from the resultant hash (on the same stage where the lack of extra/unchecked keys in the hash is validated):
1042
+ Also, see documentation for [`optional(base)`](#optionalbase-on-nil) and [`optional_param(base)`](#optional_parambase). If some value becomes `Datacaster.absent` in its chain of validations-transformations, it is removed from the resultant hash (on the same stage where the lack of extra/unchecked keys in the hash is validated):
1029
1043
 
1030
1044
  ```ruby
1031
1045
  person =
@@ -67,10 +67,20 @@ module Datacaster
67
67
 
68
68
  # 'Meta' types
69
69
 
70
- def absent(error_key = nil)
70
+ def absent(error_key = nil, on: nil)
71
71
  error_keys = ['.absent', 'datacaster.errors.absent']
72
72
  error_keys.unshift(error_key) if error_key
73
- check { |x| x == Datacaster.absent }.i18n_key(*error_keys)
73
+
74
+ cast do |x|
75
+ if x == Datacaster.absent ||
76
+ (!on.nil? && x.respond_to?(on) && x.public_send(on))
77
+ Datacaster.ValidResult(Datacaster.absent)
78
+ else
79
+ Datacaster.ErrorResult(
80
+ I18nValues::Key.new(error_keys, value: x)
81
+ )
82
+ end
83
+ end
74
84
  end
75
85
 
76
86
  def any(error_key = nil)
@@ -82,7 +92,7 @@ module Datacaster
82
92
  def default(value, on: nil)
83
93
  transform do |x|
84
94
  if x == Datacaster.absent ||
85
- (on && x.respond_to?(on) && x.public_send(on))
95
+ (!on.nil? && x.respond_to?(on) && x.public_send(on))
86
96
  value
87
97
  else
88
98
  x
@@ -153,8 +163,16 @@ module Datacaster
153
163
  check { |x| x.is_a?(klass) }.i18n_key(*error_keys, reference: klass.name)
154
164
  end
155
165
 
156
- def optional(base)
157
- absent | base
166
+ def optional(base, on: nil)
167
+ return absent | base if on == nil
168
+ cast do |x|
169
+ if x == Datacaster.absent ||
170
+ (!on.nil? && x.respond_to?(on) && x.public_send(on))
171
+ Datacaster.ValidResult(Datacaster.absent)
172
+ else
173
+ base.(x)
174
+ end
175
+ end
158
176
  end
159
177
 
160
178
  # Strict types
@@ -1,3 +1,3 @@
1
1
  module Datacaster
2
- VERSION = "3.1.0"
2
+ VERSION = "3.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datacaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev