datacaster 6.0.1 → 6.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0072ba53600fb794462c13dba4fe31c9134aac67e5a7687f0e91e17483cf10b1
4
- data.tar.gz: b35c2f8b667d884bcf641be2d79747c65a68b97a7da20abc718776b3fe532af6
3
+ metadata.gz: f66df22c1ea20054ae2b16ea8ccbd778b8aac71e7898bc763036e3c4f9ba50da
4
+ data.tar.gz: bad95a5182fdbbefe72c5c8f5b724c433f26cc5d676a755dcb0af554e07d95ca
5
5
  SHA512:
6
- metadata.gz: 16903213c77a60b38d8a034ac88b39deaddf926474fcc9eeb575c7633d4742a3aab51c1a6e56b094bcbbc6dff5ae5ed5911a9c4e17bd12cc14df51f1f230e790
7
- data.tar.gz: 5f3da501cb763c39f330cc8406bbd9a4e6db76db255612884c92fbb89a5fca0f8d8f81f72295941bc078b969bf5a3f1e2cfd1552eccda9cff30930c244398961
6
+ metadata.gz: f6f707de2a1555d6dbc7756b60e8a981de97f137787dc5212b78807060d43877e0c5b18f3ad191262eabb618bed356feb32e9d96d3adf93b14bbbbe73bf2452b
7
+ data.tar.gz: 8612f9609a9d1c9a8a9de4358ee66c30458158598917a3e59d09fb83582a8cdc68f7c395f577179363407fd63cbd9f72d4864b77ee957f4e4e63f6a2aad746f2
data/README.md CHANGED
@@ -34,6 +34,7 @@ It is currently used in production in several projects (mainly as request parame
34
34
  - [`maximum(max, error_key = nil, inclusive: true)`](#maximummax-error_key--nil-inclusive-true)
35
35
  - [`minimum(min, error_key = nil, inclusive: true)`](#minimummin-error_key--nil-inclusive-true)
36
36
  - [`non_empty_string(error_key = nil)`](#non_empty_stringerror_key--nil)
37
+ - [`non_empty_array(error_keys = {}`](#non_empty_arrayerror_keys--)
37
38
  - [`pattern(regexp, error_key = nil)`](#patternregexp-error_key--nil)
38
39
  - [`uuid(error_key = nil)`](#uuiderror_key--nil)
39
40
  - [Special types](#special-types)
@@ -159,7 +160,7 @@ validator.("test") # Datacaster::ErrorResult(["is invalid"])
159
160
 
160
161
  In the code above we ensure that validated value is:
161
162
 
162
- a) a string,
163
+ a) a string,\
163
164
  b) has length > 5.
164
165
 
165
166
  If first condition is not met, second one is not evaluated at all (i.e. evaluation is always "short-circuit", just as one might expect).
@@ -203,9 +204,9 @@ Validating hashes is the main case scenario for datacaster. Several specific con
203
204
 
204
205
  Let's assume we want to validate that a hash (which represents data about a person):
205
206
 
206
- a) is, in fact, a Hash;
207
- b) has exactly 2 keys, `name` and `salary`,
208
- c) key 'name' is a string,
207
+ a) is, in fact, a Hash;\
208
+ b) has exactly 2 keys, `name` and `salary`,\
209
+ c) key 'name' is a string,\
209
210
  d) key 'salary' is an integer:
210
211
 
211
212
  ```ruby
@@ -561,6 +562,15 @@ I18n keys:
561
562
  * not a string – `error_key`, `'.string'`, `'datacaster.errors.string'`
562
563
  * is empty – `error_key`, `'.non_empty_string'`, `'datacaster.errors.non_empty_string'`
563
564
 
565
+ #### `non_empty_array(error_keys = {})`
566
+
567
+ Returns ValidResult if and only if provided value is an array and is not empty. Doesn't transform the value.
568
+
569
+ I18n keys:
570
+
571
+ * not an array – `error_keys[:array]`, `'.array'`, `'datacaster.errors.array'`
572
+ * empty array – `error_keys[:empty]`, `'.empty'`, `'datacaster.errors.empty'`
573
+
564
574
  #### `pattern(regexp, error_key = nil)`
565
575
 
566
576
  Returns ValidResult if and only if provided value is a string and satisfies regexp. Doesn't transform the value. Don't forget to provide start/end markers in the regexp if needed, e.g. `/\A\d+\z/` for digits-only string.
@@ -742,7 +752,7 @@ I18n keys:
742
752
 
743
753
  Returns ValidResult if and only if the value `#is_a?(klass)`. Doesn't transform the value.
744
754
 
745
- I18n keys: `error_key`, `'.must_be'`, `'datacaster.errors.must_be'`. Adds `reference` i18n variable, setting it to `klass.name`.
755
+ I18n keys: `error_key`, `'.must_be'`, `'datacaster.errors.must_be'`. Adds `reference` i18n variable, setting it to `klass.name`.
746
756
 
