datacaster 6.0.1 → 6.0.3

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: 530416943895584aa390aeb22eec1724349a073ff782f56e575d679c700eb636
4
+ data.tar.gz: 2281df272c23df501134a4b7ad1c0471065cae708b613d5e6c3b74e3ddb6bef7
5
5
  SHA512:
6
- metadata.gz: 16903213c77a60b38d8a034ac88b39deaddf926474fcc9eeb575c7633d4742a3aab51c1a6e56b094bcbbc6dff5ae5ed5911a9c4e17bd12cc14df51f1f230e790
7
- data.tar.gz: 5f3da501cb763c39f330cc8406bbd9a4e6db76db255612884c92fbb89a5fca0f8d8f81f72295941bc078b969bf5a3f1e2cfd1552eccda9cff30930c244398961
6
+ metadata.gz: 23c2bda85de1bab4c3b5e75b274fde4b8caae866a0c2c399b819654b242f915663adebec136bebbc5ce6ce272f00a9c1e9551e9a9acb0c3d7a53dc6231347080
7
+ data.tar.gz: a953b5397a17a02c3b0833e0e4ab914d3ca8b8c712eccb58be0342ed9e10083e62b06c951e8e8b313a26c13a28be16e1ac83be9db6b2ad1ce98befef4a9e9e78
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.
data/datacaster.gemspec CHANGED
@@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency 'dry-monads', '>= 1.3', '< 1.4'
30
30
 
31
31
  spec.add_runtime_dependency 'zeitwerk', '>= 2', '< 3'
32
+ spec.add_runtime_dependency 'js_regex'
32
33
  end
@@ -1,3 +1,5 @@
1
+ require 'js_regex'
2
+
1
3
  module Datacaster
2
4
  module Predefined
3
5
  extend self
@@ -363,8 +365,12 @@ module Datacaster
363
365
  def pattern(regexp, error_key = nil)
364
366
  error_keys = ['.pattern', 'datacaster.errors.pattern']
365
367
  error_keys.unshift(error_key) if error_key
366
- string(error_key) & check { |x| x.match?(regexp) }.i18n_key(*error_keys, reference: regexp.inspect).
367
- json_schema(pattern: regexp.inspect)
368
+
369
+ ecma_regexp = JsRegex.new(regexp).to_s
370
+
371
+ string(error_key) & check { |x| x.match?(regexp) }.
372
+ i18n_key(*error_keys, reference: ecma_regexp).
373
+ json_schema(pattern: ecma_regexp)
368
374
  end
369
375
 
370
376
  # 'hash' would be a bad method name, because it would override built in Object#hash
@@ -451,10 +457,16 @@ module Datacaster
451
457
  string(error_key) & check { |x| !x.empty? }.i18n_key(*error_keys)
452
458
  end
453
459
 
460
+ def non_empty_array(error_keys = {})
461
+ array_of(any, error_keys, allow_empty: false)
462
+ end
463
+
454
464
  def uuid(error_key = nil)
455
465
  error_keys = ['.uuid', 'datacaster.errors.uuid']
456
466
  error_keys.unshift(error_key) if error_key
457
- pattern(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/, error_key).i18n_key(*error_keys)
467
+ pattern(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/, error_key).
468
+ i18n_key(*error_keys).
469
+ json_schema { { 'type' => 'string', 'format' => 'uuid' } }
458
470
  end
459
471
 
460
472
  # Form request types
@@ -1,3 +1,3 @@
1
1
  module Datacaster
2
- VERSION = "6.0.1"
2
+ VERSION = "6.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,13 @@
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2025-10-14 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activemodel
@@ -106,7 +105,20 @@ dependencies:
106
105
  - - "<"
107
106
  - !ruby/object:Gem::Version
108
107
  version: '3'
109
- description:
108
+ - !ruby/object:Gem::Dependency
109
+ name: js_regex
110
+ requirement: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ type: :runtime
116
+ prerelease: false
117
+ version_requirements: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
110
122
  email:
111
123
  - eugzol@gmail.com
112
124
  executables: []
@@ -181,7 +193,6 @@ licenses:
181
193
  - MIT
182
194
  metadata:
183
195
  source_code_uri: https://github.com/EugZol/datacaster
184
- post_install_message:
185
196
  rdoc_options: []
186
197
  require_paths:
187
198
  - lib
@@ -196,8 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
207
  - !ruby/object:Gem::Version
197
208
  version: '0'
198
209
  requirements: []
199
- rubygems_version: 3.1.6
200
- signing_key:
210
+ rubygems_version: 4.0.3
201
211
  specification_version: 4
202
212
  summary: Run-time type checker and transformer for Ruby
203
213
  test_files: []