sorbet-schema 0.9.2 → 0.9.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.
@@ -15,9 +15,13 @@ module Typed
15
15
  sig { returns(Schema) }
16
16
  attr_reader :schema
17
17
 
18
+ sig { returns(T::Hash[T::Types::Base, T.untyped]) }
19
+ attr_reader :coercer_cache
20
+
18
21
  sig { params(schema: Schema).void }
19
22
  def initialize(schema:)
20
23
  @schema = schema
24
+ @coercer_cache = T.let({}, T::Hash[T::Types::Base, T.untyped])
21
25
  end
22
26
 
23
27
  sig { abstract.params(source: Input).returns(DeserializeResult) }
@@ -34,41 +38,43 @@ module Typed
34
38
  def deserialize_from_creation_params(creation_params)
35
39
  results = schema.fields.map do |field|
36
40
  value = creation_params.fetch(field.name, nil)
37
- coercer = Coercion::CoercerRegistry.instance.select_coercer_by(type: field.type)
38
41
 
39
42
  if value.nil? && !field.default.nil?
40
43
  Success.new(Validations::ValidatedValue.new(name: field.name, value: field.default))
41
44
  elsif value.nil? || field.works_with?(value)
42
45
  field.validate(value)
43
- elsif !coercer.nil?
44
- result = coercer.new.coerce(type: field.type, value:)
45
- if result.success?
46
- field.validate(result.payload)
47
- else
48
- Failure.new(Validations::ValidationError.new(result.error.message))
49
- end
50
- elsif field.type.class <= T::Types::Union
51
- errors = []
52
- validated_value = T.let(nil, T.nilable(Typed::Result[Typed::Validations::ValidatedValue, Typed::Validations::ValidationError]))
46
+ else
47
+ coercer_instance = fetch_coercer(field.type)
48
+ if !coercer_instance.nil?
49
+ result = coercer_instance.coerce(type: field.type, value:)
50
+ if result.success?
51
+ field.validate(result.payload)
52
+ else
53
+ Failure.new(Validations::ValidationError.new(result.error.message))
54
+ end
55
+ elsif field.type.class <= T::Types::Union
56
+ errors = []
57
+ validated_value = T.let(nil, T.nilable(Typed::Result[Typed::Validations::ValidatedValue, Typed::Validations::ValidationError]))
53
58
 
54
- T.cast(field.type, T::Types::Union).types.each do |sub_type|
55
- # the if clause took care of cases where value is nil so we can skip NilClass
56
- next if sub_type.raw_type.equal?(NilClass)
59
+ T.cast(field.type, T::Types::Union).types.each do |sub_type|
60
+ # the if clause took care of cases where value is nil so we can skip NilClass
61
+ next if sub_type.raw_type.equal?(NilClass)
57
62
 
58
- coercion_result = Coercion.coerce(type: sub_type, value: value)
63
+ coercion_result = Coercion.coerce(type: sub_type, value: value)
59
64
 
60
- if coercion_result.success?
61
- validated_value = field.validate(coercion_result.payload)
65
+ if coercion_result.success?
66
+ validated_value = field.validate(coercion_result.payload)
62
67
 
63
- break
64
- else
65
- errors << Validations::ValidationError.new(coercion_result.error.message)
68
+ break
69
+ else
70
+ errors << Validations::ValidationError.new(coercion_result.error.message)
71
+ end
66
72
  end
67
- end
68
73
 
69
- validated_value.nil? ? Failure.new(Validations::ValidationError.new(errors.map(&:message).join(", "))) : validated_value
70
- else
71
- Failure.new(Validations::ValidationError.new("Coercer not found for type #{field.type}."))
74
+ validated_value.nil? ? Failure.new(Validations::ValidationError.new(errors.map(&:message).join(", "))) : validated_value
75
+ else
76
+ Failure.new(Validations::ValidationError.new("Coercer not found for type #{field.type}."))
77
+ end
72
78
  end
73
79
  end
74
80
 
@@ -90,5 +96,16 @@ module Typed
90
96
 
91
97
  hsh
92
98
  end
99
+
100
+ sig { params(type: T::Types::Base).returns(T.untyped) }
101
+ def fetch_coercer(type)
102
+ cached = coercer_cache[type]
103
+ return cached if cached
104
+
105
+ coercer_class = Coercion::CoercerRegistry.instance.select_coercer_by(type: type)
106
+ return nil if coercer_class.nil?
107
+
108
+ coercer_cache[type] = coercer_class.new
109
+ end
93
110
  end
94
111
  end