747
757
  #### `optional(base, on: nil)`
748
758
 
@@ -1162,12 +1172,12 @@ Notice that extra keys of inner hashes could be validated only if each element i
1162
1172
 
1163
1173
  Formally, `array_of(x, error_keys = {})` will return ValidResult if and only if:
1164
1174
 
1165
- a) provided value implements basic array methods (`#map`, `#zip`),
1166
- b) provided value is not `#empty?`,
1175
+ a) provided value implements basic array methods (`#map`, `#zip`),\
1176
+ b) provided value is not `#empty?`,\
1167
1177
  c) each element of the provided value passes validation of `x`.
1168
1178
 
1169
- If a) fails, `ErrorResult(["should be an array"]) is returned.
1170
- If b) fails, `ErrorResult(["should not be empty"])` is returned.
1179
+ If a) fails, `ErrorResult(["should be an array"])` is returned.\
1180
+ If b) fails, `ErrorResult(["should not be empty"])` is returned.\
1171
1181
  If c) fails, `ErrorResult({0 => ..., 1 => ...})` is returned. Wrapped hash contains keys which correspond to initial array's indices, and values correspond to failure returned from `x` validator, called for the corresponding element.
1172
1182
 
1173
1183
  Array schema transforms array if inner type (`x`) transforms element (in this case `array_schema` works more or less like `map` function). Otherwise, it doesn't transform.
@@ -1201,12 +1211,12 @@ person.(name: "John Smith", salary: "100_000")
1201
1211
 
1202
1212
  Formally, hash schema returns ValidResult if and only if:
1203
1213
 
1204
- a) provided value `is_a?(Hash)`,
1205
- b) all values, fetched by keys mentioned in `hash_schema(...)` definition, pass corresponding validations,
1214
+ a) provided value `is_a?(Hash)`,\
1215
+ b) all values, fetched by keys mentioned in `hash_schema(...)` definition, pass corresponding validations,\
1206
1216
  c) after all checks (including logical operators), there are no unchecked keys in the hash.
1207
1217
 
1208
- If a) fails, `ErrorResult(["is not a hash"])` is returned.
1209
- if b) fails, `ErrorResult(key1 => [errors...], key2 => [errors...])` is returned. Each key of wrapped "error hash" corresponds to the key of validated hash, and each value of "error hash" contains array of errors, returned by the corresponding validator.
1218
+ If a) fails, `ErrorResult(["is not a hash"])` is returned.\
1219
+ if b) fails, `ErrorResult(key1 => [errors...], key2 => [errors...])` is returned. Each key of wrapped "error hash" corresponds to the key of validated hash, and each value of "error hash" contains array of errors, returned by the corresponding validator.\
1210
1220
  If b) is fulfilled, then and only then validated hash is checked for extra keys. If they are found, `ErrorResult(extra_key_1 => ["should be absent"], ...)` is returned.
1211
1221
 
1212
1222
  I18n keys:
@@ -1500,10 +1510,10 @@ Of course, order of keys in the definition hash doesn't change the result.
1500
1510
 
1501
1511
  Formally, `transform_to_hash`:
1502
1512
 
1503
- a) transforms (any) value to hash;
1504
- b) this hash will contain keys listed in `transform_to_hash` definition;
1505
- c) value of these keys will be: initial value (*not the corresponding key of it, the value altogether*) transformed with the corresponding validator/type;
1506
- d) if any of the values from c) happen to be `Datacaster.absent`, this value *with its key* is removed from the resultant hash;
1513
+ a) transforms (any) value to hash;\
1514
+ b) this hash will contain keys listed in `transform_to_hash` definition;\
1515
+ c) value of these keys will be: initial value (*not the corresponding key of it, the value altogether*) transformed with the corresponding validator/type;\
1516
+ d) if any of the values from c) happen to be `Datacaster.absent`, this value *with its key* is removed from the resultant hash;\
1507
1517
  e) if the initial value happens to also be a hash, all its unvalidated (unused) keys are merged to the resultant hash.
1508
1518
 
1509
1519
  `transform_to_hash` will return ValidResult if and only if all transformations return ValidResults.
@@ -451,6 +451,10 @@ module Datacaster
451
451
  string(error_key) & check { |x| !x.empty? }.i18n_key(*error_keys)
452
452
  end
453
453
 
454
+ def non_empty_array(error_keys = {})
455
+ array_of(any, error_keys, allow_empty: false)
456
+ end
457
+
454
458
  def uuid(error_key = nil)
455
459
  error_keys = ['.uuid', 'datacaster.errors.uuid']
456
460
  error_keys.unshift(error_key) if error_key
@@ -1,3 +1,3 @@
1
1
  module Datacaster
2
- VERSION = "6.0.1"
2
+ VERSION = "6.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datacaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.1
4
+ version: 6.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-10-14 00:00:00.000000000 Z
11
+ date: 2025-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel