dry-schema 1.1.0 → 1.2.0

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: 5a4bd19052082cf526e57fca967abe2bbfbdcdfd4c1439c09acfb19bc3b25562
4
- data.tar.gz: 82ffd3bf33409876cf372406e480ca25de8776dfad334ff634b0b041f2ca33ed
3
+ metadata.gz: e7dce78efba45d06df82c2de21ab9da5208fef746034e1f3aba396221897540d
4
+ data.tar.gz: 85f73e374a58a8110a3d75f1617d3b015b07b0d3f9c44caf4dceced1930d88df
5
5
  SHA512:
6
- metadata.gz: dfde0da5889a34f7ef68699048cd755462f49d9d6249435fd5e0006a8d482dfa3a130e73397f02a83bbb11a4ad2c6e5c039e53154a0072d7f89e4a71ee89748b
7
- data.tar.gz: dbc0e2f57947b69241c34451980af16b2c60be5cffb770c510f9ddce91daf58a9e6d900eac2884ca88f30c88dce415a4d6bd629d97922a3c79458458fd3ec289
6
+ metadata.gz: b1317319cec5bacf68ec380166226382c4d10818db4b1a575841f04c43b12b2b420877121876ebb13ce85c59b7b88a375c1f9798523cc6fb123e274507203cd2
7
+ data.tar.gz: fa995747a3efd66855e6d8cbb7d07e6d2002cd9e459237acc58b8747dd7f7d728d4a5f57482269d25e3c63319110975e8b01ec713536defd31742812dc92e334
data/CHANGELOG.md CHANGED
@@ -1,7 +1,30 @@
1
+ # v1.2.0 2019-06-13
2
+
3
+ ### Added
4
+
5
+ * Ability to configure your own type container (@Morozzzko)
6
+
7
+ ### Fixed
8
+
9
+ * `filled` macro no longer generates incorrect messages for arrays (issue #151) (@solnic)
10
+ * `filled` macro works correctly with constructor types (@solnic)
11
+ * `filled` works correctly with nested schemas (#149) (@solnic + @timriley)
12
+ * Custom array constructors are no longer discredited by `array` macro (@solnic)
13
+ * `BigDecimal` type is correctly handled by predicate inference (@solnic)
14
+ * Works with latest `dry-logic` which provides the new `respond_to?` predicate (#153) (@flash-gordon)
15
+
16
+ ### Changed
17
+
18
+ * Fixes related to `filled` restored pre-1.1.0 behavior of `:hints` which are again included (@solnic)
19
+ * `filled` no longer uses filter rules to handle empty strings in `Params` (@solnic)
20
+
21
+ [Compare v1.1.0...v1.2.0](https://github.com/dry-rb/dry-schema/compare/v1.1.0...v1.2.0)
22
+
1
23
  # v1.1.0 2019-05-30
2
24
 
3
25
  ### Added
4
26
 
27
+ * `config.messages.default_locale` for setting...default locale (surprise, surprise) (solnic)
5
28
  * `Config` exposes `predicates` setting too (solnic)
6
29
 
7
30
  ### Fixed
@@ -5,6 +5,7 @@ require 'dry/configurable'
5
5
 
6
6
  require 'dry/schema/constants'
7
7
  require 'dry/schema/predicate_registry'
8
+ require 'dry/schema/type_container'
8
9
 
9
10
  module Dry
10
11
  module Schema
@@ -26,6 +27,15 @@ module Dry
26
27
  # @api public
27
28
  setting(:predicates, Schema::PredicateRegistry.new)
28
29
 
30
+ # @!method types
31
+ #
32
+ # Return configured container with extra types
33
+ #
34
+ # @return [Hash]
35
+ #
36
+ # @api public
37
+ setting(:types, Dry::Types)
38
+
29
39
  # @!method messages
30
40
  #
31
41
  # Return configuration for message backend
@@ -351,7 +351,10 @@ module Dry
351
351
  #
352
352
  # @api private
353
353
  def type_registry
354
- processor_type.config.type_registry
354
+ @type_registry ||= TypeRegistry.new(
355
+ config.types,
356
+ processor_type.config.type_registry_namespace
357
+ )
355
358
  end
356
359
 
357
360
  # Return key map type configured by the processor type
@@ -12,7 +12,7 @@ module Dry
12
12
  # @api public
13
13
  class JSON < Processor
14
14
  config.key_map_type = :stringified
15
- config.type_registry = config.type_registry.namespaced(:json)
15
+ config.type_registry_namespace = :json
16
16
  config.filter_empty_string = false
17
17
  end
18
18
  end
@@ -68,7 +68,6 @@ module Dry
68
68
  # @api public
69
69
  def filled(*args, **opts, &block)
70
70
  append_macro(Macros::Filled) do |macro|
71
- filter(:filled?) if opts[:type_spec] && macro.filter?
72
71
  macro.call(*args, **opts, &block)
73
72
  end
74
73
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'dry/types/type'
3
4
  require 'dry/schema/macros/dsl'
4
5
 
5
6
  module Dry
@@ -12,7 +13,9 @@ module Dry
12
13
  # @api private
13
14
  def value(*args, **opts)
14
15
  extract_type_spec(*args, set_type: false) do |*predicates, type_spec:|
15
- type(schema_dsl.array[type_spec]) if type_spec
16
+ if type_spec && !type_spec.is_a?(Dry::Types::Type)
17
+ type(schema_dsl.array[type_spec])
18
+ end
16
19
 
17
20
  super(*predicates, type_spec: type_spec, **opts)
18
21
  end
@@ -20,12 +20,8 @@ module Dry
20
20
  def call(*predicates, **opts, &block)
21
21
  ensure_valid_predicates(predicates)
22
22
 
23
- if opts[:type_spec]
24
- if filter?
25
- value(*predicates, **opts, &block)
26
- else
27
- value(predicates[0], :filled?, *predicates[1..predicates.size - 1], **opts, &block)
28
- end
23
+ if opts[:type_spec] && !filter_empty_string?
24
+ value(predicates[0], :filled?, *predicates[1..predicates.size - 1], **opts, &block)
29
25
  else
30
26
  value(:filled?, *predicates, **opts, &block)
31
27
  end
@@ -45,8 +41,8 @@ module Dry
45
41
  # rubocop:enable Style/GuardClause
46
42
 
47
43
  # @api private
48
- def filter?
49
- !primitives.include?(NilClass) && processor_config.filter_empty_string
44
+ def filter_empty_string?
45
+ !expected_primitives.include?(NilClass) && processor_config.filter_empty_string
50
46
  end
51
47
 
52
48
  # @api private
@@ -55,7 +51,7 @@ module Dry
55
51
  end
56
52
 
57
53
  # @api private
58
- def primitives
54
+ def expected_primitives
59
55
  primitive_inferrer[schema_type]
60
56
  end
61
57
 
@@ -14,31 +14,45 @@ module Dry
14
14
  super(*args) unless args.empty?
15
15
 
16
16
  if block
17
- definition = schema_dsl.new(&block)
17
+ schema = define(&block)
18
+ trace << schema.to_rule
19
+ end
20
+
21
+ self
22
+ end
18
23
 
19
- parent_type = schema_dsl.types[name]
20
- definition_schema = definition.type_schema
24
+ private
21
25
 
22
- schema_type =
23
- if parent_type.respond_to?(:of)
24
- parent_type.of(definition_schema)
25
- else
26
- definition_schema
27
- end
26
+ # @api private
27
+ def define(&block)
28
+ definition = schema_dsl.new(&block)
29
+ schema = definition.call
28
30
 
29
- final_type =
30
- if parent_type.optional?
31
- schema_type.optional
32
- else
33
- schema_type
34
- end
31
+ type_schema = array? ? parent_type.of(definition.type_schema) : definition.type_schema
32
+ final_type = optional? ? type_schema.optional : type_schema
35
33
 
36
- schema_dsl.set_type(name, final_type)
34
+ type(final_type)
37
35
 
38
- trace << definition.to_rule
36
+ if schema.filter_rules?
37
+ schema_dsl[name].filter { hash?.then(schema(schema.filter_schema)) }
39
38
  end
40
39
 
41
- self
40
+ schema
41
+ end
42
+
43
+ # @api private
44
+ def parent_type
45
+ schema_dsl.types[name]
46
+ end
47
+
48
+ # @api private
49
+ def optional?
50
+ parent_type.optional?
51
+ end
52
+
53
+ # @api private
54
+ def array?
55
+ parent_type.respond_to?(:of)
42
56
  end
43
57
  end
44
58
  end
@@ -12,7 +12,7 @@ module Dry
12
12
  # @api public
13
13
  class Params < Processor
14
14
  config.key_map_type = :stringified
15
- config.type_registry = config.type_registry.namespaced(:params)
15
+ config.type_registry_namespace = :params
16
16
  config.filter_empty_string = true
17
17
  end
18
18
  end
@@ -17,7 +17,8 @@ module Dry
17
17
  Integer => :int?,
18
18
  NilClass => :nil?,
19
19
  String => :str?,
20
- TrueClass => :true?
20
+ TrueClass => :true?,
21
+ BigDecimal => :decimal?
21
22
  }.freeze
22
23
 
23
24
  REDUCED_TYPES = {
@@ -11,9 +11,13 @@ module Dry
11
11
  # @api private
12
12
  attr_reader :predicates
13
13
 
14
+ # @api private
15
+ attr_reader :has_predicate
16
+
14
17
  # @api private
15
18
  def initialize(predicates = Dry::Logic::Predicates)
16
19
  @predicates = predicates
20
+ @has_predicate = ::Kernel.instance_method(:respond_to?).bind(@predicates)
17
21
  end
18
22
 
19
23
  # @api private
@@ -23,7 +27,7 @@ module Dry
23
27
 
24
28
  # @api private
25
29
  def key?(name)
26
- predicates.respond_to?(name)
30
+ has_predicate.(name)
27
31
  end
28
32
 
29
33
  # @api private
@@ -4,6 +4,7 @@ require 'dry/configurable'
4
4
  require 'dry/initializer'
5
5
 
6
6
  require 'dry/schema/type_registry'
7
+ require 'dry/schema/type_container'
7
8
  require 'dry/schema/rule_applier'
8
9
  require 'dry/schema/key_coercer'
9
10
  require 'dry/schema/value_coercer'
@@ -28,7 +29,7 @@ module Dry
28
29
  extend Dry::Configurable
29
30
 
30
31
  setting :key_map_type
31
- setting :type_registry, TypeRegistry.new
32
+ setting :type_registry_namespace, :nominal
32
33
  setting :filter_empty_string, false
33
34
 
34
35
  option :steps, default: -> { EMPTY_ARRAY.dup }
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/container'
4
+ require 'dry/types'
5
+
6
+ module Dry
7
+ module Schema
8
+ # A class to build containers for custom types, which can be used in schemas
9
+ #
10
+ # @example
11
+ # MyTypeContainer = Dry::Schema::TypeContainer.new
12
+ # MyTypeContainer.register('params.fancy_string', Types::FancyString)
13
+ #
14
+ # @api public
15
+ class TypeContainer
16
+ include Dry::Container::Mixin
17
+
18
+ def initialize(types_container = Dry::Types.container)
19
+ super()
20
+
21
+ merge(types_container)
22
+ end
23
+
24
+ alias registered? key?
25
+ end
26
+ end
27
+ end
@@ -36,6 +36,7 @@ module Dry
36
36
  # @api private
37
37
  def [](name)
38
38
  key = [namespace, name].compact.join(DOT)
39
+
39
40
  type = types.registered?(key) ? types[key] : types[name.to_s]
40
41
  type
41
42
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Schema
5
- VERSION = '1.1.0'
5
+ VERSION = '1.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-30 00:00:00.000000000 Z
11
+ date: 2019-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -222,6 +222,7 @@ files:
222
222
  - lib/dry/schema/result.rb
223
223
  - lib/dry/schema/rule_applier.rb
224
224
  - lib/dry/schema/trace.rb
225
+ - lib/dry/schema/type_container.rb
225
226
  - lib/dry/schema/type_registry.rb
226
227
  - lib/dry/schema/types.rb
227
228
  - lib/dry/schema/value_coercer.rb