121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
|
# File 'lib/json/schema/serializer.rb', line 121
def type_coerce(schema, type, format, obj, required, 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 options[:false_values]
!options[: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, 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), 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, options)]
end.to_h,
)
else
ret
end
end
end
|