163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/json/schema/serializer.rb', line 163
def type_coerce(schema, type, format, obj, required, using_default, options)
return nil if !required && obj.nil?
case type.to_s
when "null"
nil
when "string"
case obj
when nil
options[:null_through] ? nil : ""
when DateTime, Date, Time, TimeWithZone
case format.to_s
when "date-time"
obj.strftime("%FT%T%:z")
when "date"
obj.strftime("%F")
when "time"
obj.strftime("%T%:z")
else
obj.to_s
end
when Regexp
obj.inspect.gsub(%r{^/|/[a-z]*$}, "")
else
obj.to_s
end
when "integer"
case obj
when true
1
when false
0
when nil
options[:null_through] ? nil : 0
when ""
options[:empty_string_number_coerce_null] ? nil : 0
else
obj.to_i
end
when "number"
case obj
when true
1.0
when false
0.0
when nil
options[:null_through] ? nil : 0.0
when ""
options[:empty_string_number_coerce_null] ? nil : 0.0
else
obj.to_f
end
when "boolean"
if obj.nil? && options[:null_through]
nil
elsif options[:empty_string_boolean_coerce_null] && obj == ""
nil
elsif (false_values = options[:false_values])
!false_values.include?(obj)
elsif options[:no_boolean_coerce]
obj == true
else
obj ? true : false
end
when "array"
items_schema = try_hash(schema, :items)
return options[:null_through] ? nil : [] if obj.nil? || !obj.respond_to?(:map)
return options[:null_through] ? nil : [] if options[:guard_primitive_in_structure] && is_primitive?(obj)
obj.map { |item| walk(items_schema, item, true, using_default, options) }
when "object"
return nil if obj.nil? && options[:null_through]
return options[:null_through] ? nil : {} if options[:guard_primitive_in_structure] && is_primitive?(obj)
properties_schema = try_hash(schema, :properties)
additional_properties_schema = try_hash(schema, :additionalProperties)
required_schema = Set.new(try_hash(schema, :required)&.map(&:to_s))
input_key_transform = options[:schema_key_transform_for_input]
output_key_transform = options[:schema_key_transform_for_output]
ret =
properties_schema.map do |name, property_schema|
input_key = input_key_transform ? input_key_transform.call(name.to_s) : name
output_key = output_key_transform ? output_key_transform.call(name.to_s) : name.to_s
[output_key, walk(property_schema, try_hash(obj, input_key), required_schema.include?(name.to_s), using_default, options)]
end.to_h
if additional_properties_schema
not_additional_keys_array = properties_schema.keys.map(&:to_s)
not_additional_keys = Set.new(input_key_transform ? not_additional_keys_array.map { |k| input_key_transform.call(k) } : not_additional_keys_array)
additional_keys = obj.keys.reject { |key| not_additional_keys.include?(key.to_s) }
ret.merge(
additional_keys.map do |name|
output_key = output_key_transform ? output_key_transform.call(name.to_s) : name.to_s
[output_key, walk(additional_properties_schema, try_hash(obj, name), false, using_default, options)]
end.to_h,
)
else
ret
end
end
end
|