Sha256: 75e1d187807bcb62aacec156b4e77514668c95a17c10ba9690fcc4f871c29591

Contents?: true

Size: 1003 Bytes

Versions: 12

Compression:

Stored size: 1003 Bytes

Contents

module Fakeit
  module Validation
    class Validator
      def initialize(operation)
        @operation = operation
      end

      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
        @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'
      end

      def request_content_type
        request_body&.content&.find { |k, _| k =~ %r{^application/.*json} }&.first
      end

      def request_body
        @operation.operation_object.request_body
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
fakeit-0.5.3 lib/fakeit/validation/validator.rb
fakeit-0.5.2 lib/fakeit/validation/validator.rb
fakeit-0.5.1 lib/fakeit/validation/validator.rb
fakeit-0.5.0 lib/fakeit/validation/validator.rb
fakeit-0.4.2 lib/fakeit/validation/validator.rb
fakeit-0.4.1 lib/fakeit/validation/validator.rb
fakeit-0.4.0 lib/fakeit/validation/validator.rb
fakeit-0.3.1 lib/fakeit/validation/validator.rb
fakeit-0.3.0 lib/fakeit/validation/validator.rb
fakeit-0.2.1 lib/fakeit/validation/validator.rb
fakeit-0.2.0 lib/fakeit/validation/validator.rb
fakeit-0.1.5 lib/fakeit/validation/validator.rb