lib/fakeit/validation/validator.rb in fakeit-0.5.3 vs lib/fakeit/validation/validator.rb in fakeit-0.6.0
- old
+ new
@@ -3,32 +3,36 @@
class Validator
def initialize(operation)
@operation = operation
end
- def validate(body: '', params: {}, headers: {})
+ def validate(body: {}, params: {}, headers: {})
options = OpenAPIParser::SchemaValidator::Options.new(coerce_value: true)
- if request_content_type
- body_data = parse(body)
- @operation.validate_request_body(request_content_type, body_data)
- end
+ validate_body(body) unless request_content_types.empty?
@operation.validate_path_params(options)
@operation.validate_request_parameter(params, headers, options)
rescue StandardError => e
raise ValidationError, e.message
end
private
- def parse(body)
- JSON.parse(body)
- rescue StandardError
- raise ValidationError, 'Invalid json payload'
+ def validate_body(body)
+ if request_content_types.include?(body[:media_type])
+ @operation.validate_request_body(body[:media_type], body[:data]) if can_validate?(body[:media_type])
+ else
+ raise ValidationError, 'Invalid request content type' if body[:media_type]
+ raise ValidationError, 'Request body is required' if request_body.required
+ end
end
- def request_content_type
- request_body&.content&.find { |k, _| k =~ %r{^application/.*json} }&.first
+ def can_validate?(media_type)
+ media_type =~ %r{^application/.*json} || media_type == 'multipart/form-data'
+ end
+
+ def request_content_types
+ request_body&.content&.keys.to_a
end
def request_body
@operation.operation_object.request_body
